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.

362 lines
10 KiB

10 years ago
10 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>
4 years ago
10 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>
4 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>
4 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>
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
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
  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. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts)
  63. if err != nil {
  64. ctx.ServerError("GetReleasesByRepoID", err)
  65. return
  66. }
  67. count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, opts)
  68. if err != nil {
  69. ctx.ServerError("GetReleaseCountByRepoID", err)
  70. return
  71. }
  72. err = models.GetReleaseAttachments(releases...)
  73. if err != nil {
  74. ctx.ServerError("GetReleaseAttachments", err)
  75. return
  76. }
  77. // Temporary cache commits count of used branches to speed up.
  78. countCache := make(map[string]int64)
  79. cacheUsers := make(map[int64]*models.User)
  80. if ctx.User != nil {
  81. cacheUsers[ctx.User.ID] = ctx.User
  82. }
  83. var ok bool
  84. for _, r := range releases {
  85. if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
  86. r.Publisher, err = models.GetUserByID(r.PublisherID)
  87. if err != nil {
  88. if models.IsErrUserNotExist(err) {
  89. r.Publisher = models.NewGhostUser()
  90. } else {
  91. ctx.ServerError("GetUserByID", err)
  92. return
  93. }
  94. }
  95. cacheUsers[r.PublisherID] = r.Publisher
  96. }
  97. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
  98. ctx.ServerError("calReleaseNumCommitsBehind", err)
  99. return
  100. }
  101. r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  102. }
  103. ctx.Data["Releases"] = releases
  104. pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
  105. pager.SetDefaultParams(ctx)
  106. ctx.Data["Page"] = pager
  107. ctx.HTML(200, tplReleases)
  108. }
  109. // SingleRelease renders a single release's page
  110. func SingleRelease(ctx *context.Context) {
  111. ctx.Data["Title"] = ctx.Tr("repo.release.releases")
  112. ctx.Data["PageIsReleaseList"] = true
  113. writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
  114. ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
  115. release, err := models.GetRelease(ctx.Repo.Repository.ID, ctx.Params("*"))
  116. if err != nil {
  117. if models.IsErrReleaseNotExist(err) {
  118. ctx.NotFound("GetRelease", err)
  119. return
  120. }
  121. ctx.ServerError("GetReleasesByRepoID", err)
  122. return
  123. }
  124. err = models.GetReleaseAttachments(release)
  125. if err != nil {
  126. ctx.ServerError("GetReleaseAttachments", err)
  127. return
  128. }
  129. release.Publisher, err = models.GetUserByID(release.PublisherID)
  130. if err != nil {
  131. if models.IsErrUserNotExist(err) {
  132. release.Publisher = models.NewGhostUser()
  133. } else {
  134. ctx.ServerError("GetUserByID", err)
  135. return
  136. }
  137. }
  138. if err := calReleaseNumCommitsBehind(ctx.Repo, release, make(map[string]int64)); err != nil {
  139. ctx.ServerError("calReleaseNumCommitsBehind", err)
  140. return
  141. }
  142. release.Note = markdown.RenderString(release.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
  143. ctx.Data["Releases"] = []*models.Release{release}
  144. ctx.HTML(200, tplReleases)
  145. }
  146. // LatestRelease redirects to the latest release
  147. func LatestRelease(ctx *context.Context) {
  148. release, err := models.GetLatestReleaseByRepoID(ctx.Repo.Repository.ID)
  149. if err != nil {
  150. if models.IsErrReleaseNotExist(err) {
  151. ctx.NotFound("LatestRelease", err)
  152. return
  153. }
  154. ctx.ServerError("GetLatestReleaseByRepoID", err)
  155. return
  156. }
  157. if err := release.LoadAttributes(); err != nil {
  158. ctx.ServerError("LoadAttributes", err)
  159. return
  160. }
  161. ctx.Redirect(release.HTMLURL())
  162. }
  163. // NewRelease render creating release page
  164. func NewRelease(ctx *context.Context) {
  165. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  166. ctx.Data["PageIsReleaseList"] = true
  167. ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
  168. renderAttachmentSettings(ctx)
  169. ctx.HTML(200, tplReleaseNew)
  170. }
  171. // NewReleasePost response for creating a release
  172. func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) {
  173. ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
  174. ctx.Data["PageIsReleaseList"] = true
  175. if ctx.HasError() {
  176. ctx.HTML(200, tplReleaseNew)
  177. return
  178. }
  179. if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
  180. ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
  181. return
  182. }
  183. var attachmentUUIDs []string
  184. if setting.Attachment.Enabled {
  185. attachmentUUIDs = form.Files
  186. }
  187. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  188. if err != nil {
  189. if !models.IsErrReleaseNotExist(err) {
  190. ctx.ServerError("GetRelease", err)
  191. return
  192. }
  193. rel = &models.Release{
  194. RepoID: ctx.Repo.Repository.ID,
  195. PublisherID: ctx.User.ID,
  196. Title: form.Title,
  197. TagName: form.TagName,
  198. Target: form.Target,
  199. Note: form.Content,
  200. IsDraft: len(form.Draft) > 0,
  201. IsPrerelease: form.Prerelease,
  202. IsTag: false,
  203. }
  204. if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
  205. ctx.Data["Err_TagName"] = true
  206. switch {
  207. case models.IsErrReleaseAlreadyExist(err):
  208. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  209. case models.IsErrInvalidTagName(err):
  210. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form)
  211. default:
  212. ctx.ServerError("CreateRelease", err)
  213. }
  214. return
  215. }
  216. } else {
  217. if !rel.IsTag {
  218. ctx.Data["Err_TagName"] = true
  219. ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)
  220. return
  221. }
  222. rel.Title = form.Title
  223. rel.Note = form.Content
  224. rel.Target = form.Target
  225. rel.IsDraft = len(form.Draft) > 0
  226. rel.IsPrerelease = form.Prerelease
  227. rel.PublisherID = ctx.User.ID
  228. rel.IsTag = false
  229. if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, true); err != nil {
  230. ctx.Data["Err_TagName"] = true
  231. ctx.ServerError("UpdateReleaseOrCreatReleaseFromTag", err)
  232. return
  233. }
  234. }
  235. log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
  236. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  237. }
  238. // EditRelease render release edit page
  239. func EditRelease(ctx *context.Context) {
  240. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  241. ctx.Data["PageIsReleaseList"] = true
  242. ctx.Data["PageIsEditRelease"] = true
  243. renderAttachmentSettings(ctx)
  244. tagName := ctx.Params("*")
  245. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  246. if err != nil {
  247. if models.IsErrReleaseNotExist(err) {
  248. ctx.NotFound("GetRelease", err)
  249. } else {
  250. ctx.ServerError("GetRelease", err)
  251. }
  252. return
  253. }
  254. ctx.Data["ID"] = rel.ID
  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. ctx.Data["IsDraft"] = rel.IsDraft
  261. ctx.HTML(200, tplReleaseNew)
  262. }
  263. // EditReleasePost response for edit release
  264. func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
  265. ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
  266. ctx.Data["PageIsReleaseList"] = true
  267. ctx.Data["PageIsEditRelease"] = true
  268. tagName := ctx.Params("*")
  269. rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  270. if err != nil {
  271. if models.IsErrReleaseNotExist(err) {
  272. ctx.NotFound("GetRelease", err)
  273. } else {
  274. ctx.ServerError("GetRelease", err)
  275. }
  276. return
  277. }
  278. if rel.IsTag {
  279. ctx.NotFound("GetRelease", err)
  280. return
  281. }
  282. ctx.Data["tag_name"] = rel.TagName
  283. ctx.Data["tag_target"] = rel.Target
  284. ctx.Data["title"] = rel.Title
  285. ctx.Data["content"] = rel.Note
  286. ctx.Data["prerelease"] = rel.IsPrerelease
  287. if ctx.HasError() {
  288. ctx.HTML(200, tplReleaseNew)
  289. return
  290. }
  291. var attachmentUUIDs []string
  292. if setting.Attachment.Enabled {
  293. attachmentUUIDs = form.Files
  294. }
  295. rel.Title = form.Title
  296. rel.Note = form.Content
  297. rel.IsDraft = len(form.Draft) > 0
  298. rel.IsPrerelease = form.Prerelease
  299. if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, false); err != nil {
  300. ctx.ServerError("UpdateRelease", err)
  301. return
  302. }
  303. ctx.Redirect(ctx.Repo.RepoLink + "/releases")
  304. }
  305. // DeleteRelease delete a release
  306. func DeleteRelease(ctx *context.Context) {
  307. if err := releaseservice.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil {
  308. ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
  309. } else {
  310. ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
  311. }
  312. ctx.JSON(200, map[string]interface{}{
  313. "redirect": ctx.Repo.RepoLink + "/releases",
  314. })
  315. }