From f644cefa865c04b440902695ba90114b224c640d Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 14 Apr 2014 01:57:25 -0400 Subject: [PATCH] Finish release --- models/models.go | 2 +- models/release.go | 83 ++++++++++++++++++++++ modules/auth/release.go | 50 ++++++++++++++ modules/base/markdown.go | 4 ++ routers/repo/release.go | 133 +++++++++++++++++++++++++++++++++++- templates/release/list.tmpl | 66 ++++++++---------- templates/release/new.tmpl | 23 ++++--- web.go | 10 ++- 8 files changed, 318 insertions(+), 53 deletions(-) create mode 100644 models/release.go create mode 100644 modules/auth/release.go diff --git a/models/models.go b/models/models.go index f1d43531b..0e20a1ab2 100644 --- a/models/models.go +++ b/models/models.go @@ -33,7 +33,7 @@ var ( func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), - new(Mirror)) + new(Mirror), new(Release)) } func LoadModelsConfig() { diff --git a/models/release.go b/models/release.go new file mode 100644 index 000000000..08d74d3f4 --- /dev/null +++ b/models/release.go @@ -0,0 +1,83 @@ +// 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 models + +import ( + "errors" + "strings" + "time" + + "github.com/Unknwon/com" + "github.com/gogits/git" +) + +var ( + ErrReleaseAlreadyExist = errors.New("Release already exist") +) + +// Release represents a release of repository. +type Release struct { + Id int64 + RepoId int64 + PublisherId int64 + Publisher *User `xorm:"-"` + Title string + TagName string + LowerTagName string + SHA1 string + NumCommits int + NumCommitsBehind int `xorm:"-"` + Note string `xorm:"TEXT"` + IsPrerelease bool + 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. +func IsReleaseExist(repoId int64, tagName string) (bool, error) { + if len(tagName) == 0 { + return false, nil + } + + return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)}) +} + +// CreateRelease creates a new release of repository. +func CreateRelease(repoPath string, rel *Release, gitRepo *git.Repository) error { + isExist, err := IsReleaseExist(rel.RepoId, rel.TagName) + if err != nil { + return err + } else if isExist { + return ErrReleaseAlreadyExist + } + + if !git.IsTagExist(repoPath, rel.TagName) { + _, stderr, err := com.ExecCmdDir(repoPath, "git", "tag", rel.TagName, "-m", "\""+rel.Title+"\"") + if err != nil { + return err + } else if strings.Contains(stderr, "fatal:") { + return errors.New(stderr) + } + } else { + commit, err := gitRepo.GetCommitOfTag(rel.TagName) + if err != nil { + return err + } + + rel.NumCommits, err = commit.CommitsCount() + if err != nil { + return err + } + } + + rel.LowerTagName = strings.ToLower(rel.TagName) + _, err = orm.InsertOne(rel) + return err +} diff --git a/modules/auth/release.go b/modules/auth/release.go new file mode 100644 index 000000000..a29028e0e --- /dev/null +++ b/modules/auth/release.go @@ -0,0 +1,50 @@ +// 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/go-martini/martini" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" +) + +type NewReleaseForm struct { + TagName string `form:"tag_name" binding:"Required"` + Title string `form:"title" binding:"Required"` + Content string `form:"content" binding:"Required"` + Prerelease bool `form:"prerelease"` +} + +func (f *NewReleaseForm) Name(field string) string { + names := map[string]string{ + "TagName": "Tag name", + "Title": "Release title", + "Content": "Release content", + } + return names[field] +} + +func (f *NewReleaseForm) Validate(errors *base.BindingErrors, 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("NewReleaseForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/modules/base/markdown.go b/modules/base/markdown.go index cc1807750..95b4b212f 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -166,3 +166,7 @@ func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { // fmt.Println(string(body)) return body } + +func RenderMarkdownString(raw, urlPrefix string) string { + return string(RenderMarkdown([]byte(raw), urlPrefix)) +} diff --git a/routers/repo/release.go b/routers/repo/release.go index 3d549bf77..a4baa4792 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -5,25 +5,152 @@ package repo import ( + "sort" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" "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 { + return rs.rels[i].NumCommits > rs.rels[j].NumCommits +} + +func (rs *ReleaseSorter) Swap(i, j int) { + rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i] +} + func Releases(ctx *middleware.Context) { ctx.Data["Title"] = "Releases" ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoReleaseNew"] = false - tags, err := ctx.Repo.GitRepo.GetTags() + rawTags, err := ctx.Repo.GitRepo.GetTags() if err != nil { - ctx.Handle(404, "repo.Releases(GetTags)", err) + ctx.Handle(500, "release.Releases(GetTags)", err) return } - ctx.Data["Releases"] = tags + + rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id) + if err != nil { + ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err) + return + } + + commitsCount, err := ctx.Repo.Commit.CommitsCount() + if err != nil { + ctx.Handle(500, "release.Releases(CommitsCount)", err) + return + } + + var tags ReleaseSorter + tags.rels = make([]*models.Release, len(rawTags)) + for i, rawTag := range rawTags { + for _, rel := range rels { + if rel.TagName == rawTag { + rel.Publisher, err = models.GetUserById(rel.PublisherId) + if err != nil { + ctx.Handle(500, "release.Releases(GetUserById)", err) + return + } + rel.NumCommitsBehind = commitsCount - rel.NumCommits + rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) + tags.rels[i] = rel + break + } + } + + if tags.rels[i] == nil { + commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag) + if err != nil { + ctx.Handle(500, "release.Releases(GetCommitOfTag)", err) + return + } + + tags.rels[i] = &models.Release{ + Title: rawTag, + TagName: rawTag, + SHA1: commit.Id.String(), + } + tags.rels[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) + if err != nil { + ctx.Handle(500, "release.Releases(CommitsCount)", err) + return + } + tags.rels[i].NumCommitsBehind = commitsCount - tags.rels[i].NumCommits + tags.rels[i].Created = commit.Author.When + } + } + + sort.Sort(&tags) + + ctx.Data["Releases"] = tags.rels ctx.HTML(200, "release/list") } func ReleasesNew(ctx *middleware.Context) { + if !ctx.Repo.IsOwner { + ctx.Handle(404, "release.ReleasesNew", nil) + return + } + ctx.Data["Title"] = "New Release" ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoReleaseNew"] = true ctx.HTML(200, "release/new") } + +func ReleasesNewPost(ctx *middleware.Context, form auth.NewReleaseForm) { + if !ctx.Repo.IsOwner { + ctx.Handle(404, "release.ReleasesNew", nil) + return + } + + ctx.Data["Title"] = "New Release" + ctx.Data["IsRepoToolbarReleases"] = true + ctx.Data["IsRepoReleaseNew"] = true + + if ctx.HasError() { + ctx.HTML(200, "release/new") + return + } + + commitsCount, err := ctx.Repo.Commit.CommitsCount() + if err != nil { + ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err) + return + } + + rel := &models.Release{ + RepoId: ctx.Repo.Repository.Id, + PublisherId: ctx.User.Id, + Title: form.Title, + TagName: form.TagName, + SHA1: ctx.Repo.Commit.Id.String(), + NumCommits: commitsCount, + Note: form.Content, + IsPrerelease: form.Prerelease, + } + + if err = models.CreateRelease(models.RepoPath(ctx.User.Name, ctx.Repo.Repository.Name), + rel, ctx.Repo.GitRepo); err != nil { + if err == models.ErrReleaseAlreadyExist { + ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form) + } else { + ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err) + } + return + } + log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) + + ctx.Redirect(ctx.Repo.RepoLink + "/releases") +} diff --git a/templates/release/list.tmpl b/templates/release/list.tmpl index 9541265c9..dd37e9c1a 100644 --- a/templates/release/list.tmpl +++ b/templates/release/list.tmpl @@ -10,50 +10,47 @@ - {{range .Releases}} - {{.}} - {{end}} {{template "base/footer" .}} \ No newline at end of file diff --git a/templates/release/new.tmpl b/templates/release/new.tmpl index fe5aa179c..439496ea6 100644 --- a/templates/release/new.tmpl +++ b/templates/release/new.tmpl @@ -5,30 +5,34 @@

New Release

-
+ {{template "base/alert" .}} + + {{.CsrfTokenHtml}}
- + @
-

Choose an existing tag without release notes

+

Choose an existing tag, or create a new tag on publish

- +
@@ -41,7 +45,7 @@
- +
loading...
@@ -50,15 +54,14 @@

We’ll point out that this release is identified as non-production ready.

- - +
diff --git a/web.go b/web.go index 268d9e71d..04c41b9d2 100644 --- a/web.go +++ b/web.go @@ -63,7 +63,7 @@ func runWeb(*cli.Context) { SignInRequire: base.Service.RequireSignInView, DisableCsrf: true, }) - + reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true}) bindIgnErr := middleware.BindIgnErr @@ -153,13 +153,16 @@ func runWeb(*cli.Context) { r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost) r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) r.Post("/comment/:action", repo.Comment) + r.Get("/releases/new", repo.ReleasesNew) }, reqSignIn, middleware.RepoAssignment(true)) + m.Group("/:username/:reponame", func(r martini.Router) { + r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost) + }, reqSignIn, middleware.RepoAssignment(true, true)) + m.Group("/:username/:reponame", func(r martini.Router) { r.Get("/issues", repo.Issues) r.Get("/issues/:index", repo.ViewIssue) - r.Get("/releases", repo.Releases) - r.Any("/releases/new", repo.ReleasesNew) // TODO: r.Get("/pulls", repo.Pulls) r.Get("/branches", repo.Branches) }, ignSignIn, middleware.RepoAssignment(true)) @@ -172,6 +175,7 @@ func runWeb(*cli.Context) { r.Get("/commits/:branchname/search", repo.SearchCommits) r.Get("/commit/:branchname", repo.Diff) r.Get("/commit/:branchname/**", repo.Diff) + r.Get("/releases", repo.Releases) }, ignSignIn, middleware.RepoAssignment(true, true)) m.Group("/:username", func(r martini.Router) {