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.

issue_pin.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. issues_model "code.gitea.io/gitea/models/issues"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/json"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // IssuePinOrUnpin pin or unpin a Issue
  12. func IssuePinOrUnpin(ctx *context.Context) {
  13. issue := GetActionIssue(ctx)
  14. if ctx.Written() {
  15. return
  16. }
  17. // If we don't do this, it will crash when trying to add the pin event to the comment history
  18. err := issue.LoadRepo(ctx)
  19. if err != nil {
  20. ctx.Status(http.StatusInternalServerError)
  21. log.Error(err.Error())
  22. return
  23. }
  24. err = issue.PinOrUnpin(ctx, ctx.Doer)
  25. if err != nil {
  26. ctx.Status(http.StatusInternalServerError)
  27. log.Error(err.Error())
  28. return
  29. }
  30. ctx.JSONRedirect(issue.Link())
  31. }
  32. // IssueUnpin unpins a Issue
  33. func IssueUnpin(ctx *context.Context) {
  34. issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  35. if err != nil {
  36. ctx.Status(http.StatusInternalServerError)
  37. log.Error(err.Error())
  38. return
  39. }
  40. // If we don't do this, it will crash when trying to add the pin event to the comment history
  41. err = issue.LoadRepo(ctx)
  42. if err != nil {
  43. ctx.Status(http.StatusInternalServerError)
  44. log.Error(err.Error())
  45. return
  46. }
  47. err = issue.Unpin(ctx, ctx.Doer)
  48. if err != nil {
  49. ctx.Status(http.StatusInternalServerError)
  50. log.Error(err.Error())
  51. return
  52. }
  53. ctx.Status(http.StatusNoContent)
  54. }
  55. // IssuePinMove moves a pinned Issue
  56. func IssuePinMove(ctx *context.Context) {
  57. if ctx.Doer == nil {
  58. ctx.JSON(http.StatusForbidden, "Only signed in users are allowed to perform this action.")
  59. return
  60. }
  61. type movePinIssueForm struct {
  62. ID int64 `json:"id"`
  63. Position int `json:"position"`
  64. }
  65. form := &movePinIssueForm{}
  66. if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
  67. ctx.Status(http.StatusInternalServerError)
  68. log.Error(err.Error())
  69. return
  70. }
  71. issue, err := issues_model.GetIssueByID(ctx, form.ID)
  72. if err != nil {
  73. ctx.Status(http.StatusInternalServerError)
  74. log.Error(err.Error())
  75. return
  76. }
  77. if issue.RepoID != ctx.Repo.Repository.ID {
  78. ctx.Status(http.StatusNotFound)
  79. log.Error("Issue does not belong to this repository")
  80. return
  81. }
  82. err = issue.MovePin(ctx, form.Position)
  83. if err != nil {
  84. ctx.Status(http.StatusInternalServerError)
  85. log.Error(err.Error())
  86. return
  87. }
  88. ctx.Status(http.StatusNoContent)
  89. }