gitea源码

issue_lock.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "net/http"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/modules/web"
  10. "code.gitea.io/gitea/services/context"
  11. )
  12. // LockIssue lock an issue
  13. func LockIssue(ctx *context.APIContext) {
  14. // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/lock issue issueLockIssue
  15. // ---
  16. // summary: Lock an issue
  17. // consumes:
  18. // - application/json
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: index
  33. // in: path
  34. // description: index of the issue
  35. // type: integer
  36. // format: int64
  37. // required: true
  38. // - name: body
  39. // in: body
  40. // schema:
  41. // "$ref": "#/definitions/LockIssueOption"
  42. // responses:
  43. // "204":
  44. // "$ref": "#/responses/empty"
  45. // "403":
  46. // "$ref": "#/responses/forbidden"
  47. // "404":
  48. // "$ref": "#/responses/notFound"
  49. reason := web.GetForm(ctx).(*api.LockIssueOption).Reason
  50. issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
  51. if err != nil {
  52. if issues_model.IsErrIssueNotExist(err) {
  53. ctx.APIErrorNotFound(err)
  54. } else {
  55. ctx.APIErrorInternal(err)
  56. }
  57. return
  58. }
  59. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  60. ctx.APIError(http.StatusForbidden, errors.New("no permission to lock this issue"))
  61. return
  62. }
  63. if !issue.IsLocked {
  64. opt := &issues_model.IssueLockOptions{
  65. Doer: ctx.ContextUser,
  66. Issue: issue,
  67. Reason: reason,
  68. }
  69. issue.Repo = ctx.Repo.Repository
  70. err = issues_model.LockIssue(ctx, opt)
  71. if err != nil {
  72. ctx.APIErrorInternal(err)
  73. return
  74. }
  75. }
  76. ctx.Status(http.StatusNoContent)
  77. }
  78. // UnlockIssue unlock an issue
  79. func UnlockIssue(ctx *context.APIContext) {
  80. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/lock issue issueUnlockIssue
  81. // ---
  82. // summary: Unlock an issue
  83. // consumes:
  84. // - application/json
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: owner
  89. // in: path
  90. // description: owner of the repo
  91. // type: string
  92. // required: true
  93. // - name: repo
  94. // in: path
  95. // description: name of the repo
  96. // type: string
  97. // required: true
  98. // - name: index
  99. // in: path
  100. // description: index of the issue
  101. // type: integer
  102. // format: int64
  103. // required: true
  104. // responses:
  105. // "204":
  106. // "$ref": "#/responses/empty"
  107. // "403":
  108. // "$ref": "#/responses/forbidden"
  109. // "404":
  110. // "$ref": "#/responses/notFound"
  111. issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
  112. if err != nil {
  113. if issues_model.IsErrIssueNotExist(err) {
  114. ctx.APIErrorNotFound(err)
  115. } else {
  116. ctx.APIErrorInternal(err)
  117. }
  118. return
  119. }
  120. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  121. ctx.APIError(http.StatusForbidden, errors.New("no permission to unlock this issue"))
  122. return
  123. }
  124. if issue.IsLocked {
  125. opt := &issues_model.IssueLockOptions{
  126. Doer: ctx.ContextUser,
  127. Issue: issue,
  128. }
  129. issue.Repo = ctx.Repo.Repository
  130. err = issues_model.UnlockIssue(ctx, opt)
  131. if err != nil {
  132. ctx.APIErrorInternal(err)
  133. return
  134. }
  135. }
  136. ctx.Status(http.StatusNoContent)
  137. }