Skip to content

Commit 7fbd39c

Browse files
committed
Backport Migration Things (fix #13085)
1 parent 2bd7fee commit 7fbd39c

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed

modules/migrations/gitea.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,15 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
9393
}
9494

9595
var remoteAddr = repo.CloneURL
96-
if len(opts.AuthUsername) > 0 {
96+
if len(opts.AuthToken) > 0 || len(opts.AuthUsername) > 0 {
9797
u, err := url.Parse(repo.CloneURL)
9898
if err != nil {
9999
return err
100100
}
101101
u.User = url.UserPassword(opts.AuthUsername, opts.AuthPassword)
102+
if len(opts.AuthToken) > 0 {
103+
u.User = url.UserPassword("oauth2", opts.AuthToken)
104+
}
102105
remoteAddr = u.String()
103106
}
104107

modules/migrations/gitea_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestGiteaUploadRepo(t *testing.T) {
2626
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
2727

2828
var (
29-
downloader = NewGithubDownloaderV3("", "", "go-xorm", "builder")
29+
downloader = NewGithubDownloaderV3("", "", "", "go-xorm", "builder")
3030
repoName = "builder-" + time.Now().Format("2006-01-02-15-04-05")
3131
uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName)
3232
)

modules/migrations/github.go

+15-19
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Download
6060

6161
log.Trace("Create github downloader: %s/%s", oldOwner, oldName)
6262

63-
return NewGithubDownloaderV3(opts.AuthUsername, opts.AuthPassword, oldOwner, oldName), nil
63+
return NewGithubDownloaderV3(opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil
6464
}
6565

6666
// GitServiceType returns the type of git service
@@ -81,7 +81,7 @@ type GithubDownloaderV3 struct {
8181
}
8282

8383
// NewGithubDownloaderV3 creates a github Downloader via github v3 API
84-
func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *GithubDownloaderV3 {
84+
func NewGithubDownloaderV3(userName, password, token, repoOwner, repoName string) *GithubDownloaderV3 {
8585
var downloader = GithubDownloaderV3{
8686
userName: userName,
8787
password: password,
@@ -90,23 +90,19 @@ func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *Gith
9090
repoName: repoName,
9191
}
9292

93-
var client *http.Client
94-
if userName != "" {
95-
if password == "" {
96-
ts := oauth2.StaticTokenSource(
97-
&oauth2.Token{AccessToken: userName},
98-
)
99-
client = oauth2.NewClient(downloader.ctx, ts)
100-
} else {
101-
client = &http.Client{
102-
Transport: &http.Transport{
103-
Proxy: func(req *http.Request) (*url.URL, error) {
104-
req.SetBasicAuth(userName, password)
105-
return nil, nil
106-
},
107-
},
108-
}
109-
}
93+
client := &http.Client{
94+
Transport: &http.Transport{
95+
Proxy: func(req *http.Request) (*url.URL, error) {
96+
req.SetBasicAuth(userName, password)
97+
return nil, nil
98+
},
99+
},
100+
}
101+
if token != "" {
102+
ts := oauth2.StaticTokenSource(
103+
&oauth2.Token{AccessToken: token},
104+
)
105+
client = oauth2.NewClient(downloader.ctx, ts)
110106
}
111107
downloader.client = github.NewClient(client)
112108
return &downloader

modules/migrations/github_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func assertLabelEqual(t *testing.T, name, color, description string, label *base
6464

6565
func TestGitHubDownloadRepo(t *testing.T) {
6666
GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
67-
downloader := NewGithubDownloaderV3(os.Getenv("GITHUB_READ_TOKEN"), "", "go-gitea", "test_repo")
67+
downloader := NewGithubDownloaderV3("", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo")
6868
err := downloader.RefreshRate()
6969
assert.NoError(t, err)
7070

modules/migrations/gitlab.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ func (f *GitlabDownloaderFactory) New(opts base.MigrateOptions) (base.Downloader
5656

5757
baseURL := u.Scheme + "://" + u.Host
5858
repoNameSpace := strings.TrimPrefix(u.Path, "/")
59+
repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git")
5960

6061
log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
6162

62-
return NewGitlabDownloader(baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword), nil
63+
return NewGitlabDownloader(baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken), nil
6364
}
6465

6566
// GitServiceType returns the type of git service
@@ -85,15 +86,14 @@ type GitlabDownloader struct {
8586
// NewGitlabDownloader creates a gitlab Downloader via gitlab API
8687
// Use either a username/password, personal token entered into the username field, or anonymous/public access
8788
// Note: Public access only allows very basic access
88-
func NewGitlabDownloader(baseURL, repoPath, username, password string) *GitlabDownloader {
89+
func NewGitlabDownloader(baseURL, repoPath, username, password, token string) *GitlabDownloader {
8990
var gitlabClient *gitlab.Client
9091
var err error
91-
if username != "" {
92-
if password == "" {
93-
gitlabClient, err = gitlab.NewClient(username, gitlab.WithBaseURL(baseURL))
94-
} else {
95-
gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL))
96-
}
92+
gitlabClient, err = gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
93+
// Only use basic auth if token is blank and password is NOT
94+
// Basic auth will fail with empty strings, but empty token will allow anonymous public API usage
95+
if token == "" && password != "" {
96+
gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL))
9797
}
9898

9999
if err != nil {

modules/migrations/gitlab_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
2727
t.Skipf("Can't access test repo, skipping %s", t.Name())
2828
}
2929

30-
downloader := NewGitlabDownloader("https://gitlab.com", "gitea/test_repo", gitlabPersonalAccessToken, "")
30+
downloader := NewGitlabDownloader("https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
3131
if downloader == nil {
3232
t.Fatal("NewGitlabDownloader is nil")
3333
}

modules/migrations/migrate.go

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func MigrateRepository(ctx context.Context, doer *models.User, ownerName string,
3636
theFactory base.DownloaderFactory
3737
)
3838

39+
// determine if user is token
40+
if len(opts.AuthUsername) != 0 && len(opts.AuthPassword) == 0 {
41+
opts.AuthToken = opts.AuthUsername
42+
opts.AuthUsername = ""
43+
}
44+
3945
for _, factory := range factories {
4046
if match, err := factory.Match(opts); err != nil {
4147
return nil, err

modules/structs/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ type MigrateRepoOption struct {
213213
CloneAddr string `json:"clone_addr" binding:"Required"`
214214
AuthUsername string `json:"auth_username"`
215215
AuthPassword string `json:"auth_password"`
216+
AuthToken string `json:"auth_token"`
216217
// required: true
217218
UID int `json:"uid" binding:"Required"`
218219
// required: true

0 commit comments

Comments
 (0)