create repository from web

This commit is contained in:
Lunny Xiao 2014-02-25 15:11:54 +08:00
parent 3b8657d917
commit f7826d4ed7
5 changed files with 74 additions and 7 deletions

View File

@ -46,12 +46,12 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
//
func CreateRepository(user *User, reposName string) (*Repo, error) {
f := RepoPath(user.Name, reposName)
_, err := git.InitRepository(f, false)
_, err := git.InitRepository(f, true)
if err != nil {
return nil, err
}
repo := Repo{OwnerId: user.Id, Name: reposName}
repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)}
session := orm.NewSession()
defer session.Close()
session.Begin()
@ -64,6 +64,19 @@ func CreateRepository(user *User, reposName string) (*Repo, error) {
session.Rollback()
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)
if err != nil {
err2 := os.RemoveAll(f)

View File

@ -161,6 +161,18 @@ func GetUserByKeyId(keyId int64) (*User, error) {
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.
func LoginUserPlain(name, passwd string) (*User, error) {
user := User{Name: name, Passwd: passwd}

View File

@ -7,6 +7,7 @@ package repo
import (
"fmt"
"net/http"
"strconv"
"github.com/martini-contrib/render"
@ -21,12 +22,33 @@ func Create(req *http.Request, r render.Render) {
return
}
u := &models.User{}
_, err := models.CreateRepository(u, "")
// TODO: access check
fmt.Println(req.FormValue("userId"), req.FormValue("name"))
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) {
if req.Method == "GET" {

View File

@ -3,6 +3,18 @@
<div class="container">
<form action="/repo/create" method="post" class="form-horizontal">
<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">
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>
</div>

View File

@ -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" .}}