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.

301 lines
8.3 KiB

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