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

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