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

297 lines
8.1 KiB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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.ServerError("GetReleasesByRepoID", err)
  65. return
  66. }
  67. count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, opts)
  68. if err != nil {
  69. ctx.ServerError("GetReleaseCountByRepoID", err)
  70. return
  71. }
  72. err = models.GetReleaseAttachments(releases...)
  73. if err != nil {
  74. ctx.ServerError("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.ServerError("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.ServerError("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.ServerError("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.ServerError("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.Target = form.Target
  170. rel.IsDraft = len(form.Draft) > 0
  171. rel.IsPrerelease = form.Prerelease
  172. rel.PublisherID = ctx.User.ID
  173. rel.IsTag = false
  174. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  175. ctx.Data["Err_TagName"] = true
  176. ctx.ServerError("UpdateRelease", err)
  177. return
  178. }
  179. }
  180. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  181. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  182. }
  183. // EditRelease render release edit page
  184. func EditRelease(ctx *context.Context) {
  185. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  186. ctx.Data["PageIsReleaseList"] = true
  187. ctx.Data["PageIsEditRelease"] = true
  188. renderAttachmentSettings(ctx)
  189. tagName := ctx.Params("*")
  190. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  191. if err != nil {
  192. if models.IsErrReleaseNotExist(err) {
  193. ctx.NotFound("GetRelease", err)
  194. } else {
  195. ctx.ServerError("GetRelease", err)
  196. }
  197. return
  198. }
  199. ctx.Data["ID"] = rel.ID
  200. ctx.Data["tag_name"] = rel.TagName
  201. ctx.Data["tag_target"] = rel.Target
  202. ctx.Data["title"] = rel.Title
  203. ctx.Data["content"] = rel.Note
  204. ctx.Data["prerelease"] = rel.IsPrerelease
  205. ctx.Data["IsDraft"] = rel.IsDraft
  206. ctx.HTML(200, tplReleaseNew)
  207. }
  208. // EditReleasePost response for edit release
  209. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  210. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  211. ctx.Data["PageIsReleaseList"] = true
  212. ctx.Data["PageIsEditRelease"] = true
  213. tagName := ctx.Params("*")
  214. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  215. if err != nil {
  216. if models.IsErrReleaseNotExist(err) {
  217. ctx.NotFound("GetRelease", err)
  218. } else {
  219. ctx.ServerError("GetRelease", err)
  220. }
  221. return
  222. }
  223. if rel.IsTag {
  224. ctx.NotFound("GetRelease", err)
  225. return
  226. }
  227. ctx.Data["tag_name"] = rel.TagName
  228. ctx.Data["tag_target"] = rel.Target
  229. ctx.Data["title"] = rel.Title
  230. ctx.Data["content"] = rel.Note
  231. ctx.Data["prerelease"] = rel.IsPrerelease
  232. if ctx.HasError() {
  233. ctx.HTML(200, tplReleaseNew)
  234. return
  235. }
  236. var attachmentUUIDs []string
  237. if setting.AttachmentEnabled {
  238. attachmentUUIDs = form.Files
  239. }
  240. rel.Title = form.Title
  241. rel.Note = form.Content
  242. rel.IsDraft = len(form.Draft) > 0
  243. rel.IsPrerelease = form.Prerelease
  244. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  245. ctx.ServerError("UpdateRelease", err)
  246. return
  247. }
  248. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  249. }
  250. // DeleteRelease delete a release
  251. func DeleteRelease(ctx *context.Context) {
  252. if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil {
  253. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  254. } else {
  255. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  256. }
  257. ctx.JSON(200, map[string]interface{}{
  258. "redirect": ctx.Repo.RepoLink + "/releases",
  259. })
  260. }