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

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