create repository from web

tokarchuk/v1.17
Lunny Xiao 11 years ago
parent 3b8657d917
commit f7826d4ed7
  1. 17
      models/repo.go
  2. 12
      models/user.go
  3. 32
      routers/repo/repo.go
  4. 12
      templates/repo/create.tmpl
  5. 8
      templates/repo/created.tmpl

@ -46,12 +46,12 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
// //
func CreateRepository(user *User, reposName string) (*Repo, error) { func CreateRepository(user *User, reposName string) (*Repo, error) {
f := RepoPath(user.Name, reposName) f := RepoPath(user.Name, reposName)
_, err := git.InitRepository(f, false) _, err := git.InitRepository(f, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
repo := Repo{OwnerId: user.Id, Name: reposName} repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)}
session := orm.NewSession() session := orm.NewSession()
defer session.Close() defer session.Close()
session.Begin() session.Begin()
@ -64,6 +64,19 @@ func CreateRepository(user *User, reposName string) (*Repo, error) {
session.Rollback() session.Rollback()
return nil, err return nil, err
} }
access := Access{UserName: user.Name,
RepoName: repo.Name,
Mode: AU_WRITABLE,
}
_, err = session.Insert(&access)
if err != nil {
err2 := os.RemoveAll(f)
if err2 != nil {
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
}
session.Rollback()
return nil, err
}
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id) _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
if err != nil { if err != nil {
err2 := os.RemoveAll(f) err2 := os.RemoveAll(f)

@ -161,6 +161,18 @@ func GetUserByKeyId(keyId int64) (*User, error) {
return user, nil return user, nil
} }
func GetUserById(id int64) (*User, error) {
user := new(User)
has, err := orm.Id(id).Get(user)
if err != nil {
return nil, err
}
if !has {
return nil, ErrUserNotExist
}
return user, nil
}
// LoginUserPlain validates user by raw user name and password. // LoginUserPlain validates user by raw user name and password.
func LoginUserPlain(name, passwd string) (*User, error) { func LoginUserPlain(name, passwd string) (*User, error) {
user := User{Name: name, Passwd: passwd} user := User{Name: name, Passwd: passwd}

@ -7,6 +7,7 @@ package repo
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
@ -21,11 +22,32 @@ func Create(req *http.Request, r render.Render) {
return return
} }
u := &models.User{} // TODO: access check
_, err := models.CreateRepository(u, "") fmt.Println(req.FormValue("userId"), req.FormValue("name"))
r.HTML(403, "status/403", map[string]interface{}{
"Title": fmt.Sprintf("%v", err), id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64)
}) if err == nil {
var user *models.User
user, err = models.GetUserById(id)
if user == nil {
err = models.ErrUserNotExist
}
if err == nil {
_, err = models.CreateRepository(user, req.FormValue("name"))
}
if err == nil {
r.HTML(200, "repo/created", map[string]interface{}{
"RepoName": user.Name + "/" + req.FormValue("name"),
})
return
}
}
if err != nil {
r.HTML(403, "status/403", map[string]interface{}{
"Title": fmt.Sprintf("%v", err),
})
}
} }
func Delete(req *http.Request, r render.Render) { func Delete(req *http.Request, r render.Render) {

@ -3,7 +3,19 @@
<div class="container"> <div class="container">
<form action="/repo/create" method="post" class="form-horizontal"> <form action="/repo/create" method="post" class="form-horizontal">
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-4 col-md-3">
Owner: <input name="userId" type="hidden" value="1"/>lunny
</div>
<div class="col-md-offset-4 col-md-3">
repo name: <input name="name" type="text"/>
</div>
<div class="col-md-offset-4 col-md-3"> <div class="col-md-offset-4 col-md-3">
description(optional): <input name="desc" type="text"/>
</div>
<div class="col-md-offset-4 col-md-3">
</div>
<div class="col-md-offset-4 col-md-3">
<button type="submit" class="btn btn-info">Create repository</button> <button type="submit" class="btn btn-info">Create repository</button>
</div> </div>
</div> </div>

@ -0,0 +1,8 @@
{{template "base/head" .}}
{{template "base/navbar" .}}
<div class="container">
<div class="col-md-offset-4 col-md-3">
Created successfully!
</div>
</div>
{{template "base/footer" .}}
Loading…
Cancel
Save