release.go 7.8 KB

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