From f64ff25984dca80cf21ad8a9ee51e07c9f9e7410 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Mon, 14 Apr 2025 01:20:14 +0200 Subject: [PATCH 1/2] fix github migration error when using multiple tokens (#34144) Git authorization was not taking into account multiple token feature, leading to auth failures Closes: https://github.com/go-gitea/gitea/issues/34141 --------- Co-authored-by: wxiaoguang --- services/migrations/github.go | 17 ++++++++++++++- services/migrations/github_test.go | 34 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/services/migrations/github.go b/services/migrations/github.go index 604ab84b39645..bbb378b8b29af 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -135,7 +135,7 @@ func (g *GithubDownloaderV3) LogString() string { func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) { githubClient := github.NewClient(client) if baseURL != "https://github.com" { - githubClient, _ = github.NewClient(client).WithEnterpriseURLs(baseURL, baseURL) + githubClient, _ = githubClient.WithEnterpriseURLs(baseURL, baseURL) } g.clients = append(g.clients, githubClient) g.rates = append(g.rates, nil) @@ -879,3 +879,18 @@ func (g *GithubDownloaderV3) GetReviews(reviewable base.Reviewable) ([]*base.Rev } return allReviews, nil } + +// FormatCloneURL add authentication into remote URLs +func (g *GithubDownloaderV3) FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error) { + u, err := url.Parse(remoteAddr) + if err != nil { + return "", err + } + if len(opts.AuthToken) > 0 { + // "multiple tokens" are used to benefit more "API rate limit quota" + // git clone doesn't count for rate limits, so only use the first token. + // source: https://github.com/orgs/community/discussions/44515 + u.User = url.UserPassword("oauth2", strings.Split(opts.AuthToken, ",")[0]) + } + return u.String(), nil +} diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index 2b89e6dc0fd16..cbe3b931d617a 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -13,6 +13,7 @@ import ( base "code.gitea.io/gitea/modules/migration" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGitHubDownloadRepo(t *testing.T) { @@ -429,3 +430,36 @@ func TestGitHubDownloadRepo(t *testing.T) { }, }, reviews) } + +func TestGithubMultiToken(t *testing.T) { + testCases := []struct { + desc string + token string + expectedCloneURL string + }{ + { + desc: "Single Token", + token: "single_token", + expectedCloneURL: "https://oauth2:single_token@github.com", + }, + { + desc: "Multi Token", + token: "token1,token2", + expectedCloneURL: "https://oauth2:token1@github.com", + }, + } + factory := GithubDownloaderV3Factory{} + + for _, tC := range testCases { + t.Run(tC.desc, func(t *testing.T) { + opts := base.MigrateOptions{CloneAddr: "https://github.com/go-gitea/gitea", AuthToken: tC.token} + client, err := factory.New(t.Context(), opts) + require.NoError(t, err) + + cloneURL, err := client.FormatCloneURL(opts, "https://github.com") + require.NoError(t, err) + + assert.Equal(t, tC.expectedCloneURL, cloneURL) + }) + } +} From e75132dfddf16a7d83ab40f28496c6db3130f202 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 28 Apr 2025 09:47:00 -0700 Subject: [PATCH 2/2] Fix lint --- services/migrations/github_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index cbe3b931d617a..13f4b358c50d3 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -453,7 +453,7 @@ func TestGithubMultiToken(t *testing.T) { for _, tC := range testCases { t.Run(tC.desc, func(t *testing.T) { opts := base.MigrateOptions{CloneAddr: "https://github.com/go-gitea/gitea", AuthToken: tC.token} - client, err := factory.New(t.Context(), opts) + client, err := factory.New(context.Background(), opts) require.NoError(t, err) cloneURL, err := client.FormatCloneURL(opts, "https://github.com")