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.

312 lines
7.2 KiB

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