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.

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