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 7.7 kB

11 years ago
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
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/markdown"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. const (
  14. RELEASES base.TplName = "repo/release/list"
  15. RELEASE_NEW base.TplName = "repo/release/new"
  16. )
  17. func Releases(ctx *middleware.Context) {
  18. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  19. ctx.Data["PageIsReleaseList"] = true
  20. rawTags, err := ctx.Repo.GitRepo.GetTags()
  21. if err != nil {
  22. ctx.Handle(500, "GetTags", err)
  23. return
  24. }
  25. rels, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
  26. if err != nil {
  27. ctx.Handle(500, "GetReleasesByRepoID", err)
  28. return
  29. }
  30. // Temproray cache commits count of used branches to speed up.
  31. countCache := make(map[string]int64)
  32. tags := make([]*models.Release, len(rawTags))
  33. for i, rawTag := range rawTags {
  34. for j, rel := range rels {
  35. if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
  36. continue
  37. }
  38. if rel.TagName == rawTag {
  39. rel.Publisher, err = models.GetUserByID(rel.PublisherID)
  40. if err != nil {
  41. if models.IsErrUserNotExist(err) {
  42. rel.Publisher = models.NewFakeUser()
  43. } else {
  44. ctx.Handle(500, "GetUserByID", err)
  45. return
  46. }
  47. }
  48. // FIXME: duplicated code.
  49. // Get corresponding target if it's not the current branch.
  50. if ctx.Repo.BranchName != rel.Target {
  51. // Get count if not exists.
  52. if _, ok := countCache[rel.Target]; !ok {
  53. commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
  54. if err != nil {
  55. ctx.Handle(500, "GetBranchCommit", err)
  56. return
  57. }
  58. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  59. if err != nil {
  60. ctx.Handle(500, "CommitsCount", err)
  61. return
  62. }
  63. }
  64. rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
  65. } else {
  66. rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
  67. }
  68. rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  69. tags[i] = rel
  70. rels[j] = nil // Mark as used.
  71. break
  72. }
  73. }
  74. if tags[i] == nil {
  75. commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag)
  76. if err != nil {
  77. ctx.Handle(500, "GetTagCommit", err)
  78. return
  79. }
  80. tags[i] = &models.Release{
  81. Title: rawTag,
  82. TagName: rawTag,
  83. Sha1: commit.ID.String(),
  84. }
  85. tags[i].NumCommits, err = commit.CommitsCount()
  86. if err != nil {
  87. ctx.Handle(500, "CommitsCount", err)
  88. return
  89. }
  90. tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits
  91. }
  92. }
  93. for _, rel := range rels {
  94. if rel == nil {
  95. continue
  96. }
  97. rel.Publisher, err = models.GetUserByID(rel.PublisherID)
  98. if err != nil {
  99. if models.IsErrUserNotExist(err) {
  100. rel.Publisher = models.NewFakeUser()
  101. } else {
  102. ctx.Handle(500, "GetUserByID", err)
  103. return
  104. }
  105. }
  106. // FIXME: duplicated code.
  107. // Get corresponding target if it's not the current branch.
  108. if ctx.Repo.BranchName != rel.Target {
  109. // Get count if not exists.
  110. if _, ok := countCache[rel.Target]; !ok {
  111. commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
  112. if err != nil {
  113. ctx.Handle(500, "GetBranchCommit", err)
  114. return
  115. }
  116. countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
  117. if err != nil {
  118. ctx.Handle(500, "CommitsCount", err)
  119. return
  120. }
  121. }
  122. rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
  123. } else {
  124. rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
  125. }
  126. rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  127. tags = append(tags, rel)
  128. }
  129. models.SortReleases(tags)
  130. ctx.Data["Releases"] = tags
  131. ctx.HTML(200, RELEASES)
  132. }
  133. func NewRelease(ctx *middleware.Context) {
  134. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  135. ctx.Data["PageIsReleaseList"] = true
  136. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  137. ctx.HTML(200, RELEASE_NEW)
  138. }
  139. func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
  140. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  141. ctx.Data["PageIsReleaseList"] = true
  142. if ctx.HasError() {
  143. ctx.HTML(200, RELEASE_NEW)
  144. return
  145. }
  146. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  147. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &form)
  148. return
  149. }
  150. commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target)
  151. if err != nil {
  152. ctx.Handle(500, "GetBranchCommit", err)
  153. return
  154. }
  155. commitsCount, err := commit.CommitsCount()
  156. if err != nil {
  157. ctx.Handle(500, "CommitsCount", err)
  158. return
  159. }
  160. rel := &models.Release{
  161. RepoID: ctx.Repo.Repository.ID,
  162. PublisherID: ctx.User.Id,
  163. Title: form.Title,
  164. TagName: form.TagName,
  165. Target: form.Target,
  166. Sha1: commit.ID.String(),
  167. NumCommits: commitsCount,
  168. Note: form.Content,
  169. IsDraft: len(form.Draft) > 0,
  170. IsPrerelease: form.Prerelease,
  171. }
  172. if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
  173. if models.IsErrReleaseAlreadyExist(err) {
  174. ctx.Data["Err_TagName"] = true
  175. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form)
  176. } else {
  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 *middleware.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(":tagname")
  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.HTML(200, RELEASE_NEW)
  205. }
  206. func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
  207. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  208. ctx.Data["PageIsReleaseList"] = true
  209. ctx.Data["PageIsEditRelease"] = true
  210. tagName := ctx.Params(":tagname")
  211. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  212. if err != nil {
  213. if models.IsErrReleaseNotExist(err) {
  214. ctx.Handle(404, "GetRelease", err)
  215. } else {
  216. ctx.Handle(500, "GetRelease", err)
  217. }
  218. return
  219. }
  220. ctx.Data["tag_name"] = rel.TagName
  221. ctx.Data["tag_target"] = rel.Target
  222. ctx.Data["title"] = rel.Title
  223. ctx.Data["content"] = rel.Note
  224. ctx.Data["prerelease"] = rel.IsPrerelease
  225. if ctx.HasError() {
  226. ctx.HTML(200, RELEASE_NEW)
  227. return
  228. }
  229. rel.Title = form.Title
  230. rel.Note = form.Content
  231. rel.IsDraft = len(form.Draft) > 0
  232. rel.IsPrerelease = form.Prerelease
  233. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
  234. ctx.Handle(500, "UpdateRelease", err)
  235. return
  236. }
  237. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  238. }
  239. func DeleteRelease(ctx *middleware.Context) {
  240. if err := models.DeleteReleaseByID(ctx.QueryInt64("id")); err != nil {
  241. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  242. } else {
  243. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  244. }
  245. ctx.JSON(200, map[string]interface{}{
  246. "redirect": ctx.Repo.RepoLink + "/releases",
  247. })
  248. }