From 6fe868a4d512adf3d4cf652a777c0c16521fbe5c Mon Sep 17 00:00:00 2001 From: Steven Oud Date: Sun, 4 Oct 2015 17:09:16 +0200 Subject: [PATCH] added repository remove option to api --- cmd/web.go | 1 + routers/api/v1/repo.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cmd/web.go b/cmd/web.go index ec0e4cd74f69..0c0eea3f81c8 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -226,6 +226,7 @@ func runWeb(ctx *cli.Context) { m.Group("", func() { m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), v1.MigrateRepo) + m.Delete("/:owner/:reponame", v1.RemoveRepo) }, middleware.ApiReqToken()) m.Group("/:username/:reponame", func() { diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index d38a0b0ff256..9fd2f92a1ae0 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -253,3 +253,39 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true})) } + +func RemoveRepo(ctx *middleware.Context) { + user, err := models.GetUserByName(ctx.Params(":owner")) + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.HandleAPI(404, err) + } else { + ctx.JSON(500, &base.ApiJsonErr{"GetUserByName: " + err.Error(), base.DOC_URL}) + } + return + } + + repo, err := models.GetRepositoryByName(user.Id, ctx.Params(":reponame")) + if err != nil { + if models.IsErrRepoNotExist(err) { + ctx.HandleAPI(404, err) + } else { + ctx.JSON(500, &base.ApiJsonErr{"GetRepositoryByName: " + err.Error(), base.DOC_URL}) + } + return + } + + if user.IsOrganization() && !user.IsOwnedBy(ctx.User.Id) { + ctx.HandleAPI(403, "Given user is not owner of organization.") + return + } + + if err := models.DeleteRepository(user.Id, repo.ID); err != nil { + log.Error(4, "DeleteRespository: %v:", err) + ctx.HandleAPI(500, err) + return + } + + log.Trace("Repository deleted: %s/%s", user.Name, repo.Name) + ctx.Status(204) +}