release.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.NewGhostUser()
  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.NewGhostUser()
  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. var tagCreatedUnix int64
  146. tag, err := ctx.Repo.GitRepo.GetTag(form.TagName)
  147. if err == nil {
  148. commit, err := tag.Commit()
  149. if err == nil {
  150. tagCreatedUnix = commit.Author.When.Unix()
  151. }
  152. }
  153. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  154. if err != nil {
  155. ctx.Handle(500, "GetBranchCommit", 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. CreatedUnix: tagCreatedUnix,
  175. }
  176. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  177. ctx.Data["Err_TagName"] = true
  178. switch {
  179. case models.IsErrReleaseAlreadyExist(err):
  180. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form)
  181. case models.IsErrInvalidTagName(err):
  182. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &form)
  183. default:
  184. ctx.Handle(500, "CreateRelease", err)
  185. }
  186. return
  187. }
  188. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  189. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  190. }
  191. func EditRelease(ctx *context.Context) {
  192. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  193. ctx.Data["PageIsReleaseList"] = true
  194. ctx.Data["PageIsEditRelease"] = true
  195. tagName := ctx.Params("*")
  196. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  197. if err != nil {
  198. if models.IsErrReleaseNotExist(err) {
  199. ctx.Handle(404, "GetRelease", err)
  200. } else {
  201. ctx.Handle(500, "GetRelease", err)
  202. }
  203. return
  204. }
  205. ctx.Data["ID"] = rel.ID
  206. ctx.Data["tag_name"] = rel.TagName
  207. ctx.Data["tag_target"] = rel.Target
  208. ctx.Data["title"] = rel.Title
  209. ctx.Data["content"] = rel.Note
  210. ctx.Data["prerelease"] = rel.IsPrerelease
  211. ctx.HTML(200, RELEASE_NEW)
  212. }
  213. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  214. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  215. ctx.Data["PageIsReleaseList"] = true
  216. ctx.Data["PageIsEditRelease"] = true
  217. tagName := ctx.Params("*")
  218. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  219. if err != nil {
  220. if models.IsErrReleaseNotExist(err) {
  221. ctx.Handle(404, "GetRelease", err)
  222. } else {
  223. ctx.Handle(500, "GetRelease", err)
  224. }
  225. return
  226. }
  227. ctx.Data["tag_name"] = rel.TagName
  228. ctx.Data["tag_target"] = rel.Target
  229. ctx.Data["title"] = rel.Title
  230. ctx.Data["content"] = rel.Note
  231. ctx.Data["prerelease"] = rel.IsPrerelease
  232. if ctx.HasError() {
  233. ctx.HTML(200, RELEASE_NEW)
  234. return
  235. }
  236. rel.Title = form.Title
  237. rel.Note = form.Content
  238. rel.IsDraft = len(form.Draft) > 0
  239. rel.IsPrerelease = form.Prerelease
  240. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  241. ctx.Handle(500, "UpdateRelease", err)
  242. return
  243. }
  244. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  245. }
  246. func DeleteRelease(ctx *context.Context) {
  247. if err := models.DeleteReleaseByID(ctx.QueryInt64("id")); err != nil {
  248. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  249. } else {
  250. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  251. }
  252. ctx.JSON(200, map[string]interface{}{
  253. "redirect": ctx.Repo.RepoLink + "/releases",
  254. })
  255. }