Skip to content

tests: configure github remaining limit + read token #9800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 16, 2020
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ steps:
environment:
GOPROXY: off
TAGS: bindata sqlite sqlite_unlock_notify
GITHUB_READ_TOKEN:
from_secret: github_read_token
when:
branch:
- master
Expand Down
17 changes: 14 additions & 3 deletions modules/migrations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
var (
_ base.Downloader = &GithubDownloaderV3{}
_ base.DownloaderFactory = &GithubDownloaderV3Factory{}
// GithubLimitRateRemaining limit to wait for new rate to apply
GithubLimitRateRemaining = 0
)

func init() {
Expand Down Expand Up @@ -115,7 +117,7 @@ func (g *GithubDownloaderV3) SetContext(ctx context.Context) {
}

func (g *GithubDownloaderV3) sleep() {
for g.rate != nil && g.rate.Remaining <= 0 {
for g.rate != nil && g.rate.Remaining <= GithubLimitRateRemaining {
timer := time.NewTimer(time.Until(g.rate.Reset.Time))
select {
case <-g.ctx.Done():
Expand All @@ -124,13 +126,22 @@ func (g *GithubDownloaderV3) sleep() {
case <-timer.C:
}

rates, _, err := g.client.RateLimits(g.ctx)
err := g.RefreshRate()
if err != nil {
log.Error("g.client.RateLimits: %s", err)
}
}
}

g.rate = rates.GetCore()
// RefreshRate update the current rate (doesn't count in rate limit)
func (g *GithubDownloaderV3) RefreshRate() error {
rates, _, err := g.client.RateLimits(g.ctx)
if err != nil {
return err
}

g.rate = rates.GetCore()
return nil
}

// GetRepoInfo returns a repository information
Expand Down
7 changes: 6 additions & 1 deletion modules/migrations/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package migrations

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -62,7 +63,11 @@ func assertLabelEqual(t *testing.T, name, color, description string, label *base
}

func TestGitHubDownloadRepo(t *testing.T) {
downloader := NewGithubDownloaderV3("", "", "go-gitea", "test_repo")
GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
downloader := NewGithubDownloaderV3(os.Getenv("GITHUB_READ_TOKEN"), "", "go-gitea", "test_repo")
err := downloader.RefreshRate()
assert.NoError(t, err)

repo, err := downloader.GetRepoInfo()
assert.NoError(t, err)
assert.EqualValues(t, &base.Repository{
Expand Down