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 /routers | |
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 'routers')
-rw-r--r-- | routers/api/v1/api.go | 2 | ||||
-rw-r--r-- | routers/api/v1/repo/migrate.go | 66 | ||||
-rw-r--r-- | routers/api/v1/swagger/options.go | 3 | ||||
-rw-r--r-- | routers/repo/migrate.go | 4 |
4 files changed, 40 insertions, 35 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 5f472f3518..9e85625770 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -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). diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index fa4b7366e8..019d82031c 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -9,12 +9,12 @@ import ( "errors" "fmt" "net/http" - "net/url" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" @@ -26,7 +26,7 @@ import ( ) // Migrate migrate remote git repository to gitea -func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { +func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) { // swagger:operation POST /repos/migrate repository repoMigrate // --- // summary: Migrate a remote git repository @@ -38,7 +38,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { // - name: body // in: body // schema: - // "$ref": "#/definitions/MigrateRepoForm" + // "$ref": "#/definitions/MigrateRepoOptions" // responses: // "201": // "$ref": "#/responses/Repository" @@ -47,20 +47,25 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { // "422": // "$ref": "#/responses/validationError" - ctxUser := ctx.User - // Not equal means context user is an organization, - // or is another user/organization if current user is admin. - if form.UID != ctxUser.ID { - org, err := models.GetUserByID(form.UID) - if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Error(http.StatusUnprocessableEntity, "", err) - } else { - ctx.Error(http.StatusInternalServerError, "GetUserByID", err) - } - return + //get repoOwner + var ( + repoOwner *models.User + err error + ) + if len(form.RepoOwner) != 0 { + repoOwner, err = models.GetUserByName(form.RepoOwner) + } else if form.RepoOwnerID != 0 { + repoOwner, err = models.GetUserByID(form.RepoOwnerID) + } else { + repoOwner = ctx.User + } + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.Error(http.StatusUnprocessableEntity, "", err) + } else { + ctx.Error(http.StatusInternalServerError, "GetUser", err) } - ctxUser = org + return } if ctx.HasError() { @@ -69,14 +74,14 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { } if !ctx.User.IsAdmin { - if !ctxUser.IsOrganization() && ctx.User.ID != ctxUser.ID { + if !repoOwner.IsOrganization() && ctx.User.ID != repoOwner.ID { ctx.Error(http.StatusForbidden, "", "Given user is not an organization.") return } - if ctxUser.IsOrganization() { + if repoOwner.IsOrganization() { // Check ownership of organization. - isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID) + isOwner, err := repoOwner.IsOwnedBy(ctx.User.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOwnedBy", err) return @@ -87,7 +92,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { } } - remoteAddr, err := form.ParseRemoteAddr(ctx.User) + remoteAddr, err := auth.ParseRemoteAddr(form.CloneAddr, form.AuthUsername, form.AuthPassword, ctx.User) if err != nil { if models.IsErrInvalidCloneAddr(err) { addrErr := err.(models.ErrInvalidCloneAddr) @@ -107,11 +112,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { return } - var gitServiceType = api.PlainGitService - u, err := url.Parse(remoteAddr) - if err == nil && strings.EqualFold(u.Host, "github.com") { - gitServiceType = api.GithubService - } + gitServiceType := convert.ToGitServiceType(form.Service) if form.Mirror && setting.Repository.DisableMirrors { ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled mirrors")) @@ -126,6 +127,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { Mirror: form.Mirror, AuthUsername: form.AuthUsername, AuthPassword: form.AuthPassword, + AuthToken: form.AuthToken, Wiki: form.Wiki, Issues: form.Issues, Milestones: form.Milestones, @@ -144,7 +146,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { opts.Releases = false } - repo, err := repo_module.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ + repo, err := repo_module.CreateRepository(ctx.User, repoOwner, models.CreateRepoOptions{ Name: opts.RepoName, Description: opts.Description, OriginalURL: form.CloneAddr, @@ -154,7 +156,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { Status: models.RepositoryBeingMigrated, }) if err != nil { - handleMigrateError(ctx, ctxUser, remoteAddr, err) + handleMigrateError(ctx, repoOwner, remoteAddr, err) return } @@ -171,24 +173,24 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { if err == nil { repo.Status = models.RepositoryReady if err := models.UpdateRepositoryCols(repo, "status"); err == nil { - notification.NotifyMigrateRepository(ctx.User, ctxUser, repo) + notification.NotifyMigrateRepository(ctx.User, repoOwner, repo) return } } if repo != nil { - if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil { + if errDelete := models.DeleteRepository(ctx.User, repoOwner.ID, repo.ID); errDelete != nil { log.Error("DeleteRepository: %v", errDelete) } } }() - if _, err = migrations.MigrateRepository(graceful.GetManager().HammerContext(), ctx.User, ctxUser.Name, opts); err != nil { - handleMigrateError(ctx, ctxUser, remoteAddr, err) + if _, err = migrations.MigrateRepository(graceful.GetManager().HammerContext(), ctx.User, repoOwner.Name, opts); err != nil { + handleMigrateError(ctx, repoOwner, remoteAddr, err) return } - log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) + log.Trace("Repository migrated: %s/%s", repoOwner.Name, form.RepoName) ctx.JSON(http.StatusCreated, repo.APIFormat(models.AccessModeAdmin)) } diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index d9ef05c335..ced6589e48 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -149,4 +149,7 @@ type swaggerParameterBodies struct { // in:body SubmitPullReviewOptions api.SubmitPullReviewOptions + + // in:body + MigrateRepoOptions api.MigrateRepoOptions } diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 497f2ce36f..34060aabde 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -74,7 +74,7 @@ func handleMigrateError(ctx *context.Context, owner *models.User, err error, nam ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form) default: - remoteAddr, _ := form.ParseRemoteAddr(owner) + remoteAddr, _ := auth.ParseRemoteAddr(form.CloneAddr, form.AuthUsername, form.AuthPassword, owner) err = util.URLSanitizedError(err, remoteAddr) if strings.Contains(err.Error(), "Authentication failed") || strings.Contains(err.Error(), "Bad credentials") || @@ -108,7 +108,7 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) { return } - remoteAddr, err := form.ParseRemoteAddr(ctx.User) + remoteAddr, err := auth.ParseRemoteAddr(form.CloneAddr, form.AuthUsername, form.AuthPassword, ctx.User) if err != nil { if models.IsErrInvalidCloneAddr(err) { ctx.Data["Err_CloneAddr"] = true |