release.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "fmt"
  7. "strings"
  8. "github.com/gogs/git-module"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/db"
  13. "gogs.io/gogs/internal/form"
  14. "gogs.io/gogs/internal/gitutil"
  15. "gogs.io/gogs/internal/markup"
  16. )
  17. const (
  18. RELEASES = "repo/release/list"
  19. RELEASE_NEW = "repo/release/new"
  20. )
  21. // calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
  22. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *db.Release, countCache map[string]int64) error {
  23. // Get count if not exists
  24. if _, ok := countCache[release.Target]; !ok {
  25. if repoCtx.GitRepo.HasBranch(release.Target) {
  26. commit, err := repoCtx.GitRepo.BranchCommit(release.Target)
  27. if err != nil {
  28. return fmt.Errorf("get branch commit: %v", err)
  29. }
  30. countCache[release.Target], err = commit.CommitsCount()
  31. if err != nil {
  32. return fmt.Errorf("count commits: %v", err)
  33. }
  34. } else {
  35. // Use NumCommits of the newest release on that target
  36. countCache[release.Target] = release.NumCommits
  37. }
  38. }
  39. release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
  40. return nil
  41. }
  42. func Releases(c *context.Context) {
  43. c.Data["Title"] = c.Tr("repo.release.releases")
  44. c.Data["PageIsViewFiles"] = true
  45. c.Data["PageIsReleaseList"] = true
  46. tagsPage, err := gitutil.Module.ListTagsAfter(c.Repo.GitRepo.Path(), c.Query("after"), 10)
  47. if err != nil {
  48. c.Error(err, "get tags")
  49. return
  50. }
  51. releases, err := db.GetPublishedReleasesByRepoID(c.Repo.Repository.ID, tagsPage.Tags...)
  52. if err != nil {
  53. c.Error(err, "get published releases by repository ID")
  54. return
  55. }
  56. // Temporary cache commits count of used branches to speed up.
  57. countCache := make(map[string]int64)
  58. results := make([]*db.Release, len(tagsPage.Tags))
  59. for i, rawTag := range tagsPage.Tags {
  60. for j, r := range releases {
  61. if r == nil || r.TagName != rawTag {
  62. continue
  63. }
  64. releases[j] = nil // Mark as used.
  65. if err = r.LoadAttributes(); err != nil {
  66. c.Error(err, "load attributes")
  67. return
  68. }
  69. if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
  70. c.Error(err, "calculate number of commits after release")
  71. return
  72. }
  73. r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
  74. results[i] = r
  75. break
  76. }
  77. // No published release matches this tag
  78. if results[i] == nil {
  79. commit, err := c.Repo.GitRepo.TagCommit(rawTag)
  80. if err != nil {
  81. c.Error(err, "get tag commit")
  82. return
  83. }
  84. results[i] = &db.Release{
  85. Title: rawTag,
  86. TagName: rawTag,
  87. Sha1: commit.ID.String(),
  88. }
  89. results[i].NumCommits, err = commit.CommitsCount()
  90. if err != nil {
  91. c.Error(err, "count commits")
  92. return
  93. }
  94. results[i].NumCommitsBehind = c.Repo.CommitsCount - results[i].NumCommits
  95. }
  96. }
  97. db.SortReleases(results)
  98. // Only show drafts if user is viewing the latest page
  99. var drafts []*db.Release
  100. if tagsPage.HasLatest {
  101. drafts, err = db.GetDraftReleasesByRepoID(c.Repo.Repository.ID)
  102. if err != nil {
  103. c.Error(err, "get draft releases by repository ID")
  104. return
  105. }
  106. for _, r := range drafts {
  107. if err = r.LoadAttributes(); err != nil {
  108. c.Error(err, "load attributes")
  109. return
  110. }
  111. if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
  112. c.Error(err, "calculate number of commits after release")
  113. return
  114. }
  115. r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
  116. }
  117. if len(drafts) > 0 {
  118. results = append(drafts, results...)
  119. }
  120. }
  121. c.Data["Releases"] = results
  122. c.Data["HasPrevious"] = !tagsPage.HasLatest
  123. c.Data["ReachEnd"] = !tagsPage.HasNext
  124. c.Data["PreviousAfter"] = tagsPage.PreviousAfter
  125. if len(results) > 0 {
  126. c.Data["NextAfter"] = results[len(results)-1].TagName
  127. }
  128. c.Success(RELEASES)
  129. }
  130. func renderReleaseAttachmentSettings(c *context.Context) {
  131. c.Data["RequireDropzone"] = true
  132. c.Data["IsAttachmentEnabled"] = conf.Release.Attachment.Enabled
  133. c.Data["AttachmentAllowedTypes"] = strings.Join(conf.Release.Attachment.AllowedTypes, ",")
  134. c.Data["AttachmentMaxSize"] = conf.Release.Attachment.MaxSize
  135. c.Data["AttachmentMaxFiles"] = conf.Release.Attachment.MaxFiles
  136. }
  137. func NewRelease(c *context.Context) {
  138. c.Data["Title"] = c.Tr("repo.release.new_release")
  139. c.Data["PageIsReleaseList"] = true
  140. c.Data["tag_target"] = c.Repo.Repository.DefaultBranch
  141. renderReleaseAttachmentSettings(c)
  142. c.Success(RELEASE_NEW)
  143. }
  144. func NewReleasePost(c *context.Context, f form.NewRelease) {
  145. c.Data["Title"] = c.Tr("repo.release.new_release")
  146. c.Data["PageIsReleaseList"] = true
  147. renderReleaseAttachmentSettings(c)
  148. if c.HasError() {
  149. c.Success(RELEASE_NEW)
  150. return
  151. }
  152. if !c.Repo.GitRepo.HasBranch(f.Target) {
  153. c.RenderWithErr(c.Tr("form.target_branch_not_exist"), RELEASE_NEW, &f)
  154. return
  155. }
  156. // Use current time if tag not yet exist, otherwise get time from Git
  157. var tagCreatedUnix int64
  158. tag, err := c.Repo.GitRepo.Tag(git.RefsTags + f.TagName)
  159. if err == nil {
  160. commit, err := tag.Commit()
  161. if err == nil {
  162. tagCreatedUnix = commit.Author.When.Unix()
  163. }
  164. }
  165. commit, err := c.Repo.GitRepo.BranchCommit(f.Target)
  166. if err != nil {
  167. c.Error(err, "get branch commit")
  168. return
  169. }
  170. commitsCount, err := commit.CommitsCount()
  171. if err != nil {
  172. c.Error(err, "count commits")
  173. return
  174. }
  175. var attachments []string
  176. if conf.Release.Attachment.Enabled {
  177. attachments = f.Files
  178. }
  179. rel := &db.Release{
  180. RepoID: c.Repo.Repository.ID,
  181. PublisherID: c.User.ID,
  182. Title: f.Title,
  183. TagName: f.TagName,
  184. Target: f.Target,
  185. Sha1: commit.ID.String(),
  186. NumCommits: commitsCount,
  187. Note: f.Content,
  188. IsDraft: len(f.Draft) > 0,
  189. IsPrerelease: f.Prerelease,
  190. CreatedUnix: tagCreatedUnix,
  191. }
  192. if err = db.NewRelease(c.Repo.GitRepo, rel, attachments); err != nil {
  193. c.Data["Err_TagName"] = true
  194. switch {
  195. case db.IsErrReleaseAlreadyExist(err):
  196. c.RenderWithErr(c.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &f)
  197. case db.IsErrInvalidTagName(err):
  198. c.RenderWithErr(c.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f)
  199. default:
  200. c.Error(err, "new release")
  201. }
  202. return
  203. }
  204. log.Trace("Release created: %s/%s:%s", c.User.LowerName, c.Repo.Repository.Name, f.TagName)
  205. c.Redirect(c.Repo.RepoLink + "/releases")
  206. }
  207. func EditRelease(c *context.Context) {
  208. c.Data["Title"] = c.Tr("repo.release.edit_release")
  209. c.Data["PageIsReleaseList"] = true
  210. c.Data["PageIsEditRelease"] = true
  211. renderReleaseAttachmentSettings(c)
  212. tagName := c.Params("*")
  213. rel, err := db.GetRelease(c.Repo.Repository.ID, tagName)
  214. if err != nil {
  215. c.NotFoundOrError(err, "get release")
  216. return
  217. }
  218. c.Data["ID"] = rel.ID
  219. c.Data["tag_name"] = rel.TagName
  220. c.Data["tag_target"] = rel.Target
  221. c.Data["title"] = rel.Title
  222. c.Data["content"] = rel.Note
  223. c.Data["attachments"] = rel.Attachments
  224. c.Data["prerelease"] = rel.IsPrerelease
  225. c.Data["IsDraft"] = rel.IsDraft
  226. c.Success(RELEASE_NEW)
  227. }
  228. func EditReleasePost(c *context.Context, f form.EditRelease) {
  229. c.Data["Title"] = c.Tr("repo.release.edit_release")
  230. c.Data["PageIsReleaseList"] = true
  231. c.Data["PageIsEditRelease"] = true
  232. renderReleaseAttachmentSettings(c)
  233. tagName := c.Params("*")
  234. rel, err := db.GetRelease(c.Repo.Repository.ID, tagName)
  235. if err != nil {
  236. c.NotFoundOrError(err, "get release")
  237. return
  238. }
  239. c.Data["tag_name"] = rel.TagName
  240. c.Data["tag_target"] = rel.Target
  241. c.Data["title"] = rel.Title
  242. c.Data["content"] = rel.Note
  243. c.Data["attachments"] = rel.Attachments
  244. c.Data["prerelease"] = rel.IsPrerelease
  245. c.Data["IsDraft"] = rel.IsDraft
  246. if c.HasError() {
  247. c.Success(RELEASE_NEW)
  248. return
  249. }
  250. var attachments []string
  251. if conf.Release.Attachment.Enabled {
  252. attachments = f.Files
  253. }
  254. isPublish := rel.IsDraft && len(f.Draft) == 0
  255. rel.Title = f.Title
  256. rel.Note = f.Content
  257. rel.IsDraft = len(f.Draft) > 0
  258. rel.IsPrerelease = f.Prerelease
  259. if err = db.UpdateRelease(c.User, c.Repo.GitRepo, rel, isPublish, attachments); err != nil {
  260. c.Error(err, "update release")
  261. return
  262. }
  263. c.Redirect(c.Repo.RepoLink + "/releases")
  264. }
  265. func UploadReleaseAttachment(c *context.Context) {
  266. if !conf.Release.Attachment.Enabled {
  267. c.NotFound()
  268. return
  269. }
  270. uploadAttachment(c, conf.Release.Attachment.AllowedTypes)
  271. }
  272. func DeleteRelease(c *context.Context) {
  273. if err := db.DeleteReleaseOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
  274. c.Flash.Error("DeleteReleaseByID: " + err.Error())
  275. } else {
  276. c.Flash.Success(c.Tr("repo.release.deletion_success"))
  277. }
  278. c.JSONSuccess(map[string]interface{}{
  279. "redirect": c.Repo.RepoLink + "/releases",
  280. })
  281. }