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 12 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
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 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
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
4 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
4 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. auth "code.gitea.io/gitea/modules/forms"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup/markdown"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/upload"
  17. "code.gitea.io/gitea/modules/web"
  18. releaseservice "code.gitea.io/gitea/services/release"
  19. )
  20. const (
  21. tplReleases base.TplName = "repo/release/list"
  22. tplReleaseNew base.TplName = "repo/release/new"
  23. )
  24. // calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
  25. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error {
  26. // Fast return if release target is same as default branch.
  27. if repoCtx.BranchName == release.Target {
  28. release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits
  29. return nil
  30. }
  31. // Get count if not exists
  32. if _, ok := countCache[release.Target]; !ok {
  33. if repoCtx.GitRepo.IsBranchExist(release.Target) {
  34. commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
  35. if err != nil {
  36. return fmt.Errorf("GetBranchCommit: %v", err)
  37. }
  38. countCache[release.Target], err = commit.CommitsCount()
  39. if err != nil {
  40. return fmt.Errorf("CommitsCount: %v", err)
  41. }
  42. } else {
  43. // Use NumCommits of the newest release on that target
  44. countCache[release.Target] = release.NumCommits
  45. }
  46. }
  47. release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
  48. return nil
  49. }
  50. // Releases render releases list page
  51. func Releases(ctx *context.Context) {
  52. releasesOrTags(ctx, false)
  53. }
  54. // TagsList render tags list page
  55. func TagsList(ctx *context.Context) {
  56. releasesOrTags(ctx, true)
  57. }
  58. func releasesOrTags(ctx *context.Context, isTagList bool) {
  59. ctx.Data["PageIsReleaseList"] = true
  60. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  61. if isTagList {
  62. ctx.Data["Title"] = ctx.Tr("repo.release.tags")
  63. ctx.Data["PageIsTagList"] = true
  64. } else {
  65. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  66. ctx.Data["PageIsTagList"] = false
  67. }
  68. writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
  69. ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
  70. opts := models.FindReleasesOptions{
  71. ListOptions: models.ListOptions{
  72. Page: ctx.QueryInt("page"),
  73. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  74. },
  75. IncludeDrafts: writeAccess,
  76. IncludeTags: isTagList,
  77. }
  78. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts)
  79. if err != nil {
  80. ctx.ServerError("GetReleasesByRepoID", err)
  81. return
  82. }
  83. count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, opts)
  84. if err != nil {
  85. ctx.ServerError("GetReleaseCountByRepoID", err)
  86. return
  87. }
  88. if err = models.GetReleaseAttachments(releases...); err != nil {
  89. ctx.ServerError("GetReleaseAttachments", err)
  90. return
  91. }
  92. // Temporary cache commits count of used branches to speed up.
  93. countCache := make(map[string]int64)
  94. cacheUsers := make(map[int64]*models.User)
  95. if ctx.User != nil {
  96. cacheUsers[ctx.User.ID] = ctx.User
  97. }
  98. var ok bool
  99. for _, r := range releases {
  100. if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
  101. r.Publisher, err = models.GetUserByID(r.PublisherID)
  102. if err != nil {
  103. if models.IsErrUserNotExist(err) {
  104. r.Publisher = models.NewGhostUser()
  105. } else {
  106. ctx.ServerError("GetUserByID", err)
  107. return
  108. }
  109. }
  110. cacheUsers[r.PublisherID] = r.Publisher
  111. }
  112. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  113. ctx.ServerError("calReleaseNumCommitsBehind", err)
  114. return
  115. }
  116. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  117. }
  118. ctx.Data["Releases"] = releases
  119. ctx.Data["ReleasesNum"] = len(releases)
  120. pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
  121. pager.SetDefaultParams(ctx)
  122. ctx.Data["Page"] = pager
  123. ctx.HTML(200, tplReleases)
  124. }
  125. // SingleRelease renders a single release's page
  126. func SingleRelease(ctx *context.Context) {
  127. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  128. ctx.Data["PageIsReleaseList"] = true
  129. writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
  130. ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
  131. release, err := models.GetRelease(ctx.Repo.Repository.ID, ctx.Params("*"))
  132. if err != nil {
  133. if models.IsErrReleaseNotExist(err) {
  134. ctx.NotFound("GetRelease", err)
  135. return
  136. }
  137. ctx.ServerError("GetReleasesByRepoID", err)
  138. return
  139. }
  140. err = models.GetReleaseAttachments(release)
  141. if err != nil {
  142. ctx.ServerError("GetReleaseAttachments", err)
  143. return
  144. }
  145. release.Publisher, err = models.GetUserByID(release.PublisherID)
  146. if err != nil {
  147. if models.IsErrUserNotExist(err) {
  148. release.Publisher = models.NewGhostUser()
  149. } else {
  150. ctx.ServerError("GetUserByID", err)
  151. return
  152. }
  153. }
  154. if err := calReleaseNumCommitsBehind(ctx.Repo, release, make(map[string]int64)); err != nil {
  155. ctx.ServerError("calReleaseNumCommitsBehind", err)
  156. return
  157. }
  158. release.Note = markdown.RenderString(release.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  159. ctx.Data["Releases"] = []*models.Release{release}
  160. ctx.HTML(200, tplReleases)
  161. }
  162. // LatestRelease redirects to the latest release
  163. func LatestRelease(ctx *context.Context) {
  164. release, err := models.GetLatestReleaseByRepoID(ctx.Repo.Repository.ID)
  165. if err != nil {
  166. if models.IsErrReleaseNotExist(err) {
  167. ctx.NotFound("LatestRelease", err)
  168. return
  169. }
  170. ctx.ServerError("GetLatestReleaseByRepoID", err)
  171. return
  172. }
  173. if err := release.LoadAttributes(); err != nil {
  174. ctx.ServerError("LoadAttributes", err)
  175. return
  176. }
  177. ctx.Redirect(release.HTMLURL())
  178. }
  179. // NewRelease render creating release page
  180. func NewRelease(ctx *context.Context) {
  181. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  182. ctx.Data["PageIsReleaseList"] = true
  183. ctx.Data["RequireSimpleMDE"] = true
  184. ctx.Data["RequireTribute"] = true
  185. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  186. if tagName := ctx.Query("tag"); len(tagName) > 0 {
  187. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  188. if err != nil && !models.IsErrReleaseNotExist(err) {
  189. ctx.ServerError("GetRelease", err)
  190. return
  191. }
  192. if rel != nil {
  193. ctx.Data["tag_name"] = rel.TagName
  194. ctx.Data["tag_target"] = rel.Target
  195. ctx.Data["title"] = rel.Title
  196. ctx.Data["content"] = rel.Note
  197. }
  198. }
  199. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  200. upload.AddUploadContext(ctx, "release")
  201. ctx.HTML(200, tplReleaseNew)
  202. }
  203. // NewReleasePost response for creating a release
  204. func NewReleasePost(ctx *context.Context) {
  205. form := web.GetForm(ctx).(*auth.NewReleaseForm)
  206. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  207. ctx.Data["PageIsReleaseList"] = true
  208. ctx.Data["RequireSimpleMDE"] = true
  209. ctx.Data["RequireTribute"] = true
  210. if ctx.HasError() {
  211. ctx.HTML(200, tplReleaseNew)
  212. return
  213. }
  214. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  215. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  216. return
  217. }
  218. var attachmentUUIDs []string
  219. if setting.Attachment.Enabled {
  220. attachmentUUIDs = form.Files
  221. }
  222. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  223. if err != nil {
  224. if !models.IsErrReleaseNotExist(err) {
  225. ctx.ServerError("GetRelease", err)
  226. return
  227. }
  228. rel = &models.Release{
  229. RepoID: ctx.Repo.Repository.ID,
  230. PublisherID: ctx.User.ID,
  231. Title: form.Title,
  232. TagName: form.TagName,
  233. Target: form.Target,
  234. Note: form.Content,
  235. IsDraft: len(form.Draft) > 0,
  236. IsPrerelease: form.Prerelease,
  237. IsTag: false,
  238. }
  239. if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  240. ctx.Data["Err_TagName"] = true
  241. switch {
  242. case models.IsErrReleaseAlreadyExist(err):
  243. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  244. case models.IsErrInvalidTagName(err):
  245. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  246. default:
  247. ctx.ServerError("CreateRelease", err)
  248. }
  249. return
  250. }
  251. } else {
  252. if !rel.IsTag {
  253. ctx.Data["Err_TagName"] = true
  254. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  255. return
  256. }
  257. rel.Title = form.Title
  258. rel.Note = form.Content
  259. rel.Target = form.Target
  260. rel.IsDraft = len(form.Draft) > 0
  261. rel.IsPrerelease = form.Prerelease
  262. rel.PublisherID = ctx.User.ID
  263. rel.IsTag = false
  264. if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, true); err != nil {
  265. ctx.Data["Err_TagName"] = true
  266. ctx.ServerError("UpdateReleaseOrCreatReleaseFromTag", err)
  267. return
  268. }
  269. }
  270. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  271. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  272. }
  273. // EditRelease render release edit page
  274. func EditRelease(ctx *context.Context) {
  275. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  276. ctx.Data["PageIsReleaseList"] = true
  277. ctx.Data["PageIsEditRelease"] = true
  278. ctx.Data["RequireSimpleMDE"] = true
  279. ctx.Data["RequireTribute"] = true
  280. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  281. upload.AddUploadContext(ctx, "release")
  282. tagName := ctx.Params("*")
  283. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  284. if err != nil {
  285. if models.IsErrReleaseNotExist(err) {
  286. ctx.NotFound("GetRelease", err)
  287. } else {
  288. ctx.ServerError("GetRelease", err)
  289. }
  290. return
  291. }
  292. ctx.Data["ID"] = rel.ID
  293. ctx.Data["tag_name"] = rel.TagName
  294. ctx.Data["tag_target"] = rel.Target
  295. ctx.Data["title"] = rel.Title
  296. ctx.Data["content"] = rel.Note
  297. ctx.Data["prerelease"] = rel.IsPrerelease
  298. ctx.Data["IsDraft"] = rel.IsDraft
  299. ctx.HTML(200, tplReleaseNew)
  300. }
  301. // EditReleasePost response for edit release
  302. func EditReleasePost(ctx *context.Context) {
  303. form := web.GetForm(ctx).(*auth.EditReleaseForm)
  304. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  305. ctx.Data["PageIsReleaseList"] = true
  306. ctx.Data["PageIsEditRelease"] = true
  307. ctx.Data["RequireSimpleMDE"] = true
  308. ctx.Data["RequireTribute"] = true
  309. tagName := ctx.Params("*")
  310. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  311. if err != nil {
  312. if models.IsErrReleaseNotExist(err) {
  313. ctx.NotFound("GetRelease", err)
  314. } else {
  315. ctx.ServerError("GetRelease", err)
  316. }
  317. return
  318. }
  319. if rel.IsTag {
  320. ctx.NotFound("GetRelease", err)
  321. return
  322. }
  323. ctx.Data["tag_name"] = rel.TagName
  324. ctx.Data["tag_target"] = rel.Target
  325. ctx.Data["title"] = rel.Title
  326. ctx.Data["content"] = rel.Note
  327. ctx.Data["prerelease"] = rel.IsPrerelease
  328. if ctx.HasError() {
  329. ctx.HTML(200, tplReleaseNew)
  330. return
  331. }
  332. var attachmentUUIDs []string
  333. if setting.Attachment.Enabled {
  334. attachmentUUIDs = form.Files
  335. }
  336. rel.Title = form.Title
  337. rel.Note = form.Content
  338. rel.IsDraft = len(form.Draft) > 0
  339. rel.IsPrerelease = form.Prerelease
  340. if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, false); err != nil {
  341. ctx.ServerError("UpdateRelease", err)
  342. return
  343. }
  344. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  345. }
  346. // DeleteRelease delete a release
  347. func DeleteRelease(ctx *context.Context) {
  348. deleteReleaseOrTag(ctx, false)
  349. }
  350. // DeleteTag delete a tag
  351. func DeleteTag(ctx *context.Context) {
  352. deleteReleaseOrTag(ctx, true)
  353. }
  354. func deleteReleaseOrTag(ctx *context.Context, isDelTag bool) {
  355. if err := releaseservice.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, isDelTag); err != nil {
  356. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  357. } else {
  358. if isDelTag {
  359. ctx.Flash.Success(ctx.Tr("repo.release.deletion_tag_success"))
  360. } else {
  361. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  362. }
  363. }
  364. if isDelTag {
  365. ctx.JSON(200, map[string]interface{}{
  366. "redirect": ctx.Repo.RepoLink + "/tags",
  367. })
  368. return
  369. }
  370. ctx.JSON(200, map[string]interface{}{
  371. "redirect": ctx.Repo.RepoLink + "/releases",
  372. })
  373. }