Add delete repo

This commit is contained in:
Unknown 2014-03-13 02:39:09 -04:00
parent 18ba149137
commit 27104ac89a
10 changed files with 34 additions and 18 deletions

View File

@ -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())

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -26,6 +26,6 @@
<button type="button" class="btn btn-default"><i class="fa fa-code-fork"></i>Fork&nbsp;&nbsp;{{.Repository.NumForks}}</button>
</div>
</div>
<h3><i class="fa fa-book fa-lg"></i><a href="/{{.Owner.Name}}/">{{.Owner.Name}}</a> / {{.Repository.Name}}</h3>
<h3><i class="fa fa-book fa-lg"></i><a href="/user/{{.Owner.Name}}">{{.Owner.Name}}</a> / {{.Repository.Name}}</h3>
</div>
</div>

View File

@ -22,8 +22,9 @@
<p>This action <strong>CANNOT</strong> be undone. This will delete the repository, wiki, issues, and comments permanently. </p>
<form action="/repo/delete" method="post">
<input type="hidden" name="reponame" value="{{.Repository.Name}}"/>
<input type="hidden" name="username" value="{{.Owner.Name}}"/>
<input type="hidden" name="userId" value="{{.Owner.Id}}"/>
<input type="hidden" name="userName" value="{{.Owner.Name}}"/>
<input type="hidden" name="repoId" value="{{.Repository.Id}}"/>
<hr/>
<button class="btn btn-danger btn-lg">I understand the consequences, delete this repository</button>
</form>

View File

@ -25,7 +25,7 @@
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Statics <b class="caret"></b></a>
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Statistic <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Graphic</a></li>
<li><a href="#">Pulse</a></li>

View File

@ -12,6 +12,7 @@
</div>
</div>
<div id="gogs-body" class="container">
{{if .HasInfo}}<div class="alert alert-info">{{.InfoMsg}}</div>{{end}}
<div id="gogs-feed-left" class="col-md-8">
Website is still in the progress of building...please come back later! <strong>{{.SignedUserName}}</strong> is logged!
</div>

View File

@ -7,7 +7,7 @@
<li class="list-group-item list-group-item-success"><a href="/user/setting">Account Profile</a></li>
<li class="list-group-item"><a href="#">Emails and Password</a></li>
<li class="list-group-item"><a href="#">Notifications</a></li>
<li class="list-group-item"><a href="/user/setting/ssh/">SSH Keys</a></li>
<li class="list-group-item"><a href="/user/setting/ssh">SSH Keys</a></li>
<li class="list-group-item"><a href="#">Security</a></li>
<li class="list-group-item"><a href="/user/delete">Delete Account</a></li>
</ul>

2
web.go
View File

@ -69,7 +69,7 @@ func runWeb(*cli.Context) {
m.Get("/user/:username", auth.SignInRequire(false), user.Profile)
m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete)
m.Any("/repo/delete", auth.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
m.Any("/repo/list", auth.SignInRequire(false), repo.List)
m.Get("/:username/:reponame/settings", auth.SignInRequire(false), auth.RepoAssignment(true), repo.Setting)