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.

232 lines
6.0 KiB

10 years ago
10 years ago
10 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. RELEASE_EDIT base.TplName = "repo/release/edit"
  16. )
  17. func Releases(ctx *middleware.Context) {
  18. ctx.Data["Title"] = "Releases"
  19. ctx.Data["IsRepoToolbarReleases"] = true
  20. ctx.Data["IsRepoReleaseNew"] = false
  21. rawTags, err := ctx.Repo.GitRepo.GetTags()
  22. if err != nil {
  23. ctx.Handle(500, "release.Releases(GetTags)", err)
  24. return
  25. }
  26. rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id)
  27. if err != nil {
  28. ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err)
  29. return
  30. }
  31. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  32. if err != nil {
  33. ctx.Handle(500, "release.Releases(CommitsCount)", err)
  34. return
  35. }
  36. // Temproray cache commits count of used branches to speed up.
  37. countCache := make(map[string]int)
  38. tags := make([]*models.Release, len(rawTags))
  39. for i, rawTag := range rawTags {
  40. for _, rel := range rels {
  41. if rel.IsDraft && !ctx.Repo.IsOwner {
  42. continue
  43. }
  44. if rel.TagName == rawTag {
  45. rel.Publisher, err = models.GetUserById(rel.PublisherId)
  46. if err != nil {
  47. ctx.Handle(500, "GetUserById", err)
  48. return
  49. }
  50. // Get corresponding target if it's not the current branch.
  51. if ctx.Repo.BranchName != rel.Target {
  52. // Get count if not exists.
  53. if _, ok := countCache[rel.Target]; !ok {
  54. commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName)
  55. if err != nil {
  56. ctx.Handle(500, "GetCommitOfTag", err)
  57. return
  58. }
  59. countCache[rel.Target], err = commit.CommitsCount()
  60. if err != nil {
  61. ctx.Handle(500, "CommitsCount2", err)
  62. return
  63. }
  64. }
  65. rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits
  66. } else {
  67. rel.NumCommitsBehind = commitsCount - rel.NumCommits
  68. }
  69. rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
  70. tags[i] = rel
  71. break
  72. }
  73. }
  74. if tags[i] == nil {
  75. commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
  76. if err != nil {
  77. ctx.Handle(500, "GetCommitOfTag2", err)
  78. return
  79. }
  80. tags[i] = &models.Release{
  81. Title: rawTag,
  82. TagName: rawTag,
  83. Sha1: commit.Id.String(),
  84. }
  85. tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
  86. if err != nil {
  87. ctx.Handle(500, "CommitsCount", err)
  88. return
  89. }
  90. tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits
  91. }
  92. }
  93. models.SortReleases(tags)
  94. ctx.Data["Releases"] = tags
  95. ctx.HTML(200, RELEASES)
  96. }
  97. func NewRelease(ctx *middleware.Context) {
  98. if !ctx.Repo.IsOwner {
  99. ctx.Handle(403, "release.ReleasesNew", nil)
  100. return
  101. }
  102. ctx.Data["Title"] = "New Release"
  103. ctx.Data["IsRepoToolbarReleases"] = true
  104. ctx.Data["IsRepoReleaseNew"] = true
  105. ctx.HTML(200, RELEASE_NEW)
  106. }
  107. func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
  108. if !ctx.Repo.IsOwner {
  109. ctx.Handle(403, "release.ReleasesNew", nil)
  110. return
  111. }
  112. ctx.Data["Title"] = "New Release"
  113. ctx.Data["IsRepoToolbarReleases"] = true
  114. ctx.Data["IsRepoReleaseNew"] = true
  115. if ctx.HasError() {
  116. ctx.HTML(200, RELEASE_NEW)
  117. return
  118. }
  119. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  120. if err != nil {
  121. ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err)
  122. return
  123. }
  124. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  125. ctx.RenderWithErr("Target branch does not exist", "release/new", &form)
  126. return
  127. }
  128. rel := &models.Release{
  129. RepoId: ctx.Repo.Repository.Id,
  130. PublisherId: ctx.User.Id,
  131. Title: form.Title,
  132. TagName: form.TagName,
  133. Target: form.Target,
  134. Sha1: ctx.Repo.Commit.Id.String(),
  135. NumCommits: commitsCount,
  136. Note: form.Content,
  137. IsDraft: len(form.Draft) > 0,
  138. IsPrerelease: form.Prerelease,
  139. }
  140. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  141. if err == models.ErrReleaseAlreadyExist {
  142. ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form)
  143. } else {
  144. ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err)
  145. }
  146. return
  147. }
  148. log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  149. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  150. }
  151. func EditRelease(ctx *middleware.Context) {
  152. if !ctx.Repo.IsOwner {
  153. ctx.Handle(403, "release.ReleasesEdit", nil)
  154. return
  155. }
  156. tagName := ctx.Params(":tagname")
  157. rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
  158. if err != nil {
  159. if err == models.ErrReleaseNotExist {
  160. ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err)
  161. } else {
  162. ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err)
  163. }
  164. return
  165. }
  166. ctx.Data["Release"] = rel
  167. ctx.Data["Title"] = "Edit Release"
  168. ctx.Data["IsRepoToolbarReleases"] = true
  169. ctx.HTML(200, RELEASE_EDIT)
  170. }
  171. func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
  172. if !ctx.Repo.IsOwner {
  173. ctx.Handle(403, "release.EditReleasePost", nil)
  174. return
  175. }
  176. tagName := ctx.Params(":tagname")
  177. rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
  178. if err != nil {
  179. if err == models.ErrReleaseNotExist {
  180. ctx.Handle(404, "release.EditReleasePost(GetRelease)", err)
  181. } else {
  182. ctx.Handle(500, "release.EditReleasePost(GetRelease)", err)
  183. }
  184. return
  185. }
  186. ctx.Data["Release"] = rel
  187. if ctx.HasError() {
  188. ctx.HTML(200, RELEASE_EDIT)
  189. return
  190. }
  191. ctx.Data["Title"] = "Edit Release"
  192. ctx.Data["IsRepoToolbarReleases"] = true
  193. rel.Title = form.Title
  194. rel.Note = form.Content
  195. rel.IsDraft = len(form.Draft) > 0
  196. rel.IsPrerelease = form.Prerelease
  197. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  198. ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err)
  199. return
  200. }
  201. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  202. }