From fb90bdd403d918c08ade68dcad6527d7d86389bc Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Mon, 7 Apr 2025 22:15:11 +0200 Subject: [PATCH 1/3] fix git error with multiple tokens --- services/migrations/github.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/services/migrations/github.go b/services/migrations/github.go index e512ebc24ac54..e92953cd3ca6d 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -133,7 +133,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) @@ -872,3 +872,15 @@ func (g *GithubDownloaderV3) GetReviews(ctx context.Context, 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 { + u.User = url.UserPassword("oauth2", strings.Split(opts.AuthToken, ",")[0]) + } + return u.String(), nil +} From 4728e741230a3cc1cdfa802213af33f3cb862260 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Tue, 8 Apr 2025 23:21:45 +0200 Subject: [PATCH 2/3] add test for multitoken clones --- services/migrations/github_test.go | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index 2625fb62ec728..6d1a5378b9600 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -12,6 +12,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 922d965bb480ebd062077af2503e9af0101b6433 Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Sun, 13 Apr 2025 12:21:57 +0200 Subject: [PATCH 3/3] add comment with source --- services/migrations/github.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/migrations/github.go b/services/migrations/github.go index e92953cd3ca6d..ce3e5ced9e79b 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -880,6 +880,9 @@ func (g *GithubDownloaderV3) FormatCloneURL(opts MigrateOptions, remoteAddr stri 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