You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

label.go 5.8 kB

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