Skip to content

Commit a9d5ab8

Browse files
GiteaBotTheFox0x7wxiaoguanglunny
authored
fix github migration error when using multiple tokens (#34144) (#34302)
Backport #34144 by @TheFox0x7 Git authorization was not taking into account multiple token feature, leading to auth failures Closes: #34141 --------- Co-authored-by: TheFox0x7 <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 5546b42 commit a9d5ab8

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

services/migrations/github.go

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

services/migrations/github_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
base "code.gitea.io/gitea/modules/migration"
1414

1515
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1617
)
1718

1819
func TestGitHubDownloadRepo(t *testing.T) {
@@ -429,3 +430,36 @@ func TestGitHubDownloadRepo(t *testing.T) {
429430
},
430431
}, reviews)
431432
}
433+
434+
func TestGithubMultiToken(t *testing.T) {
435+
testCases := []struct {
436+
desc string
437+
token string
438+
expectedCloneURL string
439+
}{
440+
{
441+
desc: "Single Token",
442+
token: "single_token",
443+
expectedCloneURL: "https://oauth2:[email protected]",
444+
},
445+
{
446+
desc: "Multi Token",
447+
token: "token1,token2",
448+
expectedCloneURL: "https://oauth2:[email protected]",
449+
},
450+
}
451+
factory := GithubDownloaderV3Factory{}
452+
453+
for _, tC := range testCases {
454+
t.Run(tC.desc, func(t *testing.T) {
455+
opts := base.MigrateOptions{CloneAddr: "https://github.com/go-gitea/gitea", AuthToken: tC.token}
456+
client, err := factory.New(context.Background(), opts)
457+
require.NoError(t, err)
458+
459+
cloneURL, err := client.FormatCloneURL(opts, "https://github.com")
460+
require.NoError(t, err)
461+
462+
assert.Equal(t, tC.expectedCloneURL, cloneURL)
463+
})
464+
}
465+
}

0 commit comments

Comments
 (0)