Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

issue_label.go 7.6KB

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