Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Migration: Change ServiceType String #12672

Merged
merged 25 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5923384
use different structs for MigrateRepoOptions on UI and API
6543 Sep 1, 2020
c0705ba
Fix TokenAuth and rename UID to an understandable Name
6543 Sep 1, 2020
e0fad58
fix swagger doc
6543 Sep 1, 2020
d3a2108
simplify & mk redable
6543 Sep 1, 2020
f66c2e0
R E F A C T O R:
6543 Sep 1, 2020
cbbdb93
Copyright Header
6543 Sep 2, 2020
ed459bb
Deprecate UID - add RepoOwner
6543 Sep 4, 2020
fb35729
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 5, 2020
83c2bf0
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 5, 2020
677e6ce
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 6, 2020
f121872
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 6, 2020
67c1599
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 6, 2020
f6253fe
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 7, 2020
b7c4c9f
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 7, 2020
6aa16ae
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 8, 2020
add56a5
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 8, 2020
45c2d6c
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 8, 2020
31777bf
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 8, 2020
200ead5
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 9, 2020
66a92ac
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 10, 2020
0e70614
adopt repo.go -> migrate.go
6543 Sep 10, 2020
016c326
Merge branch 'master' into api-migration-serviceType-string
6543 Sep 10, 2020
6e68fb1
add comment about each struct purpose
6543 Sep 10, 2020
02ffa90
lint
6543 Sep 10, 2020
be8bd63
Merge branch 'master' into api-migration-serviceType-string
zeripath Sep 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions integrations/api_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ func TestAPIRepoMigrate(t *testing.T) {
user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session)
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOption{
CloneAddr: testCase.cloneURL,
UID: int(testCase.userID),
RepoName: testCase.repoName,
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOptions{
CloneAddr: testCase.cloneURL,
RepoOwnerID: testCase.userID,
RepoName: testCase.repoName,
})
resp := MakeRequest(t, req, NoExpectedStatus)
if resp.Code == http.StatusUnprocessableEntity {
Expand Down Expand Up @@ -360,10 +360,10 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
cloneURL := "https://github.com/go-gitea/test_repo.git"

req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+httpContext.Token,
&api.MigrateRepoOption{
CloneAddr: cloneURL,
UID: int(userID),
RepoName: httpContext.Reponame,
&api.MigrateRepoOptions{
CloneAddr: cloneURL,
RepoOwnerID: userID,
RepoName: httpContext.Reponame,
})
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
respJSON := map[string]string{}
Expand Down
5 changes: 3 additions & 2 deletions models/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"

migration "code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

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

// MigrateConfig returns task config when migrate repository
func (task *Task) MigrateConfig() (*structs.MigrateRepoOption, error) {
func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
if task.Type == structs.TaskTypeMigrateRepo {
var opts structs.MigrateRepoOption
var opts migration.MigrateOptions
err := json.Unmarshal([]byte(task.PayloadContent), &opts)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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://") ||
Expand All @@ -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() {
Expand Down
20 changes: 20 additions & 0 deletions modules/convert/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// 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.

package convert
6543 marked this conversation as resolved.
Show resolved Hide resolved

import (
"strings"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)

// ToCorrectPageSize makes sure page size is in allowed range.
Expand All @@ -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
}
}
26 changes: 25 additions & 1 deletion modules/migrations/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
6543 marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion modules/migrations/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion modules/migrations/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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: "",
Expand Down
4 changes: 2 additions & 2 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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() {
Expand Down
55 changes: 29 additions & 26 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion modules/task/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func RegisterRoutes(m *macaron.Macaron) {

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

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

m.Group("/:username/:reponame", func() {
m.Combo("").Get(reqAnyRepoReader(), repo.Get).
Expand Down
Loading