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.

272 lines
7.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
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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/middleware"
  11. )
  12. const (
  13. RELEASES base.TplName = "repo/release/list"
  14. RELEASE_NEW base.TplName = "repo/release/new"
  15. )
  16. func Releases(ctx *middleware.Context) {
  17. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  18. ctx.Data["PageIsReleaseList"] = true
  19. rawTags, err := ctx.Repo.GitRepo.GetTags()
  20. if err != nil {
  21. ctx.Handle(500, "GetTags", err)
  22. return
  23. }
  24. rels, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
  25. if err != nil {
  26. ctx.Handle(500, "GetReleasesByRepoID", err)
  27. return
  28. }
  29. // Temproray cache commits count of used branches to speed up.
  30. countCache := make(map[string]int64)
  31. tags := make([]*models.Release, len(rawTags))
  32. for i, rawTag := range rawTags {
  33. for j, rel := range rels {
  34. if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
  35. continue
  36. }
  37. if rel.TagName == rawTag {
  38. rel.Publisher, err = models.GetUserByID(rel.PublisherID)
  39. if err != nil {
  40. ctx.Handle(500, "GetUserByID", err)
  41. return
  42. }
  43. // FIXME: duplicated code.
  44. // Get corresponding target if it's not the current branch.
  45. if ctx.Repo.BranchName != rel.Target {
  46. // Get count if not exists.
  47. if _, ok := countCache[rel.Target]; !ok {
  48. commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
  49. if err != nil {
  50. ctx.Handle(500, "GetBranchCommit", err)
  51. return
  52. }
  53. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  54. if err != nil {
  55. ctx.Handle(500, "CommitsCount", err)
  56. return
  57. }
  58. }
  59. rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
  60. } else {
  61. rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
  62. }
  63. rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  64. tags[i] = rel
  65. rels[j] = nil // Mark as used.
  66. break
  67. }
  68. }
  69. if tags[i] == nil {
  70. commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  71. if err != nil {
  72. ctx.Handle(500, "GetTagCommit", err)
  73. return
  74. }
  75. tags[i] = &models.Release{
  76. Title: rawTag,
  77. TagName: rawTag,
  78. Sha1: commit.ID.String(),
  79. }
  80. tags[i].NumCommits, err = commit.CommitsCount()
  81. if err != nil {
  82. ctx.Handle(500, "CommitsCount", err)
  83. return
  84. }
  85. tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits
  86. }
  87. }
  88. for _, rel := range rels {
  89. if rel == nil {
  90. continue
  91. }
  92. rel.Publisher, err = models.GetUserByID(rel.PublisherID)
  93. if err != nil {
  94. ctx.Handle(500, "GetUserByID", err)
  95. return
  96. }
  97. // FIXME: duplicated code.
  98. // Get corresponding target if it's not the current branch.
  99. if ctx.Repo.BranchName != rel.Target {
  100. // Get count if not exists.
  101. if _, ok := countCache[rel.Target]; !ok {
  102. commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
  103. if err != nil {
  104. ctx.Handle(500, "GetBranchCommit", err)
  105. return
  106. }
  107. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  108. if err != nil {
  109. ctx.Handle(500, "CommitsCount", err)
  110. return
  111. }
  112. }
  113. rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
  114. } else {
  115. rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
  116. }
  117. rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  118. tags = append(tags, rel)
  119. }
  120. models.SortReleases(tags)
  121. ctx.Data["Releases"] = tags
  122. ctx.HTML(200, RELEASES)
  123. }
  124. func NewRelease(ctx *middleware.Context) {
  125. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  126. ctx.Data["PageIsReleaseList"] = true
  127. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  128. ctx.HTML(200, RELEASE_NEW)
  129. }
  130. func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
  131. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  132. ctx.Data["PageIsReleaseList"] = true
  133. if ctx.HasError() {
  134. ctx.HTML(200, RELEASE_NEW)
  135. return
  136. }
  137. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  138. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &form)
  139. return
  140. }
  141. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  142. if err != nil {
  143. ctx.Handle(500, "GetBranchCommit", err)
  144. return
  145. }
  146. commitsCount, err := commit.CommitsCount()
  147. if err != nil {
  148. ctx.Handle(500, "CommitsCount", err)
  149. return
  150. }
  151. rel := &models.Release{
  152. RepoID: ctx.Repo.Repository.ID,
  153. PublisherID: ctx.User.Id,
  154. Title: form.Title,
  155. TagName: form.TagName,
  156. Target: form.Target,
  157. Sha1: commit.ID.String(),
  158. NumCommits: commitsCount,
  159. Note: form.Content,
  160. IsDraft: len(form.Draft) > 0,
  161. IsPrerelease: form.Prerelease,
  162. }
  163. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  164. if models.IsErrReleaseAlreadyExist(err) {
  165. ctx.Data["Err_TagName"] = true
  166. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form)
  167. } else {
  168. ctx.Handle(500, "CreateRelease", err)
  169. }
  170. return
  171. }
  172. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  173. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  174. }
  175. func EditRelease(ctx *middleware.Context) {
  176. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  177. ctx.Data["PageIsReleaseList"] = true
  178. ctx.Data["PageIsEditRelease"] = true
  179. tagName := ctx.Params(":tagname")
  180. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  181. if err != nil {
  182. if models.IsErrReleaseNotExist(err) {
  183. ctx.Handle(404, "GetRelease", err)
  184. } else {
  185. ctx.Handle(500, "GetRelease", err)
  186. }
  187. return
  188. }
  189. ctx.Data["ID"] = rel.ID
  190. ctx.Data["tag_name"] = rel.TagName
  191. ctx.Data["tag_target"] = rel.Target
  192. ctx.Data["title"] = rel.Title
  193. ctx.Data["content"] = rel.Note
  194. ctx.Data["prerelease"] = rel.IsPrerelease
  195. ctx.HTML(200, RELEASE_NEW)
  196. }
  197. func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
  198. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  199. ctx.Data["PageIsReleaseList"] = true
  200. ctx.Data["PageIsEditRelease"] = true
  201. tagName := ctx.Params(":tagname")
  202. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  203. if err != nil {
  204. if models.IsErrReleaseNotExist(err) {
  205. ctx.Handle(404, "GetRelease", err)
  206. } else {
  207. ctx.Handle(500, "GetRelease", err)
  208. }
  209. return
  210. }
  211. ctx.Data["tag_name"] = rel.TagName
  212. ctx.Data["tag_target"] = rel.Target
  213. ctx.Data["title"] = rel.Title
  214. ctx.Data["content"] = rel.Note
  215. ctx.Data["prerelease"] = rel.IsPrerelease
  216. if ctx.HasError() {
  217. ctx.HTML(200, RELEASE_NEW)
  218. return
  219. }
  220. rel.Title = form.Title
  221. rel.Note = form.Content
  222. rel.IsDraft = len(form.Draft) > 0
  223. rel.IsPrerelease = form.Prerelease
  224. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  225. ctx.Handle(500, "UpdateRelease", err)
  226. return
  227. }
  228. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  229. }
  230. func DeleteRelease(ctx *middleware.Context) {
  231. if err := models.DeleteReleaseByID(ctx.QueryInt64("id")); err != nil {
  232. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  233. } else {
  234. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  235. }
  236. ctx.JSON(200, map[string]interface{}{
  237. "redirect": ctx.Repo.RepoLink + "/releases",
  238. })
  239. }