diff --git a/assets/go-licenses.json b/assets/go-licenses.json index 2819730532..a66088f5f2 100644 --- a/assets/go-licenses.json +++ b/assets/go-licenses.json @@ -700,8 +700,8 @@ "licenseText": "Copyright (c) 2017 The Go Authors. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" }, { - "name": "github.com/google/go-github/v74/github", - "path": "github.com/google/go-github/v74/github/LICENSE", + "name": "github.com/google/go-github/v81/github", + "path": "github.com/google/go-github/v81/github/LICENSE", "licenseText": "Copyright (c) 2013 The go-github AUTHORS. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" }, { diff --git a/go.mod b/go.mod index 1ba59e29f7..e436e372f1 100644 --- a/go.mod +++ b/go.mod @@ -58,7 +58,7 @@ require ( github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85 github.com/golang-jwt/jwt/v5 v5.3.0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 - github.com/google/go-github/v74 v74.0.0 + github.com/google/go-github/v81 v81.0.0 github.com/google/pprof v0.0.0-20251114195745-4902fdda35c8 github.com/google/uuid v1.6.0 github.com/gorilla/feeds v1.2.0 diff --git a/go.sum b/go.sum index e8daca5d1d..db6435939d 100644 --- a/go.sum +++ b/go.sum @@ -384,8 +384,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= -github.com/google/go-github/v74 v74.0.0 h1:yZcddTUn8DPbj11GxnMrNiAnXH14gNs559AsUpNpPgM= -github.com/google/go-github/v74 v74.0.0/go.mod h1:ubn/YdyftV80VPSI26nSJvaEsTOnsjrxG3o9kJhcyak= +github.com/google/go-github/v81 v81.0.0 h1:hTLugQRxSLD1Yei18fk4A5eYjOGLUBKAl/VCqOfFkZc= +github.com/google/go-github/v81 v81.0.0/go.mod h1:upyjaybucIbBIuxgJS7YLOZGziyvvJ92WX6WEBNE3sM= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/go-tpm v0.9.5 h1:ocUmnDebX54dnW+MQWGQRbdaAcJELsa6PqZhJ48KwVU= diff --git a/services/migrations/error.go b/services/migrations/error.go index 2dd9647d95..dee008a071 100644 --- a/services/migrations/error.go +++ b/services/migrations/error.go @@ -7,7 +7,7 @@ package migrations import ( "errors" - "github.com/google/go-github/v74/github" + "github.com/google/go-github/v81/github" ) // ErrRepoNotCreated returns the error that repository not created diff --git a/services/migrations/github.go b/services/migrations/github.go index 87e8248cab..569d9c5780 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -20,7 +20,7 @@ import ( "forgejo.org/modules/proxy" "forgejo.org/modules/structs" - "github.com/google/go-github/v74/github" + "github.com/google/go-github/v81/github" "golang.org/x/oauth2" ) @@ -98,7 +98,6 @@ func NewGithubDownloaderV3(ctx context.Context, baseURL string, getPullRequests, userName: userName, baseURL: baseURL, password: password, - ctx: ctx, repoOwner: repoOwner, repoName: repoName, maxPerPage: 100, @@ -106,6 +105,8 @@ func NewGithubDownloaderV3(ctx context.Context, baseURL string, getPullRequests, getIssues: getIssues, } + downloader.SetContext(ctx) + if token != "" { tokens := strings.Split(token, ",") for _, token := range tokens { @@ -159,6 +160,7 @@ func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) { // SetContext set context func (g *GithubDownloaderV3) SetContext(ctx context.Context) { + ctx = context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true) g.ctx = ctx } diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index 786becd420..f87d38b121 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -5,14 +5,18 @@ package migrations import ( + "net/http" "os" + "regexp" + "strconv" "testing" "time" "forgejo.org/models/unittest" + "forgejo.org/modules/log" base "forgejo.org/modules/migration" - "github.com/google/go-github/v74/github" + "github.com/google/go-github/v81/github" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -97,6 +101,29 @@ func TestGithubDownloaderFilterComments(t *testing.T) { } } +func ratelimitInjectHandler(handler http.Handler, urlpattern *regexp.Regexp, every int) http.HandlerFunc { + var requestCount int + // because we also count the rate limit response + every++ + + return (http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + match := urlpattern.MatchString(r.URL.Path) + if match { + requestCount++ + } + + if match && requestCount%every == 0 { + log.Info("ratelimitInject %s", r.URL) + w.Header().Set("X-Ratelimit-Reset", + strconv.FormatInt(time.Now().Add(time.Second).Unix(), 10)) + w.Header().Set("X-Ratelimit-Remaining", "0") + w.WriteHeader(http.StatusForbidden) + } else { + handler.ServeHTTP(w, r) + } + })) +} + func TestGitHubDownloadRepo(t *testing.T) { GithubLimitRateRemaining = 3 // Wait at 3 remaining since we could have 3 CI in // @@ -105,6 +132,10 @@ func TestGitHubDownloadRepo(t *testing.T) { server := unittest.NewMockWebServer(t, "https://api.github.com", fixturePath, false) defer server.Close() + urlpattern := regexp.MustCompile("test_repo/") + + server.Config.Handler = ratelimitInjectHandler(server.Config.Handler, urlpattern, 7) + downloader := NewGithubDownloaderV3(t.Context(), server.URL, true, true, "", "", token, "forgejo", "test_repo") err := downloader.RefreshRate() require.NoError(t, err)