release.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. log "gopkg.in/clog.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/modules/form"
  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. tagsResult, err := ctx.Repo.GitRepo.GetTagsAfter(ctx.Query("after"), 10)
  48. if err != nil {
  49. ctx.Handle(500, fmt.Sprintf("GetTags '%s'", ctx.Repo.Repository.RepoPath()), err)
  50. return
  51. }
  52. // FIXME: should only get releases match tags result and drafts.
  53. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
  54. if err != nil {
  55. ctx.Handle(500, "GetReleasesByRepoID", err)
  56. return
  57. }
  58. // Temproray cache commits count of used branches to speed up.
  59. countCache := make(map[string]int64)
  60. drafts := make([]*models.Release, 0, 1)
  61. tags := make([]*models.Release, len(tagsResult.Tags))
  62. for i, rawTag := range tagsResult.Tags {
  63. for j, r := range releases {
  64. if r == nil ||
  65. (r.IsDraft && !ctx.Repo.IsOwner()) ||
  66. (!r.IsDraft && r.TagName != rawTag) {
  67. continue
  68. }
  69. releases[j] = nil // Mark as used.
  70. r.Publisher, err = models.GetUserByID(r.PublisherID)
  71. if err != nil {
  72. if models.IsErrUserNotExist(err) {
  73. r.Publisher = models.NewGhostUser()
  74. } else {
  75. ctx.Handle(500, "GetUserByID", err)
  76. return
  77. }
  78. }
  79. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  80. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  81. return
  82. }
  83. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  84. if r.TagName == rawTag {
  85. tags[i] = r
  86. break
  87. }
  88. if r.IsDraft {
  89. drafts = append(drafts, r)
  90. }
  91. }
  92. if tags[i] == nil {
  93. commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  94. if err != nil {
  95. ctx.Handle(500, "GetTagCommit", err)
  96. return
  97. }
  98. tags[i] = &models.Release{
  99. Title: rawTag,
  100. TagName: rawTag,
  101. Sha1: commit.ID.String(),
  102. }
  103. tags[i].NumCommits, err = commit.CommitsCount()
  104. if err != nil {
  105. ctx.Handle(500, "CommitsCount", err)
  106. return
  107. }
  108. tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits
  109. }
  110. }
  111. models.SortReleases(tags)
  112. if len(drafts) > 0 && tagsResult.HasLatest {
  113. tags = append(drafts, tags...)
  114. }
  115. ctx.Data["Releases"] = tags
  116. ctx.Data["HasPrevious"] = !tagsResult.HasLatest
  117. ctx.Data["ReachEnd"] = tagsResult.ReachEnd
  118. ctx.Data["PreviousAfter"] = tagsResult.PreviousAfter
  119. if len(tags) > 0 {
  120. ctx.Data["NextAfter"] = tags[len(tags)-1].TagName
  121. }
  122. ctx.HTML(200, RELEASES)
  123. }
  124. func NewRelease(ctx *context.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 *context.Context, f form.NewRelease) {
  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(f.Target) {
  138. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &f)
  139. return
  140. }
  141. var tagCreatedUnix int64
  142. tag, err := ctx.Repo.GitRepo.GetTag(f.TagName)
  143. if err == nil {
  144. commit, err := tag.Commit()
  145. if err == nil {
  146. tagCreatedUnix = commit.Author.When.Unix()
  147. }
  148. }
  149. commit, err := ctx.Repo.GitRepo.GetBranchCommit(f.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: f.Title,
  163. TagName: f.TagName,
  164. Target: f.Target,
  165. Sha1: commit.ID.String(),
  166. NumCommits: commitsCount,
  167. Note: f.Content,
  168. IsDraft: len(f.Draft) > 0,
  169. IsPrerelease: f.Prerelease,
  170. CreatedUnix: tagCreatedUnix,
  171. }
  172. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  173. ctx.Data["Err_TagName"] = true
  174. switch {
  175. case models.IsErrReleaseAlreadyExist(err):
  176. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &f)
  177. case models.IsErrInvalidTagName(err):
  178. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f)
  179. default:
  180. ctx.Handle(500, "CreateRelease", err)
  181. }
  182. return
  183. }
  184. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, f.TagName)
  185. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  186. }
  187. func EditRelease(ctx *context.Context) {
  188. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  189. ctx.Data["PageIsReleaseList"] = true
  190. ctx.Data["PageIsEditRelease"] = true
  191. tagName := ctx.Params("*")
  192. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  193. if err != nil {
  194. if models.IsErrReleaseNotExist(err) {
  195. ctx.Handle(404, "GetRelease", err)
  196. } else {
  197. ctx.Handle(500, "GetRelease", err)
  198. }
  199. return
  200. }
  201. ctx.Data["ID"] = rel.ID
  202. ctx.Data["tag_name"] = rel.TagName
  203. ctx.Data["tag_target"] = rel.Target
  204. ctx.Data["title"] = rel.Title
  205. ctx.Data["content"] = rel.Note
  206. ctx.Data["prerelease"] = rel.IsPrerelease
  207. ctx.Data["IsDraft"] = rel.IsDraft
  208. ctx.HTML(200, RELEASE_NEW)
  209. }
  210. func EditReleasePost(ctx *context.Context, f form.EditRelease) {
  211. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  212. ctx.Data["PageIsReleaseList"] = true
  213. ctx.Data["PageIsEditRelease"] = true
  214. tagName := ctx.Params("*")
  215. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  216. if err != nil {
  217. if models.IsErrReleaseNotExist(err) {
  218. ctx.Handle(404, "GetRelease", err)
  219. } else {
  220. ctx.Handle(500, "GetRelease", err)
  221. }
  222. return
  223. }
  224. ctx.Data["tag_name"] = rel.TagName
  225. ctx.Data["tag_target"] = rel.Target
  226. ctx.Data["title"] = rel.Title
  227. ctx.Data["content"] = rel.Note
  228. ctx.Data["prerelease"] = rel.IsPrerelease
  229. ctx.Data["IsDraft"] = rel.IsDraft
  230. if ctx.HasError() {
  231. ctx.HTML(200, RELEASE_NEW)
  232. return
  233. }
  234. rel.Title = f.Title
  235. rel.Note = f.Content
  236. rel.IsDraft = len(f.Draft) > 0
  237. rel.IsPrerelease = f.Prerelease
  238. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  239. ctx.Handle(500, "UpdateRelease", err)
  240. return
  241. }
  242. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  243. }
  244. func DeleteRelease(ctx *context.Context) {
  245. if err := models.DeleteReleaseOfRepoByID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  246. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  247. } else {
  248. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  249. }
  250. ctx.JSON(200, map[string]interface{}{
  251. "redirect": ctx.Repo.RepoLink + "/releases",
  252. })
  253. }