Skip to content

Commit 504bb09

Browse files
TheFox0x7wxiaoguang
authored andcommitted
fix github migration error when using multiple tokens (go-gitea#34144)
Git authorization was not taking into account multiple token feature, leading to auth failures Closes: go-gitea#34141 --------- Co-authored-by: wxiaoguang <[email protected]> (cherry picked from commit 8a6df00)
1 parent 82dd008 commit 504bb09

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

services/migrations/github.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (g *GithubDownloaderV3) LogString() string {
140140
func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) {
141141
githubClient := github.NewClient(client)
142142
if baseURL != "https://github.com" {
143-
githubClient, _ = github.NewClient(client).WithEnterpriseURLs(baseURL, baseURL)
143+
githubClient, _ = githubClient.WithEnterpriseURLs(baseURL, baseURL)
144144
}
145145
g.clients = append(g.clients, githubClient)
146146
g.rates = append(g.rates, nil)
@@ -885,3 +885,18 @@ func (g *GithubDownloaderV3) GetReviews(reviewable base.Reviewable) ([]*base.Rev
885885
}
886886
return allReviews, nil
887887
}
888+
889+
// FormatCloneURL add authentication into remote URLs
890+
func (g *GithubDownloaderV3) FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error) {
891+
u, err := url.Parse(remoteAddr)
892+
if err != nil {
893+
return "", err
894+
}
895+
if len(opts.AuthToken) > 0 {
896+
// "multiple tokens" are used to benefit more "API rate limit quota"
897+
// git clone doesn't count for rate limits, so only use the first token.
898+
// source: https://github.com/orgs/community/discussions/44515
899+
u.User = url.UserPassword("oauth2", strings.Split(opts.AuthToken, ",")[0])
900+
}
901+
return u.String(), nil
902+
}

services/migrations/github_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,36 @@ func TestGitHubDownloadRepo(t *testing.T) {
433433
},
434434
}, reviews)
435435
}
436+
437+
func TestGithubMultiToken(t *testing.T) {
438+
testCases := []struct {
439+
desc string
440+
token string
441+
expectedCloneURL string
442+
}{
443+
{
444+
desc: "Single Token",
445+
token: "single_token",
446+
expectedCloneURL: "https://oauth2:[email protected]",
447+
},
448+
{
449+
desc: "Multi Token",
450+
token: "token1,token2",
451+
expectedCloneURL: "https://oauth2:[email protected]",
452+
},
453+
}
454+
factory := GithubDownloaderV3Factory{}
455+
456+
for _, tC := range testCases {
457+
t.Run(tC.desc, func(t *testing.T) {
458+
opts := base.MigrateOptions{CloneAddr: "https://github.com/go-gitea/gitea", AuthToken: tC.token}
459+
client, err := factory.New(t.Context(), opts)
460+
require.NoError(t, err)
461+
462+
cloneURL, err := client.FormatCloneURL(opts, "https://github.com")
463+
require.NoError(t, err)
464+
465+
assert.Equal(t, tC.expectedCloneURL, cloneURL)
466+
})
467+
}
468+
}

0 commit comments

Comments
 (0)