release.go 7.5 KB

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