release.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/gogits/gogs/models"
  10. "github.com/gogits/gogs/pkg/context"
  11. "github.com/gogits/gogs/pkg/form"
  12. "github.com/gogits/gogs/pkg/markup"
  13. "github.com/gogits/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(ctx *context.Context) {
  41. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  42. ctx.Data["PageIsViewFiles"] = true
  43. ctx.Data["PageIsReleaseList"] = true
  44. tagsResult, err := ctx.Repo.GitRepo.GetTagsAfter(ctx.Query("after"), 10)
  45. if err != nil {
  46. ctx.Handle(500, fmt.Sprintf("GetTags '%s'", ctx.Repo.Repository.RepoPath()), err)
  47. return
  48. }
  49. releases, err := models.GetPublishedReleasesByRepoID(ctx.Repo.Repository.ID, tagsResult.Tags...)
  50. if err != nil {
  51. ctx.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. ctx.Handle(500, "LoadAttributes", err)
  65. return
  66. }
  67. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  68. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  69. return
  70. }
  71. r.Note = string(markup.Markdown(r.Note, ctx.Repo.RepoLink, ctx.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 := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  78. if err != nil {
  79. ctx.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. ctx.Handle(500, "CommitsCount", err)
  90. return
  91. }
  92. results[i].NumCommitsBehind = ctx.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(ctx.Repo.Repository.ID)
  100. if err != nil {
  101. ctx.Handle(500, "GetDraftReleasesByRepoID", err)
  102. return
  103. }
  104. for _, r := range drafts {
  105. if err = r.LoadAttributes(); err != nil {
  106. ctx.Handle(500, "LoadAttributes", err)
  107. return
  108. }
  109. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  110. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  111. return
  112. }
  113. r.Note = string(markup.Markdown(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  114. }
  115. if len(drafts) > 0 {
  116. results = append(drafts, results...)
  117. }
  118. }
  119. ctx.Data["Releases"] = results
  120. ctx.Data["HasPrevious"] = !tagsResult.HasLatest
  121. ctx.Data["ReachEnd"] = tagsResult.ReachEnd
  122. ctx.Data["PreviousAfter"] = tagsResult.PreviousAfter
  123. if len(results) > 0 {
  124. ctx.Data["NextAfter"] = results[len(results)-1].TagName
  125. }
  126. ctx.HTML(200, RELEASES)
  127. }
  128. func renderReleaseAttachmentSettings(ctx *context.Context) {
  129. ctx.Data["RequireDropzone"] = true
  130. ctx.Data["IsAttachmentEnabled"] = setting.Release.Attachment.Enabled
  131. ctx.Data["AttachmentAllowedTypes"] = strings.Join(setting.Release.Attachment.AllowedTypes, ",")
  132. ctx.Data["AttachmentMaxSize"] = setting.Release.Attachment.MaxSize
  133. ctx.Data["AttachmentMaxFiles"] = setting.Release.Attachment.MaxFiles
  134. }
  135. func NewRelease(ctx *context.Context) {
  136. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  137. ctx.Data["PageIsReleaseList"] = true
  138. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  139. renderReleaseAttachmentSettings(ctx)
  140. ctx.HTML(200, RELEASE_NEW)
  141. }
  142. func NewReleasePost(ctx *context.Context, f form.NewRelease) {
  143. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  144. ctx.Data["PageIsReleaseList"] = true
  145. renderReleaseAttachmentSettings(ctx)
  146. if ctx.HasError() {
  147. ctx.HTML(200, RELEASE_NEW)
  148. return
  149. }
  150. if !ctx.Repo.GitRepo.IsBranchExist(f.Target) {
  151. ctx.RenderWithErr(ctx.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 := ctx.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 := ctx.Repo.GitRepo.GetBranchCommit(f.Target)
  164. if err != nil {
  165. ctx.Handle(500, "GetBranchCommit", err)
  166. return
  167. }
  168. commitsCount, err := commit.CommitsCount()
  169. if err != nil {
  170. ctx.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: ctx.Repo.Repository.ID,
  179. PublisherID: ctx.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(ctx.Repo.GitRepo, rel, attachments); err != nil {
  191. ctx.Data["Err_TagName"] = true
  192. switch {
  193. case models.IsErrReleaseAlreadyExist(err):
  194. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &f)
  195. case models.IsErrInvalidTagName(err):
  196. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f)
  197. default:
  198. ctx.Handle(500, "NewRelease", err)
  199. }
  200. return
  201. }
  202. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, f.TagName)
  203. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  204. }
  205. func EditRelease(ctx *context.Context) {
  206. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  207. ctx.Data["PageIsReleaseList"] = true
  208. ctx.Data["PageIsEditRelease"] = true
  209. renderReleaseAttachmentSettings(ctx)
  210. tagName := ctx.Params("*")
  211. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  212. if err != nil {
  213. if models.IsErrReleaseNotExist(err) {
  214. ctx.Handle(404, "GetRelease", err)
  215. } else {
  216. ctx.Handle(500, "GetRelease", err)
  217. }
  218. return
  219. }
  220. ctx.Data["ID"] = rel.ID
  221. ctx.Data["tag_name"] = rel.TagName
  222. ctx.Data["tag_target"] = rel.Target
  223. ctx.Data["title"] = rel.Title
  224. ctx.Data["content"] = rel.Note
  225. ctx.Data["attachments"] = rel.Attachments
  226. ctx.Data["prerelease"] = rel.IsPrerelease
  227. ctx.Data["IsDraft"] = rel.IsDraft
  228. ctx.HTML(200, RELEASE_NEW)
  229. }
  230. func EditReleasePost(ctx *context.Context, f form.EditRelease) {
  231. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  232. ctx.Data["PageIsReleaseList"] = true
  233. ctx.Data["PageIsEditRelease"] = true
  234. renderReleaseAttachmentSettings(ctx)
  235. tagName := ctx.Params("*")
  236. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  237. if err != nil {
  238. if models.IsErrReleaseNotExist(err) {
  239. ctx.Handle(404, "GetRelease", err)
  240. } else {
  241. ctx.Handle(500, "GetRelease", err)
  242. }
  243. return
  244. }
  245. ctx.Data["tag_name"] = rel.TagName
  246. ctx.Data["tag_target"] = rel.Target
  247. ctx.Data["title"] = rel.Title
  248. ctx.Data["content"] = rel.Note
  249. ctx.Data["attachments"] = rel.Attachments
  250. ctx.Data["prerelease"] = rel.IsPrerelease
  251. ctx.Data["IsDraft"] = rel.IsDraft
  252. if ctx.HasError() {
  253. ctx.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(ctx.User, ctx.Repo.GitRepo, rel, isPublish, attachments); err != nil {
  266. ctx.Handle(500, "UpdateRelease", err)
  267. return
  268. }
  269. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  270. }
  271. func UploadReleaseAttachment(ctx *context.Context) {
  272. if !setting.Release.Attachment.Enabled {
  273. ctx.NotFound()
  274. return
  275. }
  276. uploadAttachment(ctx, setting.Release.Attachment.AllowedTypes)
  277. }
  278. func DeleteRelease(ctx *context.Context) {
  279. if err := models.DeleteReleaseOfRepoByID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  280. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  281. } else {
  282. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  283. }
  284. ctx.JSON(200, map[string]interface{}{
  285. "redirect": ctx.Repo.RepoLink + "/releases",
  286. })
  287. }