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

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