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.8 kB

11 years ago
11 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
11 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup/markdown"
  15. "code.gitea.io/gitea/modules/setting"
  16. releaseservice "code.gitea.io/gitea/services/release"
  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. writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
  53. ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
  54. opts := models.FindReleasesOptions{
  55. ListOptions: models.ListOptions{
  56. Page: ctx.QueryInt("page"),
  57. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  58. },
  59. IncludeDrafts: writeAccess,
  60. IncludeTags: true,
  61. }
  62. if opts.ListOptions.Page <= 1 {
  63. opts.ListOptions.Page = 1
  64. }
  65. if opts.ListOptions.PageSize <= 0 {
  66. opts.ListOptions.Page = 10
  67. }
  68. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts)
  69. if err != nil {
  70. ctx.ServerError("GetReleasesByRepoID", err)
  71. return
  72. }
  73. count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, opts)
  74. if err != nil {
  75. ctx.ServerError("GetReleaseCountByRepoID", err)
  76. return
  77. }
  78. err = models.GetReleaseAttachments(releases...)
  79. if err != nil {
  80. ctx.ServerError("GetReleaseAttachments", err)
  81. return
  82. }
  83. // Temporary cache commits count of used branches to speed up.
  84. countCache := make(map[string]int64)
  85. cacheUsers := make(map[int64]*models.User)
  86. if ctx.User != nil {
  87. cacheUsers[ctx.User.ID] = ctx.User
  88. }
  89. var ok bool
  90. for _, r := range releases {
  91. if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
  92. r.Publisher, err = models.GetUserByID(r.PublisherID)
  93. if err != nil {
  94. if models.IsErrUserNotExist(err) {
  95. r.Publisher = models.NewGhostUser()
  96. } else {
  97. ctx.ServerError("GetUserByID", err)
  98. return
  99. }
  100. }
  101. cacheUsers[r.PublisherID] = r.Publisher
  102. }
  103. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  104. ctx.ServerError("calReleaseNumCommitsBehind", err)
  105. return
  106. }
  107. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  108. }
  109. ctx.Data["Releases"] = releases
  110. pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
  111. pager.SetDefaultParams(ctx)
  112. ctx.Data["Page"] = pager
  113. ctx.HTML(200, tplReleases)
  114. }
  115. // NewRelease render creating release page
  116. func NewRelease(ctx *context.Context) {
  117. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  118. ctx.Data["PageIsReleaseList"] = true
  119. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  120. renderAttachmentSettings(ctx)
  121. ctx.HTML(200, tplReleaseNew)
  122. }
  123. // NewReleasePost response for creating a release
  124. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  125. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  126. ctx.Data["PageIsReleaseList"] = true
  127. if ctx.HasError() {
  128. ctx.HTML(200, tplReleaseNew)
  129. return
  130. }
  131. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  132. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  133. return
  134. }
  135. var attachmentUUIDs []string
  136. if setting.AttachmentEnabled {
  137. attachmentUUIDs = form.Files
  138. }
  139. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  140. if err != nil {
  141. if !models.IsErrReleaseNotExist(err) {
  142. ctx.ServerError("GetRelease", err)
  143. return
  144. }
  145. rel := &models.Release{
  146. RepoID: ctx.Repo.Repository.ID,
  147. PublisherID: ctx.User.ID,
  148. Title: form.Title,
  149. TagName: form.TagName,
  150. Target: form.Target,
  151. Note: form.Content,
  152. IsDraft: len(form.Draft) > 0,
  153. IsPrerelease: form.Prerelease,
  154. IsTag: false,
  155. }
  156. if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  157. ctx.Data["Err_TagName"] = true
  158. switch {
  159. case models.IsErrReleaseAlreadyExist(err):
  160. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  161. case models.IsErrInvalidTagName(err):
  162. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  163. default:
  164. ctx.ServerError("CreateRelease", err)
  165. }
  166. return
  167. }
  168. } else {
  169. if !rel.IsTag {
  170. ctx.Data["Err_TagName"] = true
  171. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  172. return
  173. }
  174. rel.Title = form.Title
  175. rel.Note = form.Content
  176. rel.Target = form.Target
  177. rel.IsDraft = len(form.Draft) > 0
  178. rel.IsPrerelease = form.Prerelease
  179. rel.PublisherID = ctx.User.ID
  180. rel.IsTag = false
  181. if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  182. ctx.Data["Err_TagName"] = true
  183. ctx.ServerError("UpdateRelease", err)
  184. return
  185. }
  186. }
  187. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  188. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  189. }
  190. // EditRelease render release edit page
  191. func EditRelease(ctx *context.Context) {
  192. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  193. ctx.Data["PageIsReleaseList"] = true
  194. ctx.Data["PageIsEditRelease"] = true
  195. renderAttachmentSettings(ctx)
  196. tagName := ctx.Params("*")
  197. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  198. if err != nil {
  199. if models.IsErrReleaseNotExist(err) {
  200. ctx.NotFound("GetRelease", err)
  201. } else {
  202. ctx.ServerError("GetRelease", err)
  203. }
  204. return
  205. }
  206. ctx.Data["ID"] = rel.ID
  207. ctx.Data["tag_name"] = rel.TagName
  208. ctx.Data["tag_target"] = rel.Target
  209. ctx.Data["title"] = rel.Title
  210. ctx.Data["content"] = rel.Note
  211. ctx.Data["prerelease"] = rel.IsPrerelease
  212. ctx.Data["IsDraft"] = rel.IsDraft
  213. ctx.HTML(200, tplReleaseNew)
  214. }
  215. // EditReleasePost response for edit release
  216. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  217. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  218. ctx.Data["PageIsReleaseList"] = true
  219. ctx.Data["PageIsEditRelease"] = true
  220. tagName := ctx.Params("*")
  221. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  222. if err != nil {
  223. if models.IsErrReleaseNotExist(err) {
  224. ctx.NotFound("GetRelease", err)
  225. } else {
  226. ctx.ServerError("GetRelease", err)
  227. }
  228. return
  229. }
  230. if rel.IsTag {
  231. ctx.NotFound("GetRelease", err)
  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, tplReleaseNew)
  241. return
  242. }
  243. var attachmentUUIDs []string
  244. if setting.AttachmentEnabled {
  245. attachmentUUIDs = form.Files
  246. }
  247. rel.Title = form.Title
  248. rel.Note = form.Content
  249. rel.IsDraft = len(form.Draft) > 0
  250. rel.IsPrerelease = form.Prerelease
  251. if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  252. ctx.ServerError("UpdateRelease", err)
  253. return
  254. }
  255. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  256. }
  257. // DeleteRelease delete a release
  258. func DeleteRelease(ctx *context.Context) {
  259. if err := releaseservice.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil {
  260. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  261. } else {
  262. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  263. }
  264. ctx.JSON(200, map[string]interface{}{
  265. "redirect": ctx.Repo.RepoLink + "/releases",
  266. })
  267. }