Skip to content

Commit fd60ebf

Browse files
authored
[API] Migration: Change ServiceType String (#12672)
* use different structs for MigrateRepoOptions on UI and API * Fix TokenAuth and rename UID to an understandable Name * fix swagger doc * simplify & mk redable * R E F A C T O R: migration has now internal 3 structs to store its options: * the Options for WebUI: modules/auth/repo_form.go * the Options for API: modules/structs/repo.go * the option struct with after validation for internal prossessing: modules/migrations/base/options.go * Copyright Header * Deprecate UID - add RepoOwner * adopt repo.go -> migrate.go * add comment about each struct purpose * lint
1 parent daefdd1 commit fd60ebf

File tree

16 files changed

+230
-87
lines changed

16 files changed

+230
-87
lines changed

integrations/api_repo_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ func TestAPIRepoMigrate(t *testing.T) {
316316
user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
317317
session := loginUser(t, user.Name)
318318
token := getTokenForLoggedInUser(t, session)
319-
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOption{
320-
CloneAddr: testCase.cloneURL,
321-
UID: int(testCase.userID),
322-
RepoName: testCase.repoName,
319+
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOptions{
320+
CloneAddr: testCase.cloneURL,
321+
RepoOwnerID: testCase.userID,
322+
RepoName: testCase.repoName,
323323
})
324324
resp := MakeRequest(t, req, NoExpectedStatus)
325325
if resp.Code == http.StatusUnprocessableEntity {
@@ -360,10 +360,10 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
360360
cloneURL := "https://github.com/go-gitea/test_repo.git"
361361

362362
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+httpContext.Token,
363-
&api.MigrateRepoOption{
364-
CloneAddr: cloneURL,
365-
UID: int(userID),
366-
RepoName: httpContext.Reponame,
363+
&api.MigrateRepoOptions{
364+
CloneAddr: cloneURL,
365+
RepoOwnerID: userID,
366+
RepoName: httpContext.Reponame,
367367
})
368368
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
369369
respJSON := map[string]string{}

models/task.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010

11+
migration "code.gitea.io/gitea/modules/migrations/base"
1112
"code.gitea.io/gitea/modules/structs"
1213
"code.gitea.io/gitea/modules/timeutil"
1314

@@ -101,9 +102,9 @@ func (task *Task) UpdateCols(cols ...string) error {
101102
}
102103

103104
// MigrateConfig returns task config when migrate repository
104-
func (task *Task) MigrateConfig() (*structs.MigrateRepoOption, error) {
105+
func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
105106
if task.Type == structs.TaskTypeMigrateRepo {
106-
var opts structs.MigrateRepoOption
107+
var opts migration.MigrateOptions
107108
err := json.Unmarshal([]byte(task.PayloadContent), &opts)
108109
if err != nil {
109110
return nil, err

modules/auth/repo_form.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) bin
5353
}
5454

5555
// MigrateRepoForm form for migrating repository
56+
// this is used to interact with web ui
5657
type MigrateRepoForm struct {
5758
// required: true
5859
CloneAddr string `json:"clone_addr" binding:"Required"`
@@ -84,9 +85,8 @@ func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) bi
8485
// and returns composed URL with needed username and password.
8586
// It also checks if given user has permission when remote address
8687
// is actually a local path.
87-
func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
88-
remoteAddr := strings.TrimSpace(f.CloneAddr)
89-
88+
func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models.User) (string, error) {
89+
remoteAddr = strings.TrimSpace(remoteAddr)
9090
// Remote address can be HTTP/HTTPS/Git URL or local path.
9191
if strings.HasPrefix(remoteAddr, "http://") ||
9292
strings.HasPrefix(remoteAddr, "https://") ||
@@ -95,8 +95,8 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
9595
if err != nil {
9696
return "", models.ErrInvalidCloneAddr{IsURLError: true}
9797
}
98-
if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
99-
u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
98+
if len(authUsername)+len(authPassword) > 0 {
99+
u.User = url.UserPassword(authUsername, authPassword)
100100
}
101101
remoteAddr = u.String()
102102
} else if !user.CanImportLocal() {

modules/convert/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
12
// Copyright 2016 The Gogs Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package convert
67

78
import (
9+
"strings"
10+
811
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/structs"
913
)
1014

1115
// ToCorrectPageSize makes sure page size is in allowed range.
@@ -17,3 +21,19 @@ func ToCorrectPageSize(size int) int {
1721
}
1822
return size
1923
}
24+
25+
// ToGitServiceType return GitServiceType based on string
26+
func ToGitServiceType(value string) structs.GitServiceType {
27+
switch strings.ToLower(value) {
28+
case "github":
29+
return structs.GithubService
30+
case "gitea":
31+
return structs.GiteaService
32+
case "gitlab":
33+
return structs.GitlabService
34+
case "gogs":
35+
return structs.GogsService
36+
default:
37+
return structs.PlainGitService
38+
}
39+
}

modules/migrations/base/options.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,28 @@ package base
88
import "code.gitea.io/gitea/modules/structs"
99

1010
// MigrateOptions defines the way a repository gets migrated
11-
type MigrateOptions = structs.MigrateRepoOption
11+
// this is for internal usage by migrations module and func who interact with it
12+
type MigrateOptions struct {
13+
// required: true
14+
CloneAddr string `json:"clone_addr" binding:"Required"`
15+
AuthUsername string `json:"auth_username"`
16+
AuthPassword string `json:"auth_password"`
17+
AuthToken string `json:"auth_token"`
18+
// required: true
19+
UID int `json:"uid" binding:"Required"`
20+
// required: true
21+
RepoName string `json:"repo_name" binding:"Required"`
22+
Mirror bool `json:"mirror"`
23+
Private bool `json:"private"`
24+
Description string `json:"description"`
25+
OriginalURL string
26+
GitServiceType structs.GitServiceType
27+
Wiki bool
28+
Issues bool
29+
Milestones bool
30+
Labels bool
31+
Releases bool
32+
Comments bool
33+
PullRequests bool
34+
MigrateToRepoID int64
35+
}

modules/migrations/gitea.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
123123
return err
124124
}
125125

126-
r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, structs.MigrateRepoOption{
126+
r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, base.MigrateOptions{
127127
RepoName: g.repoName,
128128
Description: repo.Description,
129129
OriginalURL: repo.OriginalURL,

modules/migrations/gitea_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/graceful"
15+
"code.gitea.io/gitea/modules/migrations/base"
1516
"code.gitea.io/gitea/modules/structs"
1617
"code.gitea.io/gitea/modules/util"
1718

@@ -32,7 +33,7 @@ func TestGiteaUploadRepo(t *testing.T) {
3233
uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName)
3334
)
3435

35-
err := migrateRepository(downloader, uploader, structs.MigrateRepoOption{
36+
err := migrateRepository(downloader, uploader, base.MigrateOptions{
3637
CloneAddr: "https://github.com/go-xorm/builder",
3738
RepoName: repoName,
3839
AuthUsername: "",

modules/repository/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/log"
16+
migration "code.gitea.io/gitea/modules/migrations/base"
1617
"code.gitea.io/gitea/modules/setting"
17-
api "code.gitea.io/gitea/modules/structs"
1818
"code.gitea.io/gitea/modules/timeutil"
1919
"code.gitea.io/gitea/modules/util"
2020

@@ -41,7 +41,7 @@ func WikiRemoteURL(remote string) string {
4141
}
4242

4343
// MigrateRepositoryGitData starts migrating git related data after created migrating repository
44-
func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts api.MigrateRepoOption) (*models.Repository, error) {
44+
func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) {
4545
repoPath := models.RepoPath(u.Name, opts.RepoName)
4646

4747
if u.IsOrganization() {

modules/structs/repo.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ func (gt GitServiceType) Title() string {
226226
return ""
227227
}
228228

229+
// MigrateRepoOptions options for migrating repository's
230+
// this is used to interact with api v1
231+
type MigrateRepoOptions struct {
232+
// required: true
233+
CloneAddr string `json:"clone_addr" binding:"Required"`
234+
// deprecated (only for backwards compatibility)
235+
RepoOwnerID int64 `json:"uid"`
236+
// Name of User or Organisation who will own Repo after migration
237+
RepoOwner string `json:"repo_owner"`
238+
// required: true
239+
RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
240+
241+
// enum: git,github,gitea,gitlab
242+
Service string `json:"service"`
243+
AuthUsername string `json:"auth_username"`
244+
AuthPassword string `json:"auth_password"`
245+
AuthToken string `json:"auth_token"`
246+
247+
Mirror bool `json:"mirror"`
248+
Private bool `json:"private"`
249+
Description string `json:"description" binding:"MaxSize(255)"`
250+
Wiki bool `json:"wiki"`
251+
Milestones bool `json:"milestones"`
252+
Labels bool `json:"labels"`
253+
Issues bool `json:"issues"`
254+
PullRequests bool `json:"pull_requests"`
255+
Releases bool `json:"releases"`
256+
}
257+
229258
// TokenAuth represents whether a service type supports token-based auth
230259
func (gt GitServiceType) TokenAuth() bool {
231260
switch gt {
@@ -243,29 +272,3 @@ var (
243272
GitlabService,
244273
}
245274
)
246-
247-
// MigrateRepoOption options for migrating a repository from an external service
248-
type MigrateRepoOption struct {
249-
// required: true
250-
CloneAddr string `json:"clone_addr" binding:"Required"`
251-
AuthUsername string `json:"auth_username"`
252-
AuthPassword string `json:"auth_password"`
253-
AuthToken string `json:"auth_token"`
254-
// required: true
255-
UID int `json:"uid" binding:"Required"`
256-
// required: true
257-
RepoName string `json:"repo_name" binding:"Required"`
258-
Mirror bool `json:"mirror"`
259-
Private bool `json:"private"`
260-
Description string `json:"description"`
261-
OriginalURL string
262-
GitServiceType GitServiceType
263-
Wiki bool
264-
Issues bool
265-
Milestones bool
266-
Labels bool
267-
Releases bool
268-
Comments bool
269-
PullRequests bool
270-
MigrateToRepoID int64
271-
}

modules/task/migrate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/graceful"
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/migrations"
17+
migration "code.gitea.io/gitea/modules/migrations/base"
1718
"code.gitea.io/gitea/modules/notification"
1819
"code.gitea.io/gitea/modules/structs"
1920
"code.gitea.io/gitea/modules/timeutil"
@@ -89,7 +90,7 @@ func runMigrateTask(t *models.Task) (err error) {
8990
return err
9091
}
9192

92-
var opts *structs.MigrateRepoOption
93+
var opts *migration.MigrateOptions
9394
opts, err = t.MigrateConfig()
9495
if err != nil {
9596
return err

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ func RegisterRoutes(m *macaron.Macaron) {
636636

637637
m.Get("/issues/search", repo.SearchIssues)
638638

639-
m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate)
639+
m.Post("/migrate", reqToken(), bind(api.MigrateRepoOptions{}), repo.Migrate)
640640

641641
m.Group("/:username/:reponame", func() {
642642
m.Combo("").Get(reqAnyRepoReader(), repo.Get).

0 commit comments

Comments
 (0)