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