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

276 lines
7.3 KiB

10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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 default branch.
  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. commit, err := repoCtx.GitRepo.GetBranchCommit(repoCtx.BranchName)
  28. if err != nil {
  29. return fmt.Errorf("GetBranchCommit: %v", err)
  30. }
  31. countCache[repoCtx.BranchName], err = commit.CommitsCount()
  32. if err != nil {
  33. return fmt.Errorf("CommitsCount: %v", err)
  34. }
  35. }
  36. release.NumCommitsBehind = countCache[repoCtx.BranchName] - release.NumCommits
  37. return nil
  38. }
  39. func Releases(ctx *context.Context) {
  40. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  41. ctx.Data["PageIsReleaseList"] = true
  42. rawTags, err := ctx.Repo.GitRepo.GetTags()
  43. if err != nil {
  44. ctx.Handle(500, "GetTags", err)
  45. return
  46. }
  47. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
  48. if err != nil {
  49. ctx.Handle(500, "GetReleasesByRepoID", err)
  50. return
  51. }
  52. // Temproray cache commits count of used branches to speed up.
  53. countCache := make(map[string]int64)
  54. tags := make([]*models.Release, len(rawTags))
  55. for i, rawTag := range rawTags {
  56. for j, r := range releases {
  57. if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) {
  58. continue
  59. }
  60. if r.TagName == rawTag {
  61. r.Publisher, err = models.GetUserByID(r.PublisherID)
  62. if err != nil {
  63. if models.IsErrUserNotExist(err) {
  64. r.Publisher = models.NewFakeUser()
  65. } else {
  66. ctx.Handle(500, "GetUserByID", err)
  67. return
  68. }
  69. }
  70. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  71. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  72. return
  73. }
  74. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  75. tags[i] = r
  76. releases[j] = nil // Mark as used.
  77. break
  78. }
  79. }
  80. if tags[i] == nil {
  81. commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  82. if err != nil {
  83. ctx.Handle(500, "GetTagCommit", err)
  84. return
  85. }
  86. tags[i] = &models.Release{
  87. Title: rawTag,
  88. TagName: rawTag,
  89. Sha1: commit.ID.String(),
  90. }
  91. tags[i].NumCommits, err = commit.CommitsCount()
  92. if err != nil {
  93. ctx.Handle(500, "CommitsCount", err)
  94. return
  95. }
  96. tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits
  97. }
  98. }
  99. for _, r := range releases {
  100. if r == nil {
  101. continue
  102. }
  103. r.Publisher, err = models.GetUserByID(r.PublisherID)
  104. if err != nil {
  105. if models.IsErrUserNotExist(err) {
  106. r.Publisher = models.NewFakeUser()
  107. } else {
  108. ctx.Handle(500, "GetUserByID", err)
  109. return
  110. }
  111. }
  112. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  113. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  114. return
  115. }
  116. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  117. tags = append(tags, r)
  118. }
  119. models.SortReleases(tags)
  120. ctx.Data["Releases"] = tags
  121. ctx.HTML(200, RELEASES)
  122. }
  123. func NewRelease(ctx *context.Context) {
  124. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  125. ctx.Data["PageIsReleaseList"] = true
  126. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  127. ctx.HTML(200, RELEASE_NEW)
  128. }
  129. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  130. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  131. ctx.Data["PageIsReleaseList"] = true
  132. if ctx.HasError() {
  133. ctx.HTML(200, RELEASE_NEW)
  134. return
  135. }
  136. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  137. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &form)
  138. return
  139. }
  140. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  141. if err != nil {
  142. ctx.Handle(500, "GetBranchCommit", err)
  143. return
  144. }
  145. commitsCount, err := commit.CommitsCount()
  146. if err != nil {
  147. ctx.Handle(500, "CommitsCount", err)
  148. return
  149. }
  150. rel := &models.Release{
  151. RepoID: ctx.Repo.Repository.ID,
  152. PublisherID: ctx.User.Id,
  153. Title: form.Title,
  154. TagName: form.TagName,
  155. Target: form.Target,
  156. Sha1: commit.ID.String(),
  157. NumCommits: commitsCount,
  158. Note: form.Content,
  159. IsDraft: len(form.Draft) > 0,
  160. IsPrerelease: form.Prerelease,
  161. }
  162. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  163. if models.IsErrReleaseAlreadyExist(err) {
  164. ctx.Data["Err_TagName"] = true
  165. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form)
  166. } else {
  167. ctx.Handle(500, "CreateRelease", err)
  168. }
  169. return
  170. }
  171. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  172. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  173. }
  174. func EditRelease(ctx *context.Context) {
  175. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  176. ctx.Data["PageIsReleaseList"] = true
  177. ctx.Data["PageIsEditRelease"] = true
  178. tagName := ctx.Params(":tagname")
  179. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  180. if err != nil {
  181. if models.IsErrReleaseNotExist(err) {
  182. ctx.Handle(404, "GetRelease", err)
  183. } else {
  184. ctx.Handle(500, "GetRelease", err)
  185. }
  186. return
  187. }
  188. ctx.Data["ID"] = rel.ID
  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 *context.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. }
  229. func DeleteRelease(ctx *context.Context) {
  230. if err := models.DeleteReleaseByID(ctx.QueryInt64("id")); err != nil {
  231. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  232. } else {
  233. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  234. }
  235. ctx.JSON(200, map[string]interface{}{
  236. "redirect": ctx.Repo.RepoLink + "/releases",
  237. })
  238. }