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_label.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  97. if err != nil {
  98. if models.IsErrIssueNotExist(err) {
  99. ctx.NotFound()
  100. } else {
  101. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  102. }
  103. return
  104. }
  105. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  106. ctx.Status(http.StatusForbidden)
  107. return
  108. }
  109. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  110. if err != nil {
  111. ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
  112. return
  113. }
  114. if err = issue_service.AddLabels(issue, ctx.User, labels); err != nil {
  115. ctx.Error(http.StatusInternalServerError, "AddLabels", err)
  116. return
  117. }
  118. labels, err = models.GetLabelsByIssueID(issue.ID)
  119. if err != nil {
  120. ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
  121. return
  122. }
  123. apiLabels := make([]*api.Label, len(labels))
  124. for i := range labels {
  125. apiLabels[i] = labels[i].APIFormat()
  126. }
  127. ctx.JSON(http.StatusOK, &apiLabels)
  128. }
  129. // DeleteIssueLabel delete a label for an issue
  130. func DeleteIssueLabel(ctx *context.APIContext) {
  131. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels/{id} issue issueRemoveLabel
  132. // ---
  133. // summary: Remove a label from an issue
  134. // produces:
  135. // - application/json
  136. // parameters:
  137. // - name: owner
  138. // in: path
  139. // description: owner of the repo
  140. // type: string
  141. // required: true
  142. // - name: repo
  143. // in: path
  144. // description: name of the repo
  145. // type: string
  146. // required: true
  147. // - name: index
  148. // in: path
  149. // description: index of the issue
  150. // type: integer
  151. // format: int64
  152. // required: true
  153. // - name: id
  154. // in: path
  155. // description: id of the label to remove
  156. // type: integer
  157. // format: int64
  158. // required: true
  159. // responses:
  160. // "204":
  161. // "$ref": "#/responses/empty"
  162. // "403":
  163. // "$ref": "#/responses/forbidden"
  164. // "422":
  165. // "$ref": "#/responses/validationError"
  166. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  167. if err != nil {
  168. if models.IsErrIssueNotExist(err) {
  169. ctx.NotFound()
  170. } else {
  171. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  172. }
  173. return
  174. }
  175. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  176. ctx.Status(http.StatusForbidden)
  177. return
  178. }
  179. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  180. if err != nil {
  181. if models.IsErrLabelNotExist(err) {
  182. ctx.Error(http.StatusUnprocessableEntity, "", err)
  183. } else {
  184. ctx.Error(http.StatusInternalServerError, "GetLabelInRepoByID", err)
  185. }
  186. return
  187. }
  188. if err := models.DeleteIssueLabel(issue, label, ctx.User); err != nil {
  189. ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err)
  190. return
  191. }
  192. ctx.Status(http.StatusNoContent)
  193. }
  194. // ReplaceIssueLabels replace labels for an issue
  195. func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
  196. // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/labels issue issueReplaceLabels
  197. // ---
  198. // summary: Replace an issue's labels
  199. // consumes:
  200. // - application/json
  201. // produces:
  202. // - application/json
  203. // parameters:
  204. // - name: owner
  205. // in: path
  206. // description: owner of the repo
  207. // type: string
  208. // required: true
  209. // - name: repo
  210. // in: path
  211. // description: name of the repo
  212. // type: string
  213. // required: true
  214. // - name: index
  215. // in: path
  216. // description: index of the issue
  217. // type: integer
  218. // format: int64
  219. // required: true
  220. // - name: body
  221. // in: body
  222. // schema:
  223. // "$ref": "#/definitions/IssueLabelsOption"
  224. // responses:
  225. // "200":
  226. // "$ref": "#/responses/LabelList"
  227. // "403":
  228. // "$ref": "#/responses/forbidden"
  229. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  230. if err != nil {
  231. if models.IsErrIssueNotExist(err) {
  232. ctx.NotFound()
  233. } else {
  234. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  235. }
  236. return
  237. }
  238. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  239. ctx.Status(http.StatusForbidden)
  240. return
  241. }
  242. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  243. if err != nil {
  244. ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
  245. return
  246. }
  247. if err := issue.ReplaceLabels(labels, ctx.User); err != nil {
  248. ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err)
  249. return
  250. }
  251. labels, err = models.GetLabelsByIssueID(issue.ID)
  252. if err != nil {
  253. ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
  254. return
  255. }
  256. apiLabels := make([]*api.Label, len(labels))
  257. for i := range labels {
  258. apiLabels[i] = labels[i].APIFormat()
  259. }
  260. ctx.JSON(http.StatusOK, &apiLabels)
  261. }
  262. // ClearIssueLabels delete all the labels for an issue
  263. func ClearIssueLabels(ctx *context.APIContext) {
  264. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels issue issueClearLabels
  265. // ---
  266. // summary: Remove all labels from an issue
  267. // produces:
  268. // - application/json
  269. // parameters:
  270. // - name: owner
  271. // in: path
  272. // description: owner of the repo
  273. // type: string
  274. // required: true
  275. // - name: repo
  276. // in: path
  277. // description: name of the repo
  278. // type: string
  279. // required: true
  280. // - name: index
  281. // in: path
  282. // description: index of the issue
  283. // type: integer
  284. // format: int64
  285. // required: true
  286. // responses:
  287. // "204":
  288. // "$ref": "#/responses/empty"
  289. // "403":
  290. // "$ref": "#/responses/forbidden"
  291. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  292. if err != nil {
  293. if models.IsErrIssueNotExist(err) {
  294. ctx.NotFound()
  295. } else {
  296. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  297. }
  298. return
  299. }
  300. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  301. ctx.Status(http.StatusForbidden)
  302. return
  303. }
  304. if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
  305. ctx.Error(http.StatusInternalServerError, "ClearLabels", err)
  306. return
  307. }
  308. ctx.Status(http.StatusNoContent)
  309. }