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.

issue_label.go 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright 2016 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. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. api "code.gitea.io/gitea/modules/structs"
  11. issue_service "code.gitea.io/gitea/services/issue"
  12. )
  13. // ListIssueLabels list all the labels of an issue
  14. func ListIssueLabels(ctx *context.APIContext) {
  15. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/labels issue issueGetLabels
  16. // ---
  17. // summary: Get an issue's labels
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: owner
  22. // in: path
  23. // description: owner of the repo
  24. // type: string
  25. // required: true
  26. // - name: repo
  27. // in: path
  28. // description: name of the repo
  29. // type: string
  30. // required: true
  31. // - name: index
  32. // in: path
  33. // description: index of the issue
  34. // type: integer
  35. // format: int64
  36. // required: true
  37. // responses:
  38. // "200":
  39. // "$ref": "#/responses/LabelList"
  40. // "404":
  41. // "$ref": "#/responses/notFound"
  42. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  43. if err != nil {
  44. if models.IsErrIssueNotExist(err) {
  45. ctx.NotFound()
  46. } else {
  47. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  48. }
  49. return
  50. }
  51. if err := issue.LoadAttributes(); err != nil {
  52. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  53. return
  54. }
  55. apiLabels := make([]*api.Label, len(issue.Labels))
  56. for i := range issue.Labels {
  57. apiLabels[i] = issue.Labels[i].APIFormat()
  58. }
  59. ctx.JSON(http.StatusOK, &apiLabels)
  60. }
  61. // AddIssueLabels add labels for an issue
  62. func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
  63. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/labels issue issueAddLabel
  64. // ---
  65. // summary: Add a label to an issue
  66. // consumes:
  67. // - application/json
  68. // produces:
  69. // - application/json
  70. // parameters:
  71. // - name: owner
  72. // in: path
  73. // description: owner of the repo
  74. // type: string
  75. // required: true
  76. // - name: repo
  77. // in: path
  78. // description: name of the repo
  79. // type: string
  80. // required: true
  81. // - name: index
  82. // in: path
  83. // description: index of the issue
  84. // type: integer
  85. // format: int64
  86. // required: true
  87. // - name: body
  88. // in: body
  89. // schema:
  90. // "$ref": "#/definitions/IssueLabelsOption"
  91. // responses:
  92. // "200":
  93. // "$ref": "#/responses/LabelList"
  94. // "403":
  95. // "$ref": "#/responses/forbidden"
  96. issue, labels, err := prepareForReplaceOrAdd(ctx, form)
  97. if err != nil {
  98. return
  99. }
  100. if err = issue_service.AddLabels(issue, ctx.User, labels); err != nil {
  101. ctx.Error(http.StatusInternalServerError, "AddLabels", err)
  102. return
  103. }
  104. labels, err = models.GetLabelsByIssueID(issue.ID)
  105. if err != nil {
  106. ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
  107. return
  108. }
  109. apiLabels := make([]*api.Label, len(labels))
  110. for i := range labels {
  111. apiLabels[i] = labels[i].APIFormat()
  112. }
  113. ctx.JSON(http.StatusOK, &apiLabels)
  114. }
  115. // DeleteIssueLabel delete a label for an issue
  116. func DeleteIssueLabel(ctx *context.APIContext) {
  117. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels/{id} issue issueRemoveLabel
  118. // ---
  119. // summary: Remove a label from an issue
  120. // produces:
  121. // - application/json
  122. // parameters:
  123. // - name: owner
  124. // in: path
  125. // description: owner of the repo
  126. // type: string
  127. // required: true
  128. // - name: repo
  129. // in: path
  130. // description: name of the repo
  131. // type: string
  132. // required: true
  133. // - name: index
  134. // in: path
  135. // description: index of the issue
  136. // type: integer
  137. // format: int64
  138. // required: true
  139. // - name: id
  140. // in: path
  141. // description: id of the label to remove
  142. // type: integer
  143. // format: int64
  144. // required: true
  145. // responses:
  146. // "204":
  147. // "$ref": "#/responses/empty"
  148. // "403":
  149. // "$ref": "#/responses/forbidden"
  150. // "422":
  151. // "$ref": "#/responses/validationError"
  152. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  153. if err != nil {
  154. if models.IsErrIssueNotExist(err) {
  155. ctx.NotFound()
  156. } else {
  157. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  158. }
  159. return
  160. }
  161. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  162. ctx.Status(http.StatusForbidden)
  163. return
  164. }
  165. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  166. if err != nil {
  167. if models.IsErrLabelNotExist(err) {
  168. ctx.Error(http.StatusUnprocessableEntity, "", err)
  169. } else {
  170. ctx.Error(http.StatusInternalServerError, "GetLabelInRepoByID", err)
  171. }
  172. return
  173. }
  174. if err := issue_service.RemoveLabel(issue, ctx.User, label); err != nil {
  175. ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err)
  176. return
  177. }
  178. ctx.Status(http.StatusNoContent)
  179. }
  180. // ReplaceIssueLabels replace labels for an issue
  181. func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
  182. // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/labels issue issueReplaceLabels
  183. // ---
  184. // summary: Replace an issue's labels
  185. // consumes:
  186. // - application/json
  187. // produces:
  188. // - application/json
  189. // parameters:
  190. // - name: owner
  191. // in: path
  192. // description: owner of the repo
  193. // type: string
  194. // required: true
  195. // - name: repo
  196. // in: path
  197. // description: name of the repo
  198. // type: string
  199. // required: true
  200. // - name: index
  201. // in: path
  202. // description: index of the issue
  203. // type: integer
  204. // format: int64
  205. // required: true
  206. // - name: body
  207. // in: body
  208. // schema:
  209. // "$ref": "#/definitions/IssueLabelsOption"
  210. // responses:
  211. // "200":
  212. // "$ref": "#/responses/LabelList"
  213. // "403":
  214. // "$ref": "#/responses/forbidden"
  215. issue, labels, err := prepareForReplaceOrAdd(ctx, form)
  216. if err != nil {
  217. return
  218. }
  219. if err := issue_service.ReplaceLabels(issue, ctx.User, labels); err != nil {
  220. ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err)
  221. return
  222. }
  223. labels, err = models.GetLabelsByIssueID(issue.ID)
  224. if err != nil {
  225. ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
  226. return
  227. }
  228. apiLabels := make([]*api.Label, len(labels))
  229. for i := range labels {
  230. apiLabels[i] = labels[i].APIFormat()
  231. }
  232. ctx.JSON(http.StatusOK, &apiLabels)
  233. }
  234. // ClearIssueLabels delete all the labels for an issue
  235. func ClearIssueLabels(ctx *context.APIContext) {
  236. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels issue issueClearLabels
  237. // ---
  238. // summary: Remove all labels from an issue
  239. // produces:
  240. // - application/json
  241. // parameters:
  242. // - name: owner
  243. // in: path
  244. // description: owner of the repo
  245. // type: string
  246. // required: true
  247. // - name: repo
  248. // in: path
  249. // description: name of the repo
  250. // type: string
  251. // required: true
  252. // - name: index
  253. // in: path
  254. // description: index of the issue
  255. // type: integer
  256. // format: int64
  257. // required: true
  258. // responses:
  259. // "204":
  260. // "$ref": "#/responses/empty"
  261. // "403":
  262. // "$ref": "#/responses/forbidden"
  263. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  264. if err != nil {
  265. if models.IsErrIssueNotExist(err) {
  266. ctx.NotFound()
  267. } else {
  268. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  269. }
  270. return
  271. }
  272. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  273. ctx.Status(http.StatusForbidden)
  274. return
  275. }
  276. if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
  277. ctx.Error(http.StatusInternalServerError, "ClearLabels", err)
  278. return
  279. }
  280. ctx.Status(http.StatusNoContent)
  281. }
  282. func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) (issue *models.Issue, labels []*models.Label, err error) {
  283. issue, err = models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  284. if err != nil {
  285. if models.IsErrIssueNotExist(err) {
  286. ctx.NotFound()
  287. } else {
  288. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  289. }
  290. return
  291. }
  292. labels, err = models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  293. if err != nil {
  294. ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
  295. return
  296. }
  297. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  298. ctx.Status(http.StatusForbidden)
  299. return
  300. }
  301. return
  302. }