Skip to content

Commit 11885da

Browse files
authored
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
1 parent fdb32ab commit 11885da

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

.drone.yml

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ steps:
122122
environment:
123123
GOPROXY: off
124124
TAGS: bindata sqlite sqlite_unlock_notify
125+
GITHUB_READ_TOKEN:
126+
from_secret: github_read_token
125127
when:
126128
branch:
127129
- master

modules/migrations/github.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
var (
2525
_ base.Downloader = &GithubDownloaderV3{}
2626
_ base.DownloaderFactory = &GithubDownloaderV3Factory{}
27+
// GithubLimitRateRemaining limit to wait for new rate to apply
28+
GithubLimitRateRemaining = 0
2729
)
2830

2931
func init() {
@@ -115,7 +117,7 @@ func (g *GithubDownloaderV3) SetContext(ctx context.Context) {
115117
}
116118

117119
func (g *GithubDownloaderV3) sleep() {
118-
for g.rate != nil && g.rate.Remaining <= 0 {
120+
for g.rate != nil && g.rate.Remaining <= GithubLimitRateRemaining {
119121
timer := time.NewTimer(time.Until(g.rate.Reset.Time))
120122
select {
121123
case <-g.ctx.Done():
@@ -124,13 +126,22 @@ func (g *GithubDownloaderV3) sleep() {
124126
case <-timer.C:
125127
}
126128

127-
rates, _, err := g.client.RateLimits(g.ctx)
129+
err := g.RefreshRate()
128130
if err != nil {
129131
log.Error("g.client.RateLimits: %s", err)
130132
}
133+
}
134+
}
131135

132-
g.rate = rates.GetCore()
136+
// RefreshRate update the current rate (doesn't count in rate limit)
137+
func (g *GithubDownloaderV3) RefreshRate() error {
138+
rates, _, err := g.client.RateLimits(g.ctx)
139+
if err != nil {
140+
return err
133141
}
142+
143+
g.rate = rates.GetCore()
144+
return nil
134145
}
135146

136147
// GetRepoInfo returns a repository information

modules/migrations/github_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package migrations
77

88
import (
9+
"os"
910
"testing"
1011
"time"
1112

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

6465
func TestGitHubDownloadRepo(t *testing.T) {
65-
downloader := NewGithubDownloaderV3("", "", "go-gitea", "test_repo")
66+
GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
67+
downloader := NewGithubDownloaderV3(os.Getenv("GITHUB_READ_TOKEN"), "", "go-gitea", "test_repo")
68+
err := downloader.RefreshRate()
69+
assert.NoError(t, err)
70+
6671
repo, err := downloader.GetRepoInfo()
6772
assert.NoError(t, err)
6873
assert.EqualValues(t, &base.Repository{

0 commit comments

Comments
 (0)