Support edit release and save as draft

This commit is contained in:
Unknown 2014-06-12 17:47:23 -04:00
parent fb0972afad
commit e07674bff1
13 changed files with 300 additions and 85 deletions

View File

@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language
![Demo](http://gowalker.org/public/gogs_demo.gif) ![Demo](http://gowalker.org/public/gogs_demo.gif)
##### Current version: 0.4.3 Alpha ##### Current version: 0.4.4 Alpha
### NOTICES ### NOTICES

View File

@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。
![Demo](http://gowalker.org/public/gogs_demo.gif) ![Demo](http://gowalker.org/public/gogs_demo.gif)
##### 当前版本0.4.3 Alpha ##### 当前版本0.4.4 Alpha
## 开发目的 ## 开发目的

View File

@ -228,11 +228,13 @@ func runWeb(*cli.Context) {
}) })
r.Post("/comment/:action", repo.Comment) r.Post("/comment/:action", repo.Comment)
r.Get("/releases/new", repo.ReleasesNew) r.Get("/releases/new", repo.NewRelease)
r.Get("/releases/edit/:tagname", repo.EditRelease)
}, reqSignIn, middleware.RepoAssignment(true)) }, reqSignIn, middleware.RepoAssignment(true))
m.Group("/:username/:reponame", func(r martini.Router) { m.Group("/:username/:reponame", func(r martini.Router) {
r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost) r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
r.Post("/releases/edit/:tagname", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
}, reqSignIn, middleware.RepoAssignment(true, true)) }, reqSignIn, middleware.RepoAssignment(true, true))
m.Group("/:username/:reponame", func(r martini.Router) { m.Group("/:username/:reponame", func(r martini.Router) {

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.4.3.0612 Alpha" const APP_VER = "0.4.4.0612 Alpha"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -6,15 +6,16 @@ package models
import ( import (
"errors" "errors"
"sort"
"strings" "strings"
"time" "time"
// "github.com/Unknwon/com"
"github.com/gogits/git" "github.com/gogits/git"
) )
var ( var (
ErrReleaseAlreadyExist = errors.New("Release already exist") ErrReleaseAlreadyExist = errors.New("Release already exist")
ErrReleaseNotExist = errors.New("Release does not exist")
) )
// Release represents a release of repository. // Release represents a release of repository.
@ -23,22 +24,17 @@ type Release struct {
RepoId int64 RepoId int64
PublisherId int64 PublisherId int64
Publisher *User `xorm:"-"` Publisher *User `xorm:"-"`
Title string
TagName string TagName string
LowerTagName string LowerTagName string
Target string Target string
Title string
Sha1 string `xorm:"VARCHAR(40)"` Sha1 string `xorm:"VARCHAR(40)"`
NumCommits int NumCommits int
NumCommitsBehind int `xorm:"-"` NumCommitsBehind int `xorm:"-"`
Note string `xorm:"TEXT"` Note string `xorm:"TEXT"`
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
IsPrerelease bool IsPrerelease bool
Created time.Time `xorm:"created"` Created time.Time `xorm:"CREATED"`
}
// GetReleasesByRepoId returns a list of releases of repository.
func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) {
err = orm.Desc("created").Find(&rels, Release{RepoId: repoId})
return rels, err
} }
// IsReleaseExist returns true if release with given tag name already exists. // IsReleaseExist returns true if release with given tag name already exists.
@ -50,15 +46,9 @@ func IsReleaseExist(repoId int64, tagName string) (bool, error) {
return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)}) return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)})
} }
// CreateRelease creates a new release of repository. func createTag(gitRepo *git.Repository, rel *Release) error {
func CreateRelease(gitRepo *git.Repository, rel *Release) error { // Only actual create when publish.
isExist, err := IsReleaseExist(rel.RepoId, rel.TagName) if !rel.IsDraft {
if err != nil {
return err
} else if isExist {
return ErrReleaseAlreadyExist
}
if !gitRepo.IsTagExist(rel.TagName) { if !gitRepo.IsTagExist(rel.TagName) {
commit, err := gitRepo.GetCommitOfBranch(rel.Target) commit, err := gitRepo.GetCommitOfBranch(rel.Target)
if err != nil { if err != nil {
@ -79,8 +69,78 @@ func CreateRelease(gitRepo *git.Repository, rel *Release) error {
return err return err
} }
} }
}
return nil
}
// CreateRelease creates a new release of repository.
func CreateRelease(gitRepo *git.Repository, rel *Release) error {
isExist, err := IsReleaseExist(rel.RepoId, rel.TagName)
if err != nil {
return err
} else if isExist {
return ErrReleaseAlreadyExist
}
if err = createTag(gitRepo, rel); err != nil {
return err
}
rel.LowerTagName = strings.ToLower(rel.TagName) rel.LowerTagName = strings.ToLower(rel.TagName)
_, err = orm.InsertOne(rel) _, err = orm.InsertOne(rel)
return err return err
} }
// GetRelease returns release by given ID.
func GetRelease(repoId int64, tagName string) (*Release, error) {
isExist, err := IsReleaseExist(repoId, tagName)
if err != nil {
return nil, err
} else if !isExist {
return nil, ErrReleaseNotExist
}
rel := &Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)}
_, err = orm.Get(rel)
return rel, err
}
// GetReleasesByRepoId returns a list of releases of repository.
func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) {
err = orm.Desc("created").Find(&rels, Release{RepoId: repoId})
return rels, err
}
type ReleaseSorter struct {
rels []*Release
}
func (rs *ReleaseSorter) Len() int {
return len(rs.rels)
}
func (rs *ReleaseSorter) Less(i, j int) bool {
diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
if diffNum != 0 {
return diffNum > 0
}
return rs.rels[i].Created.After(rs.rels[j].Created)
}
func (rs *ReleaseSorter) Swap(i, j int) {
rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
}
// SortReleases sorts releases by number of commits and created time.
func SortReleases(rels []*Release) {
sorter := &ReleaseSorter{rels: rels}
sort.Sort(sorter)
}
// UpdateRelease updates information of a release.
func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
if err = createTag(gitRepo, rel); err != nil {
return err
}
_, err = orm.Id(rel.Id).AllCols().Update(rel)
return err
}

View File

@ -208,6 +208,7 @@ type NewReleaseForm struct {
Target string `form:"tag_target" binding:"Required"` Target string `form:"tag_target" binding:"Required"`
Title string `form:"title" binding:"Required"` Title string `form:"title" binding:"Required"`
Content string `form:"content" binding:"Required"` Content string `form:"content" binding:"Required"`
Draft string `form:"draft"`
Prerelease bool `form:"prerelease"` Prerelease bool `form:"prerelease"`
} }
@ -225,3 +226,25 @@ func (f *NewReleaseForm) Validate(errors *binding.Errors, req *http.Request, con
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
validate(errors, data, f) validate(errors, data, f)
} }
type EditReleaseForm struct {
Target string `form:"tag_target" binding:"Required"`
Title string `form:"title" binding:"Required"`
Content string `form:"content" binding:"Required"`
Draft string `form:"draft"`
Prerelease bool `form:"prerelease"`
}
func (f *EditReleaseForm) Name(field string) string {
names := map[string]string{
"Target": "Target",
"Title": "Release title",
"Content": "Release content",
}
return names[field]
}
func (f *EditReleaseForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
validate(errors, data, f)
}

View File

@ -21,21 +21,17 @@ import (
func RepoAssignment(redirect bool, args ...bool) martini.Handler { func RepoAssignment(redirect bool, args ...bool) martini.Handler {
return func(ctx *Context, params martini.Params) { return func(ctx *Context, params martini.Params) {
log.Trace(fmt.Sprint(args))
// valid brachname // valid brachname
var validBranch bool var validBranch bool
// display bare quick start if it is a bare repo // display bare quick start if it is a bare repo
var displayBare bool var displayBare bool
if len(args) >= 1 { if len(args) >= 1 {
// Note: argument has wrong value in Go1.3 martini. validBranch = args[0]
// validBranch = args[0]
validBranch = true
} }
if len(args) >= 2 { if len(args) >= 2 {
// displayBare = args[1] displayBare = args[1]
displayBare = true
} }
var ( var (

View File

@ -5,7 +5,7 @@
package repo package repo
import ( import (
"sort" "github.com/go-martini/martini"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
@ -14,27 +14,6 @@ import (
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
) )
type ReleaseSorter struct {
rels []*models.Release
}
func (rs *ReleaseSorter) Len() int {
return len(rs.rels)
}
func (rs *ReleaseSorter) Less(i, j int) bool {
diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
if diffNum != 0 {
return diffNum > 0
}
return rs.rels[i].Created.Second() > rs.rels[j].Created.Second()
}
func (rs *ReleaseSorter) Swap(i, j int) {
rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
}
func Releases(ctx *middleware.Context) { func Releases(ctx *middleware.Context) {
ctx.Data["Title"] = "Releases" ctx.Data["Title"] = "Releases"
ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoToolbarReleases"] = true
@ -57,53 +36,76 @@ func Releases(ctx *middleware.Context) {
return return
} }
var tags ReleaseSorter // Temproray cache commits count of used branches to speed up.
tags.rels = make([]*models.Release, len(rawTags)) countCache := make(map[string]int)
tags := make([]*models.Release, len(rawTags))
for i, rawTag := range rawTags { for i, rawTag := range rawTags {
for _, rel := range rels { for _, rel := range rels {
if rel.IsDraft && !ctx.Repo.IsOwner {
continue
}
if rel.TagName == rawTag { if rel.TagName == rawTag {
rel.Publisher, err = models.GetUserById(rel.PublisherId) rel.Publisher, err = models.GetUserById(rel.PublisherId)
if err != nil { if err != nil {
ctx.Handle(500, "release.Releases(GetUserById)", err) ctx.Handle(500, "release.Releases(GetUserById)", err)
return return
} }
rel.NumCommitsBehind = commitsCount - rel.NumCommits // Get corresponding target if it's not the current branch.
rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) if ctx.Repo.BranchName != rel.Target {
tags.rels[i] = rel // Get count if not exists.
break if _, ok := countCache[rel.Target]; !ok {
} commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName)
}
if tags.rels[i] == nil {
commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
if err != nil { if err != nil {
ctx.Handle(500, "release.Releases(GetCommitOfTag)", err) ctx.Handle(500, "release.Releases(GetCommitOfTag)", err)
return return
} }
countCache[rel.Target], err = commit.CommitsCount()
if err != nil {
ctx.Handle(500, "release.Releases(CommitsCount2)", err)
return
}
}
rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits
} else {
rel.NumCommitsBehind = commitsCount - rel.NumCommits
}
tags.rels[i] = &models.Release{ rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
tags[i] = rel
break
}
}
if tags[i] == nil {
commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
if err != nil {
ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err)
return
}
tags[i] = &models.Release{
Title: rawTag, Title: rawTag,
TagName: rawTag, TagName: rawTag,
Sha1: commit.Id.String(), Sha1: commit.Id.String(),
} }
tags.rels[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
if err != nil { if err != nil {
ctx.Handle(500, "release.Releases(CommitsCount)", err) ctx.Handle(500, "release.Releases(CommitsCount)", err)
return return
} }
tags.rels[i].NumCommitsBehind = commitsCount - tags.rels[i].NumCommits tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits
} }
} }
models.SortReleases(tags)
sort.Sort(&tags) ctx.Data["Releases"] = tags
ctx.Data["Releases"] = tags.rels
ctx.HTML(200, "release/list") ctx.HTML(200, "release/list")
} }
func ReleasesNew(ctx *middleware.Context) { func NewRelease(ctx *middleware.Context) {
if !ctx.Repo.IsOwner { if !ctx.Repo.IsOwner {
ctx.Handle(404, "release.ReleasesNew", nil) ctx.Handle(403, "release.ReleasesNew", nil)
return return
} }
@ -113,9 +115,9 @@ func ReleasesNew(ctx *middleware.Context) {
ctx.HTML(200, "release/new") ctx.HTML(200, "release/new")
} }
func ReleasesNewPost(ctx *middleware.Context, form auth.NewReleaseForm) { func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
if !ctx.Repo.IsOwner { if !ctx.Repo.IsOwner {
ctx.Handle(404, "release.ReleasesNew", nil) ctx.Handle(403, "release.ReleasesNew", nil)
return return
} }
@ -148,6 +150,7 @@ func ReleasesNewPost(ctx *middleware.Context, form auth.NewReleaseForm) {
Sha1: ctx.Repo.Commit.Id.String(), Sha1: ctx.Repo.Commit.Id.String(),
NumCommits: commitsCount, NumCommits: commitsCount,
Note: form.Content, Note: form.Content,
IsDraft: len(form.Draft) > 0,
IsPrerelease: form.Prerelease, IsPrerelease: form.Prerelease,
} }
@ -163,3 +166,58 @@ func ReleasesNewPost(ctx *middleware.Context, form auth.NewReleaseForm) {
ctx.Redirect(ctx.Repo.RepoLink + "/releases") ctx.Redirect(ctx.Repo.RepoLink + "/releases")
} }
func EditRelease(ctx *middleware.Context, params martini.Params) {
if !ctx.Repo.IsOwner {
ctx.Handle(403, "release.ReleasesEdit", nil)
return
}
tagName := params["tagname"]
rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
if err != nil {
if err == models.ErrReleaseNotExist {
ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err)
} else {
ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err)
}
return
}
ctx.Data["Release"] = rel
ctx.Data["Title"] = "Edit Release"
ctx.Data["IsRepoToolbarReleases"] = true
ctx.HTML(200, "release/edit")
}
func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) {
if !ctx.Repo.IsOwner {
ctx.Handle(403, "release.EditReleasePost", nil)
return
}
tagName := params["tagname"]
rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
if err != nil {
if err == models.ErrReleaseNotExist {
ctx.Handle(404, "release.EditReleasePost(GetRelease)", err)
} else {
ctx.Handle(500, "release.EditReleasePost(GetRelease)", err)
}
return
}
ctx.Data["Release"] = rel
ctx.Data["Title"] = "Edit Release"
ctx.Data["IsRepoToolbarReleases"] = true
rel.Title = form.Title
rel.Note = form.Content
rel.IsDraft = len(form.Draft) > 0
rel.IsPrerelease = form.Prerelease
if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err)
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/releases")
}

View File

@ -1 +1 @@
0.4.3.0612 Alpha 0.4.4.0612 Alpha

View File

@ -0,0 +1,70 @@
{{template "base/head" .}}
{{template "base/navbar" .}}
{{template "repo/nav" .}}
{{template "repo/toolbar" .}}
<div id="body" class="container">
<div id="release">
<h4 id="release-head">Edit Release</h4>
{{template "base/alert" .}}
<form id="release-new-form" action="{{.RepoLink}}/releases/edit/{{.Release.TagName}}" method="post" class="form form-inline">
{{.CsrfTokenHtml}}
<div class="form-group">
<b>{{.Release.TagName}}</b>
<span class="target-at">@</span>
<div class="btn-group" id="release-new-target-select">
<button type="button" class="btn btn-default"><i class="fa fa-code-fork fa-lg fa-m"></i>
<span class="target-text">Target : </span>
<strong id="release-new-target-name"> {{.Release.Target}}</strong>
</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<div class="dropdown-menu clone-group-btn" id="release-new-target-branch-list">
<ul class="list-group">
{{range .Branches}}
<li class="list-group-item">
<a href="#" rel="{{.}}"><i class="fa fa-code-fork"></i>{{.}}</a>
</li>
{{end}}
</ul>
</div>
<input id="tag-target" type="hidden" name="tag_target" value="{{.Release.Target}}"/>
</div>
<p class="help-block">Choose an existing tag, or create a new tag on publish</p>
</div>
<div class="form-group" style="display: block">
<input class="form-control input-lg" id="release-new-title" name="title" type="text" placeholder="release title" value="{{.Release.Title}}" />
</div>
<div class="form-group col-md-8" style="display: block" id="release-new-content-div">
<div class="md-help pull-right">
Content with <a href="https://help.github.com/articles/markdown-basics">Markdown</a>
</div>
<ul class="nav nav-tabs" data-init="tabs">
<li class="release-write active"><a href="#release-textarea" data-toggle="tab">Write</a></li>
<li class="release-preview"><a href="#release-preview" data-toggle="tab" data-ajax="/api/v1/markdown" data-ajax-name="release-preview" data-ajax-context="{{.RepoLink}}" data-ajax-method="post" data-preview="#release-preview">Preview</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="release-textarea">
<div class="form-group">
<textarea class="form-control" name="content" id="release-new-content" rows="10" placeholder="Write some content" data-ajax-rel="release-preview" data-ajax-val="val" data-ajax-field="text">{{.Release.Note}}</textarea>
</div>
</div>
<div class="tab-pane release-preview-content" id="release-preview">loading...</div>
</div>
</div>
<div class="text-right form-group col-md-8" style="display: block">
<hr/>
<label for="release-new-pre-release">
<input id="release-new-pre-release" type="checkbox" name="prerelease" {{if .Release.IsPrerelease}}checked{{end}}/>
<strong>This is a pre-release</strong>
</label>
<p class="help-block">Well point out that this release is identified as non-production ready.</p>
</div>
<div class="text-right form-group col-md-8" style="display: block">
<button class="btn-success btn">Publish release</button>
<input class="btn btn-default" type="submit" name="draft" value="Save draft"/>
</div>
</form>
</div>
</div>
{{template "base/footer" .}}

View File

@ -14,17 +14,23 @@
<li class="release-item clearfix" id="release-{{.Sha1}}"> <li class="release-item clearfix" id="release-{{.Sha1}}">
{{if .PublisherId}} {{if .PublisherId}}
<div class="col-md-2 text-right"> <div class="col-md-2 text-right">
{{if .IsPrerelease}}<span class="btn btn-warning status pre-release">Pre-Release</span>{{else}}<span class="btn btn-success status stable">Stable</span>{{end}} {{if .IsDraft}}
<span class="btn btn-primary status pre-release">Draft</span>
{{else if .IsPrerelease}}
<span class="btn btn-warning status pre-release">Pre-Release</span>
{{else}}
<span class="btn btn-success status stable">Stable</span>
{{end}}
<a class="tag" href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow"><i class="fa fa-tag"></i>{{.TagName}}</a> <a class="tag" href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow"><i class="fa fa-tag"></i>{{.TagName}}</a>
<a class="commit" href="{{$.RepoLink}}/src/{{.Sha1}}" rel="nofollow"><i class="fa fa-code"></i>{{ShortSha .Sha1}}</a> <a class="commit" href="{{$.RepoLink}}/src/{{.Sha1}}" rel="nofollow"><i class="fa fa-code"></i>{{ShortSha .Sha1}}</a>
</div> </div>
<div class="col-md-10"> <div class="col-md-10">
<h4 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}">{{.Title}}</a></h4> <h4 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}">{{.Title}}</a> <small>(<a href="{{$.RepoLink}}/releases/edit/{{.TagName}}" rel="nofollow">edit</a>)</small></h4>
<p class="info"> <p class="info">
<span class="author"><img class="avatar" src="{{.Publisher.AvatarLink}}" alt="" width="20">&nbsp;&nbsp; <span class="author"><img class="avatar" src="{{.Publisher.AvatarLink}}" alt="" width="20">&nbsp;&nbsp;
<a href="/user/{{.Publisher.Name}}">{{.Publisher.Name}}</a></span> <a href="/user/{{.Publisher.Name}}">{{.Publisher.Name}}</a></span>
{{if .Created}}<span class="time">{{TimeSince .Created}}</span>{{end}} {{if .Created}}<span class="time">{{TimeSince .Created}}</span>{{end}}
<span class="ahead"><strong>{{.NumCommitsBehind}}</strong> commits since this release</span> <span class="ahead"><strong>{{.NumCommitsBehind}}</strong> commits to {{.Target}} since this release</span>
</p> </p>
<div class="markdown desc"> <div class="markdown desc">
{{str2html .Note}} {{str2html .Note}}

View File

@ -62,7 +62,7 @@
</div> </div>
<div class="text-right form-group col-md-8" style="display: block"> <div class="text-right form-group col-md-8" style="display: block">
<button class="btn-success btn">Publish release</button> <button class="btn-success btn">Publish release</button>
<!-- <input class="btn btn-default" type="submit" name="draft" value="Save Draft"/> --> <input class="btn btn-default" type="submit" name="draft" value="Save draft"/>
</div> </div>
</form> </form>
</div> </div>