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.3KB

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