123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // 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 repo
- import (
- "fmt"
- "strings"
- log "unknwon.dev/clog/v2"
- "gogs.io/gogs/internal/context"
- "gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/form"
- "gogs.io/gogs/internal/markup"
- "gogs.io/gogs/internal/conf"
- )
- const (
- RELEASES = "repo/release/list"
- RELEASE_NEW = "repo/release/new"
- )
- // calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
- func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *db.Release, countCache map[string]int64) error {
- // Get count if not exists
- if _, ok := countCache[release.Target]; !ok {
- if repoCtx.GitRepo.IsBranchExist(release.Target) {
- commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
- if err != nil {
- return fmt.Errorf("GetBranchCommit: %v", err)
- }
- countCache[release.Target], err = commit.CommitsCount()
- if err != nil {
- return fmt.Errorf("CommitsCount: %v", err)
- }
- } else {
- // Use NumCommits of the newest release on that target
- countCache[release.Target] = release.NumCommits
- }
- }
- release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
- return nil
- }
- func Releases(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.release.releases")
- c.Data["PageIsViewFiles"] = true
- c.Data["PageIsReleaseList"] = true
- tagsResult, err := c.Repo.GitRepo.GetTagsAfter(c.Query("after"), 10)
- if err != nil {
- c.Handle(500, fmt.Sprintf("GetTags '%s'", c.Repo.Repository.RepoPath()), err)
- return
- }
- releases, err := db.GetPublishedReleasesByRepoID(c.Repo.Repository.ID, tagsResult.Tags...)
- if err != nil {
- c.Handle(500, "GetPublishedReleasesByRepoID", err)
- return
- }
- // Temproray cache commits count of used branches to speed up.
- countCache := make(map[string]int64)
- results := make([]*db.Release, len(tagsResult.Tags))
- for i, rawTag := range tagsResult.Tags {
- for j, r := range releases {
- if r == nil || r.TagName != rawTag {
- continue
- }
- releases[j] = nil // Mark as used.
- if err = r.LoadAttributes(); err != nil {
- c.Handle(500, "LoadAttributes", err)
- return
- }
- if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
- c.Handle(500, "calReleaseNumCommitsBehind", err)
- return
- }
- r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
- results[i] = r
- break
- }
- // No published release matches this tag
- if results[i] == nil {
- commit, err := c.Repo.GitRepo.GetTagCommit(rawTag)
- if err != nil {
- c.Handle(500, "GetTagCommit", err)
- return
- }
- results[i] = &db.Release{
- Title: rawTag,
- TagName: rawTag,
- Sha1: commit.ID.String(),
- }
- results[i].NumCommits, err = commit.CommitsCount()
- if err != nil {
- c.Handle(500, "CommitsCount", err)
- return
- }
- results[i].NumCommitsBehind = c.Repo.CommitsCount - results[i].NumCommits
- }
- }
- db.SortReleases(results)
- // Only show drafts if user is viewing the latest page
- var drafts []*db.Release
- if tagsResult.HasLatest {
- drafts, err = db.GetDraftReleasesByRepoID(c.Repo.Repository.ID)
- if err != nil {
- c.Handle(500, "GetDraftReleasesByRepoID", err)
- return
- }
- for _, r := range drafts {
- if err = r.LoadAttributes(); err != nil {
- c.Handle(500, "LoadAttributes", err)
- return
- }
- if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
- c.Handle(500, "calReleaseNumCommitsBehind", err)
- return
- }
- r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
- }
- if len(drafts) > 0 {
- results = append(drafts, results...)
- }
- }
- c.Data["Releases"] = results
- c.Data["HasPrevious"] = !tagsResult.HasLatest
- c.Data["ReachEnd"] = tagsResult.ReachEnd
- c.Data["PreviousAfter"] = tagsResult.PreviousAfter
- if len(results) > 0 {
- c.Data["NextAfter"] = results[len(results)-1].TagName
- }
- c.HTML(200, RELEASES)
- }
- func renderReleaseAttachmentSettings(c *context.Context) {
- c.Data["RequireDropzone"] = true
- c.Data["IsAttachmentEnabled"] = conf.Release.Attachment.Enabled
- c.Data["AttachmentAllowedTypes"] = strings.Join(conf.Release.Attachment.AllowedTypes, ",")
- c.Data["AttachmentMaxSize"] = conf.Release.Attachment.MaxSize
- c.Data["AttachmentMaxFiles"] = conf.Release.Attachment.MaxFiles
- }
- func NewRelease(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.release.new_release")
- c.Data["PageIsReleaseList"] = true
- c.Data["tag_target"] = c.Repo.Repository.DefaultBranch
- renderReleaseAttachmentSettings(c)
- c.HTML(200, RELEASE_NEW)
- }
- func NewReleasePost(c *context.Context, f form.NewRelease) {
- c.Data["Title"] = c.Tr("repo.release.new_release")
- c.Data["PageIsReleaseList"] = true
- renderReleaseAttachmentSettings(c)
- if c.HasError() {
- c.HTML(200, RELEASE_NEW)
- return
- }
- if !c.Repo.GitRepo.IsBranchExist(f.Target) {
- c.RenderWithErr(c.Tr("form.target_branch_not_exist"), RELEASE_NEW, &f)
- return
- }
- // Use current time if tag not yet exist, otherwise get time from Git
- var tagCreatedUnix int64
- tag, err := c.Repo.GitRepo.GetTag(f.TagName)
- if err == nil {
- commit, err := tag.Commit()
- if err == nil {
- tagCreatedUnix = commit.Author.When.Unix()
- }
- }
- commit, err := c.Repo.GitRepo.GetBranchCommit(f.Target)
- if err != nil {
- c.Handle(500, "GetBranchCommit", err)
- return
- }
- commitsCount, err := commit.CommitsCount()
- if err != nil {
- c.Handle(500, "CommitsCount", err)
- return
- }
- var attachments []string
- if conf.Release.Attachment.Enabled {
- attachments = f.Files
- }
- rel := &db.Release{
- RepoID: c.Repo.Repository.ID,
- PublisherID: c.User.ID,
- Title: f.Title,
- TagName: f.TagName,
- Target: f.Target,
- Sha1: commit.ID.String(),
- NumCommits: commitsCount,
- Note: f.Content,
- IsDraft: len(f.Draft) > 0,
- IsPrerelease: f.Prerelease,
- CreatedUnix: tagCreatedUnix,
- }
- if err = db.NewRelease(c.Repo.GitRepo, rel, attachments); err != nil {
- c.Data["Err_TagName"] = true
- switch {
- case db.IsErrReleaseAlreadyExist(err):
- c.RenderWithErr(c.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &f)
- case db.IsErrInvalidTagName(err):
- c.RenderWithErr(c.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f)
- default:
- c.Handle(500, "NewRelease", err)
- }
- return
- }
- log.Trace("Release created: %s/%s:%s", c.User.LowerName, c.Repo.Repository.Name, f.TagName)
- c.Redirect(c.Repo.RepoLink + "/releases")
- }
- func EditRelease(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.release.edit_release")
- c.Data["PageIsReleaseList"] = true
- c.Data["PageIsEditRelease"] = true
- renderReleaseAttachmentSettings(c)
- tagName := c.Params("*")
- rel, err := db.GetRelease(c.Repo.Repository.ID, tagName)
- if err != nil {
- if db.IsErrReleaseNotExist(err) {
- c.Handle(404, "GetRelease", err)
- } else {
- c.Handle(500, "GetRelease", err)
- }
- return
- }
- c.Data["ID"] = rel.ID
- c.Data["tag_name"] = rel.TagName
- c.Data["tag_target"] = rel.Target
- c.Data["title"] = rel.Title
- c.Data["content"] = rel.Note
- c.Data["attachments"] = rel.Attachments
- c.Data["prerelease"] = rel.IsPrerelease
- c.Data["IsDraft"] = rel.IsDraft
- c.HTML(200, RELEASE_NEW)
- }
- func EditReleasePost(c *context.Context, f form.EditRelease) {
- c.Data["Title"] = c.Tr("repo.release.edit_release")
- c.Data["PageIsReleaseList"] = true
- c.Data["PageIsEditRelease"] = true
- renderReleaseAttachmentSettings(c)
- tagName := c.Params("*")
- rel, err := db.GetRelease(c.Repo.Repository.ID, tagName)
- if err != nil {
- if db.IsErrReleaseNotExist(err) {
- c.Handle(404, "GetRelease", err)
- } else {
- c.Handle(500, "GetRelease", err)
- }
- return
- }
- c.Data["tag_name"] = rel.TagName
- c.Data["tag_target"] = rel.Target
- c.Data["title"] = rel.Title
- c.Data["content"] = rel.Note
- c.Data["attachments"] = rel.Attachments
- c.Data["prerelease"] = rel.IsPrerelease
- c.Data["IsDraft"] = rel.IsDraft
- if c.HasError() {
- c.HTML(200, RELEASE_NEW)
- return
- }
- var attachments []string
- if conf.Release.Attachment.Enabled {
- attachments = f.Files
- }
- isPublish := rel.IsDraft && len(f.Draft) == 0
- rel.Title = f.Title
- rel.Note = f.Content
- rel.IsDraft = len(f.Draft) > 0
- rel.IsPrerelease = f.Prerelease
- if err = db.UpdateRelease(c.User, c.Repo.GitRepo, rel, isPublish, attachments); err != nil {
- c.Handle(500, "UpdateRelease", err)
- return
- }
- c.Redirect(c.Repo.RepoLink + "/releases")
- }
- func UploadReleaseAttachment(c *context.Context) {
- if !conf.Release.Attachment.Enabled {
- c.NotFound()
- return
- }
- uploadAttachment(c, conf.Release.Attachment.AllowedTypes)
- }
- func DeleteRelease(c *context.Context) {
- if err := db.DeleteReleaseOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
- c.Flash.Error("DeleteReleaseByID: " + err.Error())
- } else {
- c.Flash.Success(c.Tr("repo.release.deletion_success"))
- }
- c.JSON(200, map[string]interface{}{
- "redirect": c.Repo.RepoLink + "/releases",
- })
- }
|