diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-10-26 14:54:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-26 14:54:11 +0800 |
commit | 9e85358777ea4bd345f832d08b19915a72cc128b (patch) | |
tree | 56116e7a6acbfd91a29cade7f753802f9ea561ea /routers | |
parent | d2d5910894cfe513f8aa331b30c991db6fbe8e6d (diff) | |
download | gitea-9e85358777ea4bd345f832d08b19915a72cc128b.tar.gz gitea-9e85358777ea4bd345f832d08b19915a72cc128b.zip |
Move some repositories' operations to a standalone service package (#8557)
* Move some repositories' operations to a standalone service package
* improve code
* remove unused codes
* add rollback when fork failed
* add repo when return
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/repos.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/fork.go | 5 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 30 | ||||
-rw-r--r-- | routers/repo/pull.go | 3 | ||||
-rw-r--r-- | routers/repo/repo.go | 12 | ||||
-rw-r--r-- | routers/repo/setting.go | 3 |
6 files changed, 23 insertions, 33 deletions
diff --git a/routers/admin/repos.go b/routers/admin/repos.go index d345c8e76a..73aa66807b 100644 --- a/routers/admin/repos.go +++ b/routers/admin/repos.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers" + repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -38,7 +39,7 @@ func DeleteRepo(ctx *context.Context) { return } - if err := models.DeleteRepository(ctx.User, repo.MustOwner().ID, repo.ID); err != nil { + if err := repo_service.DeleteRepository(ctx.User, repo); err != nil { ctx.ServerError("DeleteRepository", err) return } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 7231daab29..31f2389478 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" + repo_service "code.gitea.io/gitea/services/repository" ) // ListForks list a repository's forks @@ -97,10 +98,12 @@ func CreateFork(ctx *context.APIContext, form api.CreateForkOption) { } forker = org } - fork, err := models.ForkRepository(ctx.User, forker, repo, repo.Name, repo.Description) + + fork, err := repo_service.ForkRepository(ctx.User, forker, repo, repo.Name, repo.Description) if err != nil { ctx.Error(500, "ForkRepository", err) return } + ctx.JSON(202, fork.APIFormat(models.AccessModeOwner)) } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index a4417107ee..422bb0ac3a 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/routers/api/v1/convert" mirror_service "code.gitea.io/gitea/services/mirror" + repo_service "code.gitea.io/gitea/services/repository" ) var searchOrderByMap = map[string]map[string]models.SearchOrderBy{ @@ -207,7 +208,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR if opt.AutoInit && opt.Readme == "" { opt.Readme = "Default" } - repo, err := models.CreateRepository(ctx.User, owner, models.CreateRepoOptions{ + repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{ Name: opt.Name, Description: opt.Description, IssueLabels: opt.IssueLabels, @@ -224,18 +225,11 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR models.IsErrNamePatternNotAllowed(err) { ctx.Error(422, "", err) } else { - if repo != nil { - if err = models.DeleteRepository(ctx.User, ctx.User.ID, repo.ID); err != nil { - log.Error("DeleteRepository: %v", err) - } - } ctx.Error(500, "CreateRepository", err) } return } - notification.NotifyCreateRepository(ctx.User, owner, repo) - ctx.JSON(201, repo.APIFormat(models.AccessModeOwner)) } @@ -433,7 +427,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { repo, err := migrations.MigrateRepository(ctx.User, ctxUser.Name, opts) if err == nil { - notification.NotifyCreateRepository(ctx.User, ctxUser, repo) + notification.NotifyMigrateRepository(ctx.User, ctxUser, repo) log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin)) @@ -876,18 +870,16 @@ func Delete(ctx *context.APIContext) { owner := ctx.Repo.Owner repo := ctx.Repo.Repository - if owner.IsOrganization() && !ctx.User.IsAdmin { - isOwner, err := owner.IsOwnedBy(ctx.User.ID) - if err != nil { - ctx.Error(500, "IsOwnedBy", err) - return - } else if !isOwner { - ctx.Error(403, "", "Given user is not owner of organization.") - return - } + canDelete, err := repo.CanUserDelete(ctx.User) + if err != nil { + ctx.Error(500, "CanUserDelete", err) + return + } else if !canDelete { + ctx.Error(403, "", "Given user is not owner of organization.") + return } - if err := models.DeleteRepository(ctx.User, owner.ID, repo.ID); err != nil { + if err := repo_service.DeleteRepository(ctx.User, repo); err != nil { ctx.Error(500, "DeleteRepository", err) return } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 23d97e7b7e..f3a9aca2a4 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/services/gitdiff" issue_service "code.gitea.io/gitea/services/issue" pull_service "code.gitea.io/gitea/services/pull" + repo_service "code.gitea.io/gitea/services/repository" "github.com/unknwon/com" ) @@ -209,7 +210,7 @@ func ForkPost(ctx *context.Context, form auth.CreateRepoForm) { } } - repo, err := models.ForkRepository(ctx.User, ctxUser, forkRepo, form.RepoName, form.Description) + repo, err := repo_service.ForkRepository(ctx.User, ctxUser, forkRepo, form.RepoName, form.Description) if err != nil { ctx.Data["Err_RepoName"] = true switch { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index bfd0c771b0..cf1845a727 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -17,10 +17,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" - "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/task" "code.gitea.io/gitea/modules/util" + repo_service "code.gitea.io/gitea/services/repository" "github.com/unknwon/com" ) @@ -170,7 +170,7 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) { return } - repo, err := models.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ + repo, err := repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ Name: form.RepoName, Description: form.Description, Gitignores: form.Gitignores, @@ -181,19 +181,11 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) { AutoInit: form.AutoInit, }) if err == nil { - notification.NotifyCreateRepository(ctx.User, ctxUser, repo) - log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name) return } - if repo != nil { - if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil { - log.Error("DeleteRepository: %v", errDelete) - } - } - handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form) } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index c74b273420..663394fe3d 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/mailer" mirror_service "code.gitea.io/gitea/services/mirror" + repo_service "code.gitea.io/gitea/services/repository" "github.com/unknwon/com" "mvdan.cc/xurls/v2" @@ -407,7 +408,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { return } - if err := models.DeleteRepository(ctx.User, ctx.Repo.Owner.ID, repo.ID); err != nil { + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { ctx.ServerError("DeleteRepository", err) return } |