You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

release.go 8.3 kB

11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/markup/markdown"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/Unknwon/paginater"
  15. )
  16. const (
  17. tplReleases base.TplName = "repo/release/list"
  18. tplReleaseNew 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. // Fast return if release target is same as default branch.
  23. if repoCtx.BranchName == release.Target {
  24. release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits
  25. return nil
  26. }
  27. // Get count if not exists
  28. if _, ok := countCache[release.Target]; !ok {
  29. if repoCtx.GitRepo.IsBranchExist(release.Target) {
  30. commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
  31. if err != nil {
  32. return fmt.Errorf("GetBranchCommit: %v", err)
  33. }
  34. countCache[release.Target], err = commit.CommitsCount()
  35. if err != nil {
  36. return fmt.Errorf("CommitsCount: %v", err)
  37. }
  38. } else {
  39. // Use NumCommits of the newest release on that target
  40. countCache[release.Target] = release.NumCommits
  41. }
  42. }
  43. release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
  44. return nil
  45. }
  46. // Releases render releases list page
  47. func Releases(ctx *context.Context) {
  48. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  49. ctx.Data["PageIsReleaseList"] = true
  50. page := ctx.QueryInt("page")
  51. if page <= 1 {
  52. page = 1
  53. }
  54. limit := ctx.QueryInt("limit")
  55. if limit <= 0 {
  56. limit = 10
  57. }
  58. opts := models.FindReleasesOptions{
  59. IncludeDrafts: ctx.Repo.IsWriter(),
  60. IncludeTags: true,
  61. }
  62. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts, page, limit)
  63. if err != nil {
  64. ctx.Handle(500, "GetReleasesByRepoID", err)
  65. return
  66. }
  67. count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, opts)
  68. if err != nil {
  69. ctx.Handle(500, "GetReleaseCountByRepoID", err)
  70. return
  71. }
  72. err = models.GetReleaseAttachments(releases...)
  73. if err != nil {
  74. ctx.Handle(500, "GetReleaseAttachments", err)
  75. return
  76. }
  77. // Temporary cache commits count of used branches to speed up.
  78. countCache := make(map[string]int64)
  79. cacheUsers := make(map[int64]*models.User)
  80. if ctx.User != nil {
  81. cacheUsers[ctx.User.ID] = ctx.User
  82. }
  83. var ok bool
  84. for _, r := range releases {
  85. if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
  86. r.Publisher, err = models.GetUserByID(r.PublisherID)
  87. if err != nil {
  88. if models.IsErrUserNotExist(err) {
  89. r.Publisher = models.NewGhostUser()
  90. } else {
  91. ctx.Handle(500, "GetUserByID", err)
  92. return
  93. }
  94. }
  95. cacheUsers[r.PublisherID] = r.Publisher
  96. }
  97. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  98. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  99. return
  100. }
  101. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  102. }
  103. pager := paginater.New(int(count), limit, page, 5)
  104. ctx.Data["Page"] = pager
  105. ctx.Data["Releases"] = releases
  106. ctx.HTML(200, tplReleases)
  107. }
  108. // NewRelease render creating release page
  109. func NewRelease(ctx *context.Context) {
  110. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  111. ctx.Data["PageIsReleaseList"] = true
  112. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  113. renderAttachmentSettings(ctx)
  114. ctx.HTML(200, tplReleaseNew)
  115. }
  116. // NewReleasePost response for creating a release
  117. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  118. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  119. ctx.Data["PageIsReleaseList"] = true
  120. if ctx.HasError() {
  121. ctx.HTML(200, tplReleaseNew)
  122. return
  123. }
  124. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  125. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  126. return
  127. }
  128. var attachmentUUIDs []string
  129. if setting.AttachmentEnabled {
  130. attachmentUUIDs = form.Files
  131. }
  132. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  133. if err != nil {
  134. if !models.IsErrReleaseNotExist(err) {
  135. ctx.Handle(500, "GetRelease", err)
  136. return
  137. }
  138. rel := &models.Release{
  139. RepoID: ctx.Repo.Repository.ID,
  140. PublisherID: ctx.User.ID,
  141. Title: form.Title,
  142. TagName: form.TagName,
  143. Target: form.Target,
  144. Note: form.Content,
  145. IsDraft: len(form.Draft) > 0,
  146. IsPrerelease: form.Prerelease,
  147. IsTag: false,
  148. }
  149. if err = models.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  150. ctx.Data["Err_TagName"] = true
  151. switch {
  152. case models.IsErrReleaseAlreadyExist(err):
  153. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  154. case models.IsErrInvalidTagName(err):
  155. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  156. default:
  157. ctx.Handle(500, "CreateRelease", err)
  158. }
  159. return
  160. }
  161. } else {
  162. if !rel.IsTag {
  163. ctx.Data["Err_TagName"] = true
  164. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  165. return
  166. }
  167. rel.Title = form.Title
  168. rel.Note = form.Content
  169. rel.IsDraft = len(form.Draft) > 0
  170. rel.IsPrerelease = form.Prerelease
  171. rel.PublisherID = ctx.User.ID
  172. rel.IsTag = false
  173. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  174. ctx.Data["Err_TagName"] = true
  175. ctx.Handle(500, "UpdateRelease", err)
  176. return
  177. }
  178. }
  179. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  180. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  181. }
  182. // EditRelease render release edit page
  183. func EditRelease(ctx *context.Context) {
  184. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  185. ctx.Data["PageIsReleaseList"] = true
  186. ctx.Data["PageIsEditRelease"] = true
  187. renderAttachmentSettings(ctx)
  188. tagName := ctx.Params("*")
  189. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  190. if err != nil {
  191. if models.IsErrReleaseNotExist(err) {
  192. ctx.Handle(404, "GetRelease", err)
  193. } else {
  194. ctx.Handle(500, "GetRelease", err)
  195. }
  196. return
  197. }
  198. ctx.Data["ID"] = rel.ID
  199. ctx.Data["tag_name"] = rel.TagName
  200. ctx.Data["tag_target"] = rel.Target
  201. ctx.Data["title"] = rel.Title
  202. ctx.Data["content"] = rel.Note
  203. ctx.Data["prerelease"] = rel.IsPrerelease
  204. ctx.Data["IsDraft"] = rel.IsDraft
  205. ctx.HTML(200, tplReleaseNew)
  206. }
  207. // EditReleasePost response for edit release
  208. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  209. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  210. ctx.Data["PageIsReleaseList"] = true
  211. ctx.Data["PageIsEditRelease"] = true
  212. tagName := ctx.Params("*")
  213. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  214. if err != nil {
  215. if models.IsErrReleaseNotExist(err) {
  216. ctx.Handle(404, "GetRelease", err)
  217. } else {
  218. ctx.Handle(500, "GetRelease", err)
  219. }
  220. return
  221. }
  222. if rel.IsTag {
  223. ctx.Handle(404, "GetRelease", err)
  224. return
  225. }
  226. ctx.Data["tag_name"] = rel.TagName
  227. ctx.Data["tag_target"] = rel.Target
  228. ctx.Data["title"] = rel.Title
  229. ctx.Data["content"] = rel.Note
  230. ctx.Data["prerelease"] = rel.IsPrerelease
  231. if ctx.HasError() {
  232. ctx.HTML(200, tplReleaseNew)
  233. return
  234. }
  235. var attachmentUUIDs []string
  236. if setting.AttachmentEnabled {
  237. attachmentUUIDs = form.Files
  238. }
  239. rel.Title = form.Title
  240. rel.Note = form.Content
  241. rel.IsDraft = len(form.Draft) > 0
  242. rel.IsPrerelease = form.Prerelease
  243. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  244. ctx.Handle(500, "UpdateRelease", err)
  245. return
  246. }
  247. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  248. }
  249. // DeleteRelease delete a release
  250. func DeleteRelease(ctx *context.Context) {
  251. if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil {
  252. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  253. } else {
  254. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  255. }
  256. ctx.JSON(200, map[string]interface{}{
  257. "redirect": ctx.Repo.RepoLink + "/releases",
  258. })
  259. }