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

release.go 8.2 kB

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