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.

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