tests: configure github remaining limit + read token (#9800)

* ci: configure remaining github limmit

* prepend with github since package is common to all migrations

* add RefreshRate

* Update github.go

* add missing space

* go fmt

* Read env variable GITHUB_READ_TOKEN for token

* Update .drone.yml
This commit is contained in:
Antoine GIRARD 2020-01-16 16:15:44 +01:00 committed by GitHub
parent fdb32ab0f8
commit 11885daaa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View file

@ -122,6 +122,8 @@ steps:
environment: environment:
GOPROXY: off GOPROXY: off
TAGS: bindata sqlite sqlite_unlock_notify TAGS: bindata sqlite sqlite_unlock_notify
GITHUB_READ_TOKEN:
from_secret: github_read_token
when: when:
branch: branch:
- master - master

View file

@ -24,6 +24,8 @@ import (
var ( var (
_ base.Downloader = &GithubDownloaderV3{} _ base.Downloader = &GithubDownloaderV3{}
_ base.DownloaderFactory = &GithubDownloaderV3Factory{} _ base.DownloaderFactory = &GithubDownloaderV3Factory{}
// GithubLimitRateRemaining limit to wait for new rate to apply
GithubLimitRateRemaining = 0
) )
func init() { func init() {
@ -115,7 +117,7 @@ func (g *GithubDownloaderV3) SetContext(ctx context.Context) {
} }
func (g *GithubDownloaderV3) sleep() { 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)) timer := time.NewTimer(time.Until(g.rate.Reset.Time))
select { select {
case <-g.ctx.Done(): case <-g.ctx.Done():
@ -124,15 +126,24 @@ func (g *GithubDownloaderV3) sleep() {
case <-timer.C: case <-timer.C:
} }
rates, _, err := g.client.RateLimits(g.ctx) err := g.RefreshRate()
if err != nil { if err != nil {
log.Error("g.client.RateLimits: %s", err) 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 // GetRepoInfo returns a repository information
func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) { func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) {
g.sleep() g.sleep()

View file

@ -6,6 +6,7 @@
package migrations package migrations
import ( import (
"os"
"testing" "testing"
"time" "time"
@ -62,7 +63,11 @@ func assertLabelEqual(t *testing.T, name, color, description string, label *base
} }
func TestGitHubDownloadRepo(t *testing.T) { 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() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, &base.Repository{ assert.EqualValues(t, &base.Repository{