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 7.5 kB

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