本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

304 lines
8.4KB

  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. releaseservice "code.gitea.io/gitea/services/release"
  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. ctx.Data["Releases"] = releases
  107. pager := context.NewPagination(int(count), limit, page, 5)
  108. pager.SetDefaultParams(ctx)
  109. ctx.Data["Page"] = pager
  110. ctx.HTML(200, tplReleases)
  111. }
  112. // NewRelease render creating release page
  113. func NewRelease(ctx *context.Context) {
  114. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  115. ctx.Data["PageIsReleaseList"] = true
  116. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  117. renderAttachmentSettings(ctx)
  118. ctx.HTML(200, tplReleaseNew)
  119. }
  120. // NewReleasePost response for creating a release
  121. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  122. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  123. ctx.Data["PageIsReleaseList"] = true
  124. if ctx.HasError() {
  125. ctx.HTML(200, tplReleaseNew)
  126. return
  127. }
  128. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  129. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  130. return
  131. }
  132. var attachmentUUIDs []string
  133. if setting.AttachmentEnabled {
  134. attachmentUUIDs = form.Files
  135. }
  136. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  137. if err != nil {
  138. if !models.IsErrReleaseNotExist(err) {
  139. ctx.ServerError("GetRelease", err)
  140. return
  141. }
  142. rel := &models.Release{
  143. RepoID: ctx.Repo.Repository.ID,
  144. PublisherID: ctx.User.ID,
  145. Title: form.Title,
  146. TagName: form.TagName,
  147. Target: form.Target,
  148. Note: form.Content,
  149. IsDraft: len(form.Draft) > 0,
  150. IsPrerelease: form.Prerelease,
  151. IsTag: false,
  152. }
  153. if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  154. ctx.Data["Err_TagName"] = true
  155. switch {
  156. case models.IsErrReleaseAlreadyExist(err):
  157. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  158. case models.IsErrInvalidTagName(err):
  159. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  160. default:
  161. ctx.ServerError("CreateRelease", err)
  162. }
  163. return
  164. }
  165. } else {
  166. if !rel.IsTag {
  167. ctx.Data["Err_TagName"] = true
  168. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  169. return
  170. }
  171. rel.Title = form.Title
  172. rel.Note = form.Content
  173. rel.Target = form.Target
  174. rel.IsDraft = len(form.Draft) > 0
  175. rel.IsPrerelease = form.Prerelease
  176. rel.PublisherID = ctx.User.ID
  177. rel.IsTag = false
  178. if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  179. ctx.Data["Err_TagName"] = true
  180. ctx.ServerError("UpdateRelease", err)
  181. return
  182. }
  183. }
  184. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  185. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  186. }
  187. // EditRelease render release edit page
  188. func EditRelease(ctx *context.Context) {
  189. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  190. ctx.Data["PageIsReleaseList"] = true
  191. ctx.Data["PageIsEditRelease"] = true
  192. renderAttachmentSettings(ctx)
  193. tagName := ctx.Params("*")
  194. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  195. if err != nil {
  196. if models.IsErrReleaseNotExist(err) {
  197. ctx.NotFound("GetRelease", err)
  198. } else {
  199. ctx.ServerError("GetRelease", err)
  200. }
  201. return
  202. }
  203. ctx.Data["ID"] = rel.ID
  204. ctx.Data["tag_name"] = rel.TagName
  205. ctx.Data["tag_target"] = rel.Target
  206. ctx.Data["title"] = rel.Title
  207. ctx.Data["content"] = rel.Note
  208. ctx.Data["prerelease"] = rel.IsPrerelease
  209. ctx.Data["IsDraft"] = rel.IsDraft
  210. ctx.HTML(200, tplReleaseNew)
  211. }
  212. // EditReleasePost response for edit release
  213. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  214. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  215. ctx.Data["PageIsReleaseList"] = true
  216. ctx.Data["PageIsEditRelease"] = true
  217. tagName := ctx.Params("*")
  218. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  219. if err != nil {
  220. if models.IsErrReleaseNotExist(err) {
  221. ctx.NotFound("GetRelease", err)
  222. } else {
  223. ctx.ServerError("GetRelease", err)
  224. }
  225. return
  226. }
  227. if rel.IsTag {
  228. ctx.NotFound("GetRelease", err)
  229. return
  230. }
  231. ctx.Data["tag_name"] = rel.TagName
  232. ctx.Data["tag_target"] = rel.Target
  233. ctx.Data["title"] = rel.Title
  234. ctx.Data["content"] = rel.Note
  235. ctx.Data["prerelease"] = rel.IsPrerelease
  236. if ctx.HasError() {
  237. ctx.HTML(200, tplReleaseNew)
  238. return
  239. }
  240. var attachmentUUIDs []string
  241. if setting.AttachmentEnabled {
  242. attachmentUUIDs = form.Files
  243. }
  244. rel.Title = form.Title
  245. rel.Note = form.Content
  246. rel.IsDraft = len(form.Draft) > 0
  247. rel.IsPrerelease = form.Prerelease
  248. if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  249. ctx.ServerError("UpdateRelease", err)
  250. return
  251. }
  252. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  253. }
  254. // DeleteRelease delete a release
  255. func DeleteRelease(ctx *context.Context) {
  256. if err := releaseservice.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil {
  257. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  258. } else {
  259. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  260. }
  261. ctx.JSON(200, map[string]interface{}{
  262. "redirect": ctx.Repo.RepoLink + "/releases",
  263. })
  264. }
上海开阖软件有限公司 沪ICP备12045867号-1