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 7.6KB

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