release.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.ServerError("get tags", err)
  49. return
  50. }
  51. releases, err := db.GetPublishedReleasesByRepoID(c.Repo.Repository.ID, tagsPage.Tags...)
  52. if err != nil {
  53. c.Handle(500, "GetPublishedReleasesByRepoID", err)
  54. return
  55. }
  56. // Temproray 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.Handle(500, "LoadAttributes", err)
  67. return
  68. }
  69. if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
  70. c.Handle(500, "calReleaseNumCommitsBehind", err)
  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.Handle(500, "get tag commit", err)
  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.ServerError("count commits", err)
  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.Handle(500, "GetDraftReleasesByRepoID", err)
  104. return
  105. }
  106. for _, r := range drafts {
  107. if err = r.LoadAttributes(); err != nil {
  108. c.Handle(500, "LoadAttributes", err)
  109. return
  110. }
  111. if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
  112. c.Handle(500, "calReleaseNumCommitsBehind", err)
  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.HTML(200, 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.HTML(200, 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.HTML(200, 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.ServerError("get branch commit", err)
  168. return
  169. }
  170. commitsCount, err := commit.CommitsCount()
  171. if err != nil {
  172. c.ServerError("count commits", err)
  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.Handle(500, "NewRelease", err)
  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. if db.IsErrReleaseNotExist(err) {
  216. c.Handle(404, "GetRelease", err)
  217. } else {
  218. c.Handle(500, "GetRelease", err)
  219. }
  220. return
  221. }
  222. c.Data["ID"] = rel.ID
  223. c.Data["tag_name"] = rel.TagName
  224. c.Data["tag_target"] = rel.Target
  225. c.Data["title"] = rel.Title
  226. c.Data["content"] = rel.Note
  227. c.Data["attachments"] = rel.Attachments
  228. c.Data["prerelease"] = rel.IsPrerelease
  229. c.Data["IsDraft"] = rel.IsDraft
  230. c.HTML(200, RELEASE_NEW)
  231. }
  232. func EditReleasePost(c *context.Context, f form.EditRelease) {
  233. c.Data["Title"] = c.Tr("repo.release.edit_release")
  234. c.Data["PageIsReleaseList"] = true
  235. c.Data["PageIsEditRelease"] = true
  236. renderReleaseAttachmentSettings(c)
  237. tagName := c.Params("*")
  238. rel, err := db.GetRelease(c.Repo.Repository.ID, tagName)
  239. if err != nil {
  240. if db.IsErrReleaseNotExist(err) {
  241. c.Handle(404, "GetRelease", err)
  242. } else {
  243. c.Handle(500, "GetRelease", err)
  244. }
  245. return
  246. }
  247. c.Data["tag_name"] = rel.TagName
  248. c.Data["tag_target"] = rel.Target
  249. c.Data["title"] = rel.Title
  250. c.Data["content"] = rel.Note
  251. c.Data["attachments"] = rel.Attachments
  252. c.Data["prerelease"] = rel.IsPrerelease
  253. c.Data["IsDraft"] = rel.IsDraft
  254. if c.HasError() {
  255. c.HTML(200, RELEASE_NEW)
  256. return
  257. }
  258. var attachments []string
  259. if conf.Release.Attachment.Enabled {
  260. attachments = f.Files
  261. }
  262. isPublish := rel.IsDraft && len(f.Draft) == 0
  263. rel.Title = f.Title
  264. rel.Note = f.Content
  265. rel.IsDraft = len(f.Draft) > 0
  266. rel.IsPrerelease = f.Prerelease
  267. if err = db.UpdateRelease(c.User, c.Repo.GitRepo, rel, isPublish, attachments); err != nil {
  268. c.Handle(500, "UpdateRelease", err)
  269. return
  270. }
  271. c.Redirect(c.Repo.RepoLink + "/releases")
  272. }
  273. func UploadReleaseAttachment(c *context.Context) {
  274. if !conf.Release.Attachment.Enabled {
  275. c.NotFound()
  276. return
  277. }
  278. uploadAttachment(c, conf.Release.Attachment.AllowedTypes)
  279. }
  280. func DeleteRelease(c *context.Context) {
  281. if err := db.DeleteReleaseOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
  282. c.Flash.Error("DeleteReleaseByID: " + err.Error())
  283. } else {
  284. c.Flash.Success(c.Tr("repo.release.deletion_success"))
  285. }
  286. c.JSON(200, map[string]interface{}{
  287. "redirect": c.Repo.RepoLink + "/releases",
  288. })
  289. }