release.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. "fmt"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/markdown"
  13. )
  14. const (
  15. RELEASES base.TplName = "repo/release/list"
  16. RELEASE_NEW base.TplName = "repo/release/new"
  17. )
  18. // calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
  19. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error {
  20. // Fast return if release target is same as default branch.
  21. if repoCtx.BranchName == release.Target {
  22. release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits
  23. return nil
  24. }
  25. // Get count if not exists
  26. if _, ok := countCache[release.Target]; !ok {
  27. if repoCtx.GitRepo.IsBranchExist(release.Target) {
  28. commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
  29. if err != nil {
  30. return fmt.Errorf("GetBranchCommit: %v", err)
  31. }
  32. countCache[release.Target], err = commit.CommitsCount()
  33. if err != nil {
  34. return fmt.Errorf("CommitsCount: %v", err)
  35. }
  36. } else {
  37. // Use NumCommits of the newest release on that target
  38. countCache[release.Target] = release.NumCommits
  39. }
  40. }
  41. release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
  42. return nil
  43. }
  44. func Releases(ctx *context.Context) {
  45. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  46. ctx.Data["PageIsReleaseList"] = true
  47. rawTags, err := ctx.Repo.GitRepo.GetTags()
  48. if err != nil {
  49. ctx.Handle(500, "GetTags", err)
  50. return
  51. }
  52. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
  53. if err != nil {
  54. ctx.Handle(500, "GetReleasesByRepoID", err)
  55. return
  56. }
  57. // Temproray cache commits count of used branches to speed up.
  58. countCache := make(map[string]int64)
  59. tags := make([]*models.Release, len(rawTags))
  60. for i, rawTag := range rawTags {
  61. for j, r := range releases {
  62. if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) {
  63. continue
  64. }
  65. if r.TagName == rawTag {
  66. r.Publisher, err = models.GetUserByID(r.PublisherID)
  67. if err != nil {
  68. if models.IsErrUserNotExist(err) {
  69. r.Publisher = models.NewFakeUser()
  70. } else {
  71. ctx.Handle(500, "GetUserByID", err)
  72. return
  73. }
  74. }
  75. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  76. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  77. return
  78. }
  79. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  80. tags[i] = r
  81. releases[j] = nil // Mark as used.
  82. break
  83. }
  84. }
  85. if tags[i] == nil {
  86. commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  87. if err != nil {
  88. ctx.Handle(500, "GetTagCommit", err)
  89. return
  90. }
  91. tags[i] = &models.Release{
  92. Title: rawTag,
  93. TagName: rawTag,
  94. Sha1: commit.ID.String(),
  95. }
  96. tags[i].NumCommits, err = commit.CommitsCount()
  97. if err != nil {
  98. ctx.Handle(500, "CommitsCount", err)
  99. return
  100. }
  101. tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits
  102. }
  103. }
  104. for _, r := range releases {
  105. if r == nil {
  106. continue
  107. }
  108. r.Publisher, err = models.GetUserByID(r.PublisherID)
  109. if err != nil {
  110. if models.IsErrUserNotExist(err) {
  111. r.Publisher = models.NewFakeUser()
  112. } else {
  113. ctx.Handle(500, "GetUserByID", err)
  114. return
  115. }
  116. }
  117. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  118. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  119. return
  120. }
  121. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  122. tags = append(tags, r)
  123. }
  124. models.SortReleases(tags)
  125. ctx.Data["Releases"] = tags
  126. ctx.HTML(200, RELEASES)
  127. }
  128. func NewRelease(ctx *context.Context) {
  129. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  130. ctx.Data["PageIsReleaseList"] = true
  131. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  132. ctx.HTML(200, RELEASE_NEW)
  133. }
  134. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  135. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  136. ctx.Data["PageIsReleaseList"] = true
  137. if ctx.HasError() {
  138. ctx.HTML(200, RELEASE_NEW)
  139. return
  140. }
  141. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  142. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &form)
  143. return
  144. }
  145. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  146. if err != nil {
  147. ctx.Handle(500, "GetBranchCommit", err)
  148. return
  149. }
  150. commitsCount, err := commit.CommitsCount()
  151. if err != nil {
  152. ctx.Handle(500, "CommitsCount", err)
  153. return
  154. }
  155. rel := &models.Release{
  156. RepoID: ctx.Repo.Repository.ID,
  157. PublisherID: ctx.User.ID,
  158. Title: form.Title,
  159. TagName: form.TagName,
  160. Target: form.Target,
  161. Sha1: commit.ID.String(),
  162. NumCommits: commitsCount,
  163. Note: form.Content,
  164. IsDraft: len(form.Draft) > 0,
  165. IsPrerelease: form.Prerelease,
  166. }
  167. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  168. ctx.Data["Err_TagName"] = true
  169. switch {
  170. case models.IsErrReleaseAlreadyExist(err):
  171. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form)
  172. case models.IsErrInvalidTagName(err):
  173. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &form)
  174. default:
  175. ctx.Handle(500, "CreateRelease", err)
  176. }
  177. return
  178. }
  179. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  180. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  181. }
  182. func EditRelease(ctx *context.Context) {
  183. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  184. ctx.Data["PageIsReleaseList"] = true
  185. ctx.Data["PageIsEditRelease"] = true
  186. tagName := ctx.Params(":tagname")
  187. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  188. if err != nil {
  189. if models.IsErrReleaseNotExist(err) {
  190. ctx.Handle(404, "GetRelease", err)
  191. } else {
  192. ctx.Handle(500, "GetRelease", err)
  193. }
  194. return
  195. }
  196. ctx.Data["ID"] = rel.ID
  197. ctx.Data["tag_name"] = rel.TagName
  198. ctx.Data["tag_target"] = rel.Target
  199. ctx.Data["title"] = rel.Title
  200. ctx.Data["content"] = rel.Note
  201. ctx.Data["prerelease"] = rel.IsPrerelease
  202. ctx.HTML(200, RELEASE_NEW)
  203. }
  204. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  205. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  206. ctx.Data["PageIsReleaseList"] = true
  207. ctx.Data["PageIsEditRelease"] = true
  208. tagName := ctx.Params(":tagname")
  209. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  210. if err != nil {
  211. if models.IsErrReleaseNotExist(err) {
  212. ctx.Handle(404, "GetRelease", err)
  213. } else {
  214. ctx.Handle(500, "GetRelease", err)
  215. }
  216. return
  217. }
  218. ctx.Data["tag_name"] = rel.TagName
  219. ctx.Data["tag_target"] = rel.Target
  220. ctx.Data["title"] = rel.Title
  221. ctx.Data["content"] = rel.Note
  222. ctx.Data["prerelease"] = rel.IsPrerelease
  223. if ctx.HasError() {
  224. ctx.HTML(200, RELEASE_NEW)
  225. return
  226. }
  227. rel.Title = form.Title
  228. rel.Note = form.Content
  229. rel.IsDraft = len(form.Draft) > 0
  230. rel.IsPrerelease = form.Prerelease
  231. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  232. ctx.Handle(500, "UpdateRelease", err)
  233. return
  234. }
  235. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  236. }
  237. func DeleteRelease(ctx *context.Context) {
  238. if err := models.DeleteReleaseByID(ctx.QueryInt64("id")); err != nil {
  239. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  240. } else {
  241. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  242. }
  243. ctx.JSON(200, map[string]interface{}{
  244. "redirect": ctx.Repo.RepoLink + "/releases",
  245. })
  246. }