release.go 8.9 KB

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