]> source.dussan.org Git - gitea.git/commitdiff
tests: configure github remaining limit + read token (#9800)
authorAntoine GIRARD <sapk@users.noreply.github.com>
Thu, 16 Jan 2020 15:15:44 +0000 (16:15 +0100)
committerGitHub <noreply@github.com>
Thu, 16 Jan 2020 15:15:44 +0000 (16:15 +0100)
* 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

.drone.yml
modules/migrations/github.go
modules/migrations/github_test.go

index 11dea174bb393080f6f327a0100142ef88615c81..3e02a47c444c51f1d23910e4ae52d3fdd5888184 100644 (file)
@@ -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
index 17e49d90d19052fc4e2b6a7baa6379f802e5eb14..a99b05e11cc6451061b402814266bc3c42b44e92 100644 (file)
@@ -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() {
@@ -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():
@@ -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
index 10943e4019a494727fd7597c6f8a46fcc22e8784..9023e2ac4dd1bd2ac30eb64ab06f53826f03d61a 100644 (file)
@@ -6,6 +6,7 @@
 package migrations
 
 import (
+       "os"
        "testing"
        "time"
 
@@ -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{