diff options
author | 6543 <6543@obermui.de> | 2020-09-11 00:29:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-10 23:29:19 +0100 |
commit | fd60ebfe14927657ff5cfa4e75f975eaadae65f1 (patch) | |
tree | 38c3d70146920a13c228fed91bc1a55c9be72436 /modules | |
parent | daefdd1385d12bf0c8321f291dbb6ab242b41c99 (diff) | |
download | gitea-fd60ebfe14927657ff5cfa4e75f975eaadae65f1.tar.gz gitea-fd60ebfe14927657ff5cfa4e75f975eaadae65f1.zip |
[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
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/repo_form.go | 10 | ||||
-rw-r--r-- | modules/convert/utils.go | 20 | ||||
-rw-r--r-- | modules/migrations/base/options.go | 26 | ||||
-rw-r--r-- | modules/migrations/gitea.go | 2 | ||||
-rw-r--r-- | modules/migrations/gitea_test.go | 3 | ||||
-rw-r--r-- | modules/repository/repo.go | 4 | ||||
-rw-r--r-- | modules/structs/repo.go | 55 | ||||
-rw-r--r-- | modules/task/migrate.go | 3 |
8 files changed, 86 insertions, 37 deletions
diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index b3fead7da9..3ad57085b0 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -53,6 +53,7 @@ func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) bin } // MigrateRepoForm form for migrating repository +// this is used to interact with web ui type MigrateRepoForm struct { // required: true CloneAddr string `json:"clone_addr" binding:"Required"` @@ -84,9 +85,8 @@ func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) bi // and returns composed URL with needed username and password. // It also checks if given user has permission when remote address // is actually a local path. -func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) { - remoteAddr := strings.TrimSpace(f.CloneAddr) - +func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models.User) (string, error) { + remoteAddr = strings.TrimSpace(remoteAddr) // Remote address can be HTTP/HTTPS/Git URL or local path. if strings.HasPrefix(remoteAddr, "http://") || strings.HasPrefix(remoteAddr, "https://") || @@ -95,8 +95,8 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) { if err != nil { return "", models.ErrInvalidCloneAddr{IsURLError: true} } - if len(f.AuthUsername)+len(f.AuthPassword) > 0 { - u.User = url.UserPassword(f.AuthUsername, f.AuthPassword) + if len(authUsername)+len(authPassword) > 0 { + u.User = url.UserPassword(authUsername, authPassword) } remoteAddr = u.String() } else if !user.CanImportLocal() { diff --git a/modules/convert/utils.go b/modules/convert/utils.go index ddb8a8820d..69de306689 100644 --- a/modules/convert/utils.go +++ b/modules/convert/utils.go @@ -1,3 +1,4 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. // Copyright 2016 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -5,7 +6,10 @@ package convert import ( + "strings" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/structs" ) // ToCorrectPageSize makes sure page size is in allowed range. @@ -17,3 +21,19 @@ func ToCorrectPageSize(size int) int { } return size } + +// ToGitServiceType return GitServiceType based on string +func ToGitServiceType(value string) structs.GitServiceType { + switch strings.ToLower(value) { + case "github": + return structs.GithubService + case "gitea": + return structs.GiteaService + case "gitlab": + return structs.GitlabService + case "gogs": + return structs.GogsService + default: + return structs.PlainGitService + } +} diff --git a/modules/migrations/base/options.go b/modules/migrations/base/options.go index 2d180b61d9..dbc40b138a 100644 --- a/modules/migrations/base/options.go +++ b/modules/migrations/base/options.go @@ -8,4 +8,28 @@ package base import "code.gitea.io/gitea/modules/structs" // MigrateOptions defines the way a repository gets migrated -type MigrateOptions = structs.MigrateRepoOption +// this is for internal usage by migrations module and func who interact with it +type MigrateOptions struct { + // required: true + CloneAddr string `json:"clone_addr" binding:"Required"` + AuthUsername string `json:"auth_username"` + AuthPassword string `json:"auth_password"` + AuthToken string `json:"auth_token"` + // required: true + UID int `json:"uid" binding:"Required"` + // required: true + RepoName string `json:"repo_name" binding:"Required"` + Mirror bool `json:"mirror"` + Private bool `json:"private"` + Description string `json:"description"` + OriginalURL string + GitServiceType structs.GitServiceType + Wiki bool + Issues bool + Milestones bool + Labels bool + Releases bool + Comments bool + PullRequests bool + MigrateToRepoID int64 +} diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go index 082ddcd5fb..b70ad7b0ce 100644 --- a/modules/migrations/gitea.go +++ b/modules/migrations/gitea.go @@ -123,7 +123,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate return err } - r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, structs.MigrateRepoOption{ + r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, base.MigrateOptions{ RepoName: g.repoName, Description: repo.Description, OriginalURL: repo.OriginalURL, diff --git a/modules/migrations/gitea_test.go b/modules/migrations/gitea_test.go index 62c8f71322..2dbd8ffd44 100644 --- a/modules/migrations/gitea_test.go +++ b/modules/migrations/gitea_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -32,7 +33,7 @@ func TestGiteaUploadRepo(t *testing.T) { uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName) ) - err := migrateRepository(downloader, uploader, structs.MigrateRepoOption{ + err := migrateRepository(downloader, uploader, base.MigrateOptions{ CloneAddr: "https://github.com/go-xorm/builder", RepoName: repoName, AuthUsername: "", diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 2d5551d987..36e9ed49c1 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -13,8 +13,8 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/setting" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -41,7 +41,7 @@ func WikiRemoteURL(remote string) string { } // MigrateRepositoryGitData starts migrating git related data after created migrating repository -func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts api.MigrateRepoOption) (*models.Repository, error) { +func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) { repoPath := models.RepoPath(u.Name, opts.RepoName) if u.IsOrganization() { diff --git a/modules/structs/repo.go b/modules/structs/repo.go index f751c00789..c57702b282 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -226,6 +226,35 @@ func (gt GitServiceType) Title() string { return "" } +// MigrateRepoOptions options for migrating repository's +// this is used to interact with api v1 +type MigrateRepoOptions struct { + // required: true + CloneAddr string `json:"clone_addr" binding:"Required"` + // deprecated (only for backwards compatibility) + RepoOwnerID int64 `json:"uid"` + // Name of User or Organisation who will own Repo after migration + RepoOwner string `json:"repo_owner"` + // required: true + RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"` + + // enum: git,github,gitea,gitlab + Service string `json:"service"` + AuthUsername string `json:"auth_username"` + AuthPassword string `json:"auth_password"` + AuthToken string `json:"auth_token"` + + Mirror bool `json:"mirror"` + Private bool `json:"private"` + Description string `json:"description" binding:"MaxSize(255)"` + Wiki bool `json:"wiki"` + Milestones bool `json:"milestones"` + Labels bool `json:"labels"` + Issues bool `json:"issues"` + PullRequests bool `json:"pull_requests"` + Releases bool `json:"releases"` +} + // TokenAuth represents whether a service type supports token-based auth func (gt GitServiceType) TokenAuth() bool { switch gt { @@ -243,29 +272,3 @@ var ( GitlabService, } ) - -// MigrateRepoOption options for migrating a repository from an external service -type MigrateRepoOption struct { - // required: true - CloneAddr string `json:"clone_addr" binding:"Required"` - AuthUsername string `json:"auth_username"` - AuthPassword string `json:"auth_password"` - AuthToken string `json:"auth_token"` - // required: true - UID int `json:"uid" binding:"Required"` - // required: true - RepoName string `json:"repo_name" binding:"Required"` - Mirror bool `json:"mirror"` - Private bool `json:"private"` - Description string `json:"description"` - OriginalURL string - GitServiceType GitServiceType - Wiki bool - Issues bool - Milestones bool - Labels bool - Releases bool - Comments bool - PullRequests bool - MigrateToRepoID int64 -} diff --git a/modules/task/migrate.go b/modules/task/migrate.go index d3b4fa45f0..9d6c8bf733 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" + migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -89,7 +90,7 @@ func runMigrateTask(t *models.Task) (err error) { return err } - var opts *structs.MigrateRepoOption + var opts *migration.MigrateOptions opts, err = t.MigrateConfig() if err != nil { return err |