Browse Source

create repository from web

pull/31/head
Lunny Xiao 11 years ago
parent
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

17
models/repo.go

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

12
models/user.go

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

32
routers/repo/repo.go

@ -7,6 +7,7 @@ package repo
import (
"fmt"
"net/http"
"strconv"
"github.com/martini-contrib/render"
@ -21,11 +22,32 @@ func Create(req *http.Request, r render.Render) {
return
}
u := &models.User{}
_, err := models.CreateRepository(u, "")
r.HTML(403, "status/403", map[string]interface{}{
"Title": fmt.Sprintf("%v", err),
})
// 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) {

12
templates/repo/create.tmpl

@ -3,7 +3,19 @@
<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>
</div>

8
templates/repo/created.tmpl

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