gitea源码

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