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.

332 lines
8.8 KiB

10 years ago
10 years ago
7 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. "errors"
  7. "fmt"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markdown"
  15. "code.gitea.io/gitea/modules/setting"
  16. "github.com/Unknwon/paginater"
  17. )
  18. const (
  19. tplReleases base.TplName = "repo/release/list"
  20. tplReleaseNew base.TplName = "repo/release/new"
  21. )
  22. // calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
  23. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error {
  24. // Fast return if release target is same as default branch.
  25. if repoCtx.BranchName == release.Target {
  26. release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits
  27. return nil
  28. }
  29. // Get count if not exists
  30. if _, ok := countCache[release.Target]; !ok {
  31. if repoCtx.GitRepo.IsBranchExist(release.Target) {
  32. commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
  33. if err != nil {
  34. return fmt.Errorf("GetBranchCommit: %v", err)
  35. }
  36. countCache[release.Target], err = commit.CommitsCount()
  37. if err != nil {
  38. return fmt.Errorf("CommitsCount: %v", err)
  39. }
  40. } else {
  41. // Use NumCommits of the newest release on that target
  42. countCache[release.Target] = release.NumCommits
  43. }
  44. }
  45. release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
  46. return nil
  47. }
  48. // Releases render releases list page
  49. func Releases(ctx *context.Context) {
  50. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  51. ctx.Data["PageIsReleaseList"] = true
  52. page := ctx.QueryInt("page")
  53. if page <= 1 {
  54. page = 1
  55. }
  56. limit := ctx.QueryInt("limit")
  57. if limit <= 0 {
  58. limit = 10
  59. }
  60. rawTags, err := ctx.Repo.GitRepo.GetTagInfos(git.TagOption{})
  61. if err != nil {
  62. ctx.Handle(500, "GetTags", err)
  63. return
  64. }
  65. if len(rawTags) == 0 {
  66. ctx.HTML(200, tplReleases)
  67. return
  68. }
  69. if len(rawTags) <= (page-1)*limit {
  70. ctx.Handle(500, "Releases", errors.New("no more pages"))
  71. return
  72. }
  73. var tags []*git.Tag
  74. if page*limit > len(rawTags) {
  75. tags = rawTags[(page-1)*limit:]
  76. } else {
  77. tags = rawTags[(page-1)*limit : page*limit]
  78. }
  79. var tagNames []string
  80. for _, t := range tags {
  81. tagNames = append(tagNames, t.Name)
  82. }
  83. releases, err := models.GetReleasesByRepoIDAndNames(ctx.Repo.Repository.ID, tagNames)
  84. if err != nil {
  85. ctx.Handle(500, "GetReleasesByRepoIDAndNames", err)
  86. return
  87. }
  88. err = models.GetReleaseAttachments(releases...)
  89. if err != nil {
  90. ctx.Handle(500, "GetReleaseAttachments", err)
  91. return
  92. }
  93. // Temproray cache commits count of used branches to speed up.
  94. countCache := make(map[string]int64)
  95. var cacheUsers = make(map[int64]*models.User)
  96. var ok bool
  97. releaseTags := make([]*models.Release, len(tags))
  98. for i, rawTag := range tags {
  99. for _, r := range releases {
  100. if r.IsDraft && !ctx.Repo.IsOwner() {
  101. continue
  102. }
  103. if r.TagName == rawTag.Name {
  104. if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
  105. r.Publisher, err = models.GetUserByID(r.PublisherID)
  106. if err != nil {
  107. if models.IsErrUserNotExist(err) {
  108. r.Publisher = models.NewGhostUser()
  109. } else {
  110. ctx.Handle(500, "GetUserByID", err)
  111. return
  112. }
  113. }
  114. cacheUsers[r.PublisherID] = r.Publisher
  115. }
  116. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  117. ctx.Handle(500, "calReleaseNumCommitsBehind", err)
  118. return
  119. }
  120. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  121. releaseTags[i] = r
  122. break
  123. }
  124. }
  125. if releaseTags[i] == nil {
  126. releaseTags[i] = &models.Release{
  127. Title: rawTag.Name,
  128. TagName: rawTag.Name,
  129. Sha1: rawTag.Object.String(),
  130. Note: rawTag.Message,
  131. }
  132. releaseTags[i].NumCommits, err = git.CommitsCount(ctx.Repo.GitRepo.Path, rawTag.Object.String())
  133. if err != nil {
  134. ctx.Handle(500, "CommitsCount", err)
  135. return
  136. }
  137. releaseTags[i].NumCommitsBehind = ctx.Repo.CommitsCount - releaseTags[i].NumCommits
  138. }
  139. }
  140. pager := paginater.New(len(rawTags), limit, page, 5)
  141. ctx.Data["Page"] = pager
  142. ctx.Data["Releases"] = releaseTags
  143. ctx.HTML(200, tplReleases)
  144. }
  145. // NewRelease render creating release page
  146. func NewRelease(ctx *context.Context) {
  147. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  148. ctx.Data["PageIsReleaseList"] = true
  149. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  150. renderAttachmentSettings(ctx)
  151. ctx.HTML(200, tplReleaseNew)
  152. }
  153. // NewReleasePost response for creating a release
  154. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  155. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  156. ctx.Data["PageIsReleaseList"] = true
  157. if ctx.HasError() {
  158. ctx.HTML(200, tplReleaseNew)
  159. return
  160. }
  161. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  162. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  163. return
  164. }
  165. var tagCreatedUnix int64
  166. tag, err := ctx.Repo.GitRepo.GetTag(form.TagName)
  167. if err == nil {
  168. commit, err := tag.Commit()
  169. if err == nil {
  170. tagCreatedUnix = commit.Author.When.Unix()
  171. }
  172. }
  173. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  174. if err != nil {
  175. ctx.Handle(500, "GetBranchCommit", err)
  176. return
  177. }
  178. commitsCount, err := commit.CommitsCount()
  179. if err != nil {
  180. ctx.Handle(500, "CommitsCount", err)
  181. return
  182. }
  183. rel := &models.Release{
  184. RepoID: ctx.Repo.Repository.ID,
  185. PublisherID: ctx.User.ID,
  186. Title: form.Title,
  187. TagName: form.TagName,
  188. Target: form.Target,
  189. Sha1: commit.ID.String(),
  190. NumCommits: commitsCount,
  191. Note: form.Content,
  192. IsDraft: len(form.Draft) > 0,
  193. IsPrerelease: form.Prerelease,
  194. CreatedUnix: tagCreatedUnix,
  195. }
  196. var attachmentUUIDs []string
  197. if setting.AttachmentEnabled {
  198. attachmentUUIDs = form.Files
  199. }
  200. if err = models.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  201. ctx.Data["Err_TagName"] = true
  202. switch {
  203. case models.IsErrReleaseAlreadyExist(err):
  204. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  205. case models.IsErrInvalidTagName(err):
  206. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  207. default:
  208. ctx.Handle(500, "CreateRelease", err)
  209. }
  210. return
  211. }
  212. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  213. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  214. }
  215. // EditRelease render release edit page
  216. func EditRelease(ctx *context.Context) {
  217. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  218. ctx.Data["PageIsReleaseList"] = true
  219. ctx.Data["PageIsEditRelease"] = true
  220. renderAttachmentSettings(ctx)
  221. tagName := ctx.Params("*")
  222. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  223. if err != nil {
  224. if models.IsErrReleaseNotExist(err) {
  225. ctx.Handle(404, "GetRelease", err)
  226. } else {
  227. ctx.Handle(500, "GetRelease", err)
  228. }
  229. return
  230. }
  231. ctx.Data["ID"] = rel.ID
  232. ctx.Data["tag_name"] = rel.TagName
  233. ctx.Data["tag_target"] = rel.Target
  234. ctx.Data["title"] = rel.Title
  235. ctx.Data["content"] = rel.Note
  236. ctx.Data["prerelease"] = rel.IsPrerelease
  237. ctx.Data["IsDraft"] = rel.IsDraft
  238. ctx.HTML(200, tplReleaseNew)
  239. }
  240. // EditReleasePost response for edit release
  241. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  242. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  243. ctx.Data["PageIsReleaseList"] = true
  244. ctx.Data["PageIsEditRelease"] = true
  245. tagName := ctx.Params("*")
  246. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  247. if err != nil {
  248. if models.IsErrReleaseNotExist(err) {
  249. ctx.Handle(404, "GetRelease", err)
  250. } else {
  251. ctx.Handle(500, "GetRelease", err)
  252. }
  253. return
  254. }
  255. ctx.Data["tag_name"] = rel.TagName
  256. ctx.Data["tag_target"] = rel.Target
  257. ctx.Data["title"] = rel.Title
  258. ctx.Data["content"] = rel.Note
  259. ctx.Data["prerelease"] = rel.IsPrerelease
  260. if ctx.HasError() {
  261. ctx.HTML(200, tplReleaseNew)
  262. return
  263. }
  264. var attachmentUUIDs []string
  265. if setting.AttachmentEnabled {
  266. attachmentUUIDs = form.Files
  267. }
  268. rel.Title = form.Title
  269. rel.Note = form.Content
  270. rel.IsDraft = len(form.Draft) > 0
  271. rel.IsPrerelease = form.Prerelease
  272. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  273. ctx.Handle(500, "UpdateRelease", err)
  274. return
  275. }
  276. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  277. }
  278. // DeleteRelease delete a release
  279. func DeleteRelease(ctx *context.Context) {
  280. delTag := ctx.QueryBool("delTag")
  281. if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, delTag); err != nil {
  282. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  283. } else {
  284. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  285. }
  286. ctx.JSON(200, map[string]interface{}{
  287. "redirect": ctx.Repo.RepoLink + "/releases",
  288. })
  289. }