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

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