mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-22 12:54:53 -05:00
Update repo.Create
This commit is contained in:
parent
5a05d6633d
commit
76ce6f9848
8 changed files with 94 additions and 32 deletions
2
gogs.go
2
gogs.go
|
@ -19,7 +19,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.2.0307"
|
||||
const APP_VER = "0.0.2.0308"
|
||||
|
||||
func init() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
// Web form interface.
|
||||
type Form interface {
|
||||
Name(field string) string
|
||||
}
|
||||
|
|
53
modules/auth/repo.go
Normal file
53
modules/auth/repo.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
|
||||
"github.com/gogits/binding"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
type CreateRepoForm struct {
|
||||
UserId int64 `form:"userId"`
|
||||
RepoName string `form:"repo" binding:"Required"`
|
||||
Visibility string `form:"visibility"`
|
||||
Description string `form:"desc" binding:"MaxSize(100)"`
|
||||
Language string `form:"language"`
|
||||
InitReadme string `form:"initReadme"`
|
||||
}
|
||||
|
||||
func (f *CreateRepoForm) Name(field string) string {
|
||||
names := map[string]string{
|
||||
"RepoName": "Repository name",
|
||||
"Description": "Description",
|
||||
}
|
||||
return names[field]
|
||||
}
|
||||
|
||||
func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
|
||||
if req.Method == "GET" || errors.Count() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
|
||||
data["HasError"] = true
|
||||
AssignForm(f, data)
|
||||
|
||||
if len(errors.Overall) > 0 {
|
||||
for _, err := range errors.Overall {
|
||||
log.Error("CreateRepoForm.Validate: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
validate(errors, data, f)
|
||||
}
|
|
@ -7,7 +7,6 @@ package repo
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/martini-contrib/render"
|
||||
"github.com/martini-contrib/sessions"
|
||||
|
@ -18,7 +17,7 @@ import (
|
|||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||
func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||
data["Title"] = "Create repository"
|
||||
|
||||
if req.Method == "GET" {
|
||||
|
@ -26,30 +25,37 @@ func Create(req *http.Request, r render.Render, data base.TmplData, session sess
|
|||
return
|
||||
}
|
||||
|
||||
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
|
||||
r.HTML(200, "repo/create", data)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: access check
|
||||
|
||||
id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64)
|
||||
if err == nil {
|
||||
var u *models.User
|
||||
u, err = models.GetUserById(id)
|
||||
if u == nil {
|
||||
err = models.ErrUserNotExist
|
||||
}
|
||||
if err == nil {
|
||||
_, err = models.CreateRepository(u, req.FormValue("name"))
|
||||
}
|
||||
if err == nil {
|
||||
data["RepoName"] = u.Name + "/" + req.FormValue("name")
|
||||
r.HTML(200, "repo/created", data)
|
||||
user, err := models.GetUserById(form.UserId)
|
||||
if err != nil {
|
||||
if err.Error() == models.ErrUserNotExist.Error() {
|
||||
data["HasError"] = true
|
||||
data["ErrorMsg"] = "User does not exist"
|
||||
auth.AssignForm(form, data)
|
||||
r.HTML(200, "repo/create", data)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
data["ErrorMsg"] = err
|
||||
log.Error("repo.Create: %v", err)
|
||||
r.HTML(200, "base/error", data)
|
||||
if err == nil {
|
||||
// TODO: init description and readme
|
||||
if _, err = models.CreateRepository(user, form.RepoName); err == nil {
|
||||
data["RepoName"] = user.Name + "/" + form.RepoName
|
||||
r.HTML(200, "repo/created", data)
|
||||
fmt.Println("good!!!!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
data["ErrorMsg"] = err
|
||||
log.Error("repo.Create: %v", err)
|
||||
r.HTML(200, "base/error", data)
|
||||
}
|
||||
|
||||
func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||
|
|
|
@ -64,8 +64,8 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render
|
|||
}
|
||||
|
||||
data["ErrorMsg"] = err
|
||||
log.Error("user.SignIn: %v", data)
|
||||
r.HTML(200, "base/error", nil)
|
||||
log.Error("user.SignIn: %v", err)
|
||||
r.HTML(200, "base/error", data)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<div class="container" id="gogs-body">
|
||||
<form action="/repo/create" method="post" class="form-horizontal gogs-card" id="gogs-repo-create">
|
||||
<h3>Create New Repository</h3>
|
||||
<div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div>
|
||||
<div class="form-group">
|
||||
<label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label>
|
||||
<div class="col-md-8">
|
||||
|
@ -11,10 +12,10 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="form-group {{if .Err_RepoName}}has-error has-feedback{{end}}">
|
||||
<label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label>
|
||||
<div class="col-md-8">
|
||||
<input name="repo" type="text" class="form-control" placeholder="Type your repository name" required="required">
|
||||
<input name="repo" type="text" class="form-control" placeholder="Type your repository name" value="{{.repo}}" required="required">
|
||||
<span class="help-block">Great repository names are short and memorable. </span>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -23,13 +24,14 @@
|
|||
<label class="col-md-2 control-label">Visibility<strong class="text-danger">*</strong></label>
|
||||
<div class="col-md-8">
|
||||
<p class="form-control-static">Public</p>
|
||||
<input type="hidden" value="public" name="visibility"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="form-group {{if .Err_Description}}has-error has-feedback{{end}}">
|
||||
<label class="col-md-2 control-label">Description</label>
|
||||
<div class="col-md-8">
|
||||
<textarea name="desc" class="form-control" placeholder="Type your repository name"></textarea>
|
||||
<textarea name="desc" class="form-control" placeholder="Type your repository description">{{.desc}}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -51,7 +53,7 @@
|
|||
<div class="col-md-8 col-md-offset-2">
|
||||
<div class="checkbox">
|
||||
<label>
|
||||
<input type="checkbox" value="true" name="init-md">
|
||||
<input type="checkbox" value="" name="initReadme">
|
||||
<strong>Initialize this repository with a README</strong>
|
||||
</label>
|
||||
</div>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{{template "base/head" .}}
|
||||
{{template "base/navbar" .}}
|
||||
<div class="container">
|
||||
<div class="col-md-offset-4 col-md-3">
|
||||
Created successfully!
|
||||
</div>
|
||||
<div class="container" id="gogs-body">
|
||||
<div class="col-md-offset-4 col-md-3">
|
||||
Created successfully!
|
||||
</div>
|
||||
</div>
|
||||
{{template "base/footer" .}}
|
2
web.go
2
web.go
|
@ -69,7 +69,7 @@ func runWeb(*cli.Context) {
|
|||
m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey)
|
||||
m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey)
|
||||
|
||||
m.Any("/repo/create", auth.SignInRequire(true), repo.Create)
|
||||
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/list", auth.SignInRequire(false), repo.List)
|
||||
|
||||
|
|
Loading…
Reference in a new issue