Skip to content

Commit 8a6df00

Browse files
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]>
1 parent bec9233 commit 8a6df00

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

services/migrations/github.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (g *GithubDownloaderV3) LogString() string {
133133
func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) {
134134
githubClient := github.NewClient(client)
135135
if baseURL != "https://github.com" {
136-
githubClient, _ = github.NewClient(client).WithEnterpriseURLs(baseURL, baseURL)
136+
githubClient, _ = githubClient.WithEnterpriseURLs(baseURL, baseURL)
137137
}
138138
g.clients = append(g.clients, githubClient)
139139
g.rates = append(g.rates, nil)
@@ -872,3 +872,18 @@ func (g *GithubDownloaderV3) GetReviews(ctx context.Context, reviewable base.Rev
872872
}
873873
return allReviews, nil
874874
}
875+
876+
// FormatCloneURL add authentication into remote URLs
877+
func (g *GithubDownloaderV3) FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error) {
878+
u, err := url.Parse(remoteAddr)
879+
if err != nil {
880+
return "", err
881+
}
882+
if len(opts.AuthToken) > 0 {
883+
// "multiple tokens" are used to benefit more "API rate limit quota"
884+
// git clone doesn't count for rate limits, so only use the first token.
885+
// source: https://github.com/orgs/community/discussions/44515
886+
u.User = url.UserPassword("oauth2", strings.Split(opts.AuthToken, ",")[0])
887+
}
888+
return u.String(), nil
889+
}

services/migrations/github_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
base "code.gitea.io/gitea/modules/migration"
1313

1414
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1516
)
1617

1718
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(t.Context(), 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)