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.

label.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "net/http"
  7. "strconv"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. "code.gitea.io/gitea/modules/label"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/web"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. "code.gitea.io/gitea/services/context"
  14. "code.gitea.io/gitea/services/convert"
  15. )
  16. // ListLabels list all the labels of a repository
  17. func ListLabels(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/labels issue issueListLabels
  19. // ---
  20. // summary: Get all of a repository's labels
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: page
  35. // in: query
  36. // description: page number of results to return (1-based)
  37. // type: integer
  38. // - name: limit
  39. // in: query
  40. // description: page size of results
  41. // type: integer
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/LabelList"
  45. // "404":
  46. // "$ref": "#/responses/notFound"
  47. labels, err := issues_model.GetLabelsByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormString("sort"), utils.GetListOptions(ctx))
  48. if err != nil {
  49. ctx.Error(http.StatusInternalServerError, "GetLabelsByRepoID", err)
  50. return
  51. }
  52. count, err := issues_model.CountLabelsByRepoID(ctx, ctx.Repo.Repository.ID)
  53. if err != nil {
  54. ctx.InternalServerError(err)
  55. return
  56. }
  57. ctx.SetTotalCountHeader(count)
  58. ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, nil))
  59. }
  60. // GetLabel get label by repository and label id
  61. func GetLabel(ctx *context.APIContext) {
  62. // swagger:operation GET /repos/{owner}/{repo}/labels/{id} issue issueGetLabel
  63. // ---
  64. // summary: Get a single label
  65. // produces:
  66. // - application/json
  67. // parameters:
  68. // - name: owner
  69. // in: path
  70. // description: owner of the repo
  71. // type: string
  72. // required: true
  73. // - name: repo
  74. // in: path
  75. // description: name of the repo
  76. // type: string
  77. // required: true
  78. // - name: id
  79. // in: path
  80. // description: id of the label to get
  81. // type: integer
  82. // format: int64
  83. // required: true
  84. // responses:
  85. // "200":
  86. // "$ref": "#/responses/Label"
  87. // "404":
  88. // "$ref": "#/responses/notFound"
  89. var (
  90. l *issues_model.Label
  91. err error
  92. )
  93. strID := ctx.Params(":id")
  94. if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
  95. l, err = issues_model.GetLabelInRepoByName(ctx, ctx.Repo.Repository.ID, strID)
  96. } else {
  97. l, err = issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, intID)
  98. }
  99. if err != nil {
  100. if issues_model.IsErrRepoLabelNotExist(err) {
  101. ctx.NotFound()
  102. } else {
  103. ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
  104. }
  105. return
  106. }
  107. ctx.JSON(http.StatusOK, convert.ToLabel(l, ctx.Repo.Repository, nil))
  108. }
  109. // CreateLabel create a label for a repository
  110. func CreateLabel(ctx *context.APIContext) {
  111. // swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel
  112. // ---
  113. // summary: Create a label
  114. // consumes:
  115. // - application/json
  116. // produces:
  117. // - application/json
  118. // parameters:
  119. // - name: owner
  120. // in: path
  121. // description: owner of the repo
  122. // type: string
  123. // required: true
  124. // - name: repo
  125. // in: path
  126. // description: name of the repo
  127. // type: string
  128. // required: true
  129. // - name: body
  130. // in: body
  131. // schema:
  132. // "$ref": "#/definitions/CreateLabelOption"
  133. // responses:
  134. // "201":
  135. // "$ref": "#/responses/Label"
  136. // "404":
  137. // "$ref": "#/responses/notFound"
  138. // "422":
  139. // "$ref": "#/responses/validationError"
  140. form := web.GetForm(ctx).(*api.CreateLabelOption)
  141. color, err := label.NormalizeColor(form.Color)
  142. if err != nil {
  143. ctx.Error(http.StatusUnprocessableEntity, "StringToColor", err)
  144. return
  145. }
  146. form.Color = color
  147. l := &issues_model.Label{
  148. Name: form.Name,
  149. Exclusive: form.Exclusive,
  150. Color: form.Color,
  151. RepoID: ctx.Repo.Repository.ID,
  152. Description: form.Description,
  153. }
  154. l.SetArchived(form.IsArchived)
  155. if err := issues_model.NewLabel(ctx, l); err != nil {
  156. ctx.Error(http.StatusInternalServerError, "NewLabel", err)
  157. return
  158. }
  159. ctx.JSON(http.StatusCreated, convert.ToLabel(l, ctx.Repo.Repository, nil))
  160. }
  161. // EditLabel modify a label for a repository
  162. func EditLabel(ctx *context.APIContext) {
  163. // swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel
  164. // ---
  165. // summary: Update a label
  166. // consumes:
  167. // - application/json
  168. // produces:
  169. // - application/json
  170. // parameters:
  171. // - name: owner
  172. // in: path
  173. // description: owner of the repo
  174. // type: string
  175. // required: true
  176. // - name: repo
  177. // in: path
  178. // description: name of the repo
  179. // type: string
  180. // required: true
  181. // - name: id
  182. // in: path
  183. // description: id of the label to edit
  184. // type: integer
  185. // format: int64
  186. // required: true
  187. // - name: body
  188. // in: body
  189. // schema:
  190. // "$ref": "#/definitions/EditLabelOption"
  191. // responses:
  192. // "200":
  193. // "$ref": "#/responses/Label"
  194. // "404":
  195. // "$ref": "#/responses/notFound"
  196. // "422":
  197. // "$ref": "#/responses/validationError"
  198. form := web.GetForm(ctx).(*api.EditLabelOption)
  199. l, err := issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  200. if err != nil {
  201. if issues_model.IsErrRepoLabelNotExist(err) {
  202. ctx.NotFound()
  203. } else {
  204. ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
  205. }
  206. return
  207. }
  208. if form.Name != nil {
  209. l.Name = *form.Name
  210. }
  211. if form.Exclusive != nil {
  212. l.Exclusive = *form.Exclusive
  213. }
  214. if form.Color != nil {
  215. color, err := label.NormalizeColor(*form.Color)
  216. if err != nil {
  217. ctx.Error(http.StatusUnprocessableEntity, "StringToColor", err)
  218. return
  219. }
  220. l.Color = color
  221. }
  222. if form.Description != nil {
  223. l.Description = *form.Description
  224. }
  225. l.SetArchived(form.IsArchived != nil && *form.IsArchived)
  226. if err := issues_model.UpdateLabel(ctx, l); err != nil {
  227. ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
  228. return
  229. }
  230. ctx.JSON(http.StatusOK, convert.ToLabel(l, ctx.Repo.Repository, nil))
  231. }
  232. // DeleteLabel delete a label for a repository
  233. func DeleteLabel(ctx *context.APIContext) {
  234. // swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel
  235. // ---
  236. // summary: Delete a label
  237. // parameters:
  238. // - name: owner
  239. // in: path
  240. // description: owner of the repo
  241. // type: string
  242. // required: true
  243. // - name: repo
  244. // in: path
  245. // description: name of the repo
  246. // type: string
  247. // required: true
  248. // - name: id
  249. // in: path
  250. // description: id of the label to delete
  251. // type: integer
  252. // format: int64
  253. // required: true
  254. // responses:
  255. // "204":
  256. // "$ref": "#/responses/empty"
  257. // "404":
  258. // "$ref": "#/responses/notFound"
  259. if err := issues_model.DeleteLabel(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  260. ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
  261. return
  262. }
  263. ctx.Status(http.StatusNoContent)
  264. }