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.6 kB

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