You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

release.go 7.0 kB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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]int)
  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.GetCommitOfBranch(ctx.Repo.BranchName)
  49. if err != nil {
  50. ctx.Handle(500, "GetCommitOfBranch", err)
  51. return
  52. }
  53. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  54. if err != nil {
  55. ctx.Handle(500, "CommitsCount2", 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)
  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.GetCommitOfTag(rawTag)
  71. if err != nil {
  72. ctx.Handle(500, "GetCommitOfTag2", 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 = ctx.Repo.GitRepo.CommitsCount(commit.ID.String())
  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.GetCommitOfBranch(ctx.Repo.BranchName)
  103. if err != nil {
  104. ctx.Handle(500, "GetCommitOfBranch", err)
  105. return
  106. }
  107. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  108. if err != nil {
  109. ctx.Handle(500, "CommitsCount2", 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)
  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.GetCommitOfBranch(form.Target)
  142. if err != nil {
  143. ctx.Handle(500, "GetCommitOfBranch", 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["tag_name"] = rel.TagName
  190. ctx.Data["tag_target"] = rel.Target
  191. ctx.Data["title"] = rel.Title
  192. ctx.Data["content"] = rel.Note
  193. ctx.Data["prerelease"] = rel.IsPrerelease
  194. ctx.HTML(200, RELEASE_NEW)
  195. }
  196. func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
  197. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  198. ctx.Data["PageIsReleaseList"] = true
  199. ctx.Data["PageIsEditRelease"] = true
  200. tagName := ctx.Params(":tagname")
  201. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  202. if err != nil {
  203. if models.IsErrReleaseNotExist(err) {
  204. ctx.Handle(404, "GetRelease", err)
  205. } else {
  206. ctx.Handle(500, "GetRelease", err)
  207. }
  208. return
  209. }
  210. ctx.Data["tag_name"] = rel.TagName
  211. ctx.Data["tag_target"] = rel.Target
  212. ctx.Data["title"] = rel.Title
  213. ctx.Data["content"] = rel.Note
  214. ctx.Data["prerelease"] = rel.IsPrerelease
  215. if ctx.HasError() {
  216. ctx.HTML(200, RELEASE_NEW)
  217. return
  218. }
  219. rel.Title = form.Title
  220. rel.Note = form.Content
  221. rel.IsDraft = len(form.Draft) > 0
  222. rel.IsPrerelease = form.Prerelease
  223. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  224. ctx.Handle(500, "UpdateRelease", err)
  225. return
  226. }
  227. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  228. }