From 27104ac89a4c489809068ae5c6dbe383ce7fbde2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 13 Mar 2014 02:39:09 -0400 Subject: [PATCH] Add delete repo --- gogs.go | 2 +- models/repo.go | 19 +++++++++++++------ modules/auth/repo.go | 6 ++++++ routers/repo/repo.go | 11 ++++++----- templates/repo/nav.tmpl | 2 +- templates/repo/setting.tmpl | 5 +++-- templates/repo/toolbar.tmpl | 2 +- templates/user/dashboard.tmpl | 1 + templates/user/setting.tmpl | 2 +- web.go | 2 +- 10 files changed, 34 insertions(+), 18 deletions(-) diff --git a/gogs.go b/gogs.go index 6865e1f213a..f1a702ed223 100644 --- a/gogs.go +++ b/gogs.go @@ -20,7 +20,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.5.0313" +const APP_VER = "0.0.6.0313" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index 6387090ed00..bc6a8f01952 100644 --- a/models/repo.go +++ b/models/repo.go @@ -105,7 +105,6 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv return nil, err } - // TODO: RemoveAll may fail due to not root access. access := Access{ UserName: user.Name, RepoName: repo.Name, @@ -329,13 +328,21 @@ func RepoPath(userName, repoName string) string { } // DeleteRepository deletes a repository for a user or orgnaztion. -func DeleteRepository(user *User, reposName string) (err error) { +func DeleteRepository(userId, repoId int64, userName string) (err error) { + repo := &Repository{Id: repoId, OwnerId: userId} + has, err := orm.Get(repo) + if err != nil { + return err + } else if !has { + return ErrRepoNotExist + } + session := orm.NewSession() - if _, err = session.Delete(&Repository{OwnerId: user.Id, Name: reposName}); err != nil { + if _, err = session.Delete(&Repository{Id: repoId}); err != nil { session.Rollback() return err } - if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil { + if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", userId); err != nil { session.Rollback() return err } @@ -343,9 +350,9 @@ func DeleteRepository(user *User, reposName string) (err error) { session.Rollback() return err } - if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil { + if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil { // TODO: log and delete manully - log.Error("delete repo %s/%s failed", user.Name, reposName) + log.Error("delete repo %s/%s failed", userName, repo.Name) return err } return nil diff --git a/modules/auth/repo.go b/modules/auth/repo.go index d769664649e..5fe091d62f9 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -56,6 +56,12 @@ func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, con validate(errors, data, f) } +type DeleteRepoForm struct { + UserId int64 `form:"userId" binding:"Required"` + UserName string `form:"userName" binding:"Required"` + RepoId int64 `form:"repoId" binding:"Required"` +} + func RepoAssignment(redirect bool) martini.Handler { return func(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { // assign false first diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 5769aff7c3a..a12b45d7b97 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -5,9 +5,9 @@ package repo import ( - "net/http" "github.com/martini-contrib/render" "github.com/martini-contrib/sessions" + "net/http" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -68,7 +68,7 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b r.HTML(200, "base/error", data) } -func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { +func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { data["Title"] = "Delete repository" if req.Method == "GET" { @@ -76,13 +76,14 @@ func Delete(req *http.Request, r render.Render, data base.TmplData, session sess return } - u := &models.User{} - err := models.DeleteRepository(u, "") - if err != nil { + if err := models.DeleteRepository(form.UserId, form.RepoId, form.UserName); err != nil { data["ErrorMsg"] = err log.Error("repo.Delete: %v", err) r.HTML(200, "base/error", data) + return } + + r.Redirect("/", 200) } func List(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { diff --git a/templates/repo/nav.tmpl b/templates/repo/nav.tmpl index 1e524888974..5d60d461071 100644 --- a/templates/repo/nav.tmpl +++ b/templates/repo/nav.tmpl @@ -26,6 +26,6 @@ -

{{.Owner.Name}} / {{.Repository.Name}}

+

{{.Owner.Name}} / {{.Repository.Name}}

\ No newline at end of file diff --git a/templates/repo/setting.tmpl b/templates/repo/setting.tmpl index 262b91a187d..5e0a9f6cb96 100644 --- a/templates/repo/setting.tmpl +++ b/templates/repo/setting.tmpl @@ -22,8 +22,9 @@

This action CANNOT be undone. This will delete the repository, wiki, issues, and comments permanently.

- - + + +
diff --git a/templates/repo/toolbar.tmpl b/templates/repo/toolbar.tmpl index fb2145ab3de..a409fcbea76 100644 --- a/templates/repo/toolbar.tmpl +++ b/templates/repo/toolbar.tmpl @@ -25,7 +25,7 @@