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.

226 lines
5.3 KiB

  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/sdk/gitea"
  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.Status(404)
  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. }
  123. if err := models.NewLabel(label); err != nil {
  124. ctx.Error(500, "NewLabel", err)
  125. return
  126. }
  127. ctx.JSON(201, label.APIFormat())
  128. }
  129. // EditLabel modify a label for a repository
  130. func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
  131. // swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel
  132. // ---
  133. // summary: Update a label
  134. // consumes:
  135. // - application/json
  136. // produces:
  137. // - application/json
  138. // parameters:
  139. // - name: owner
  140. // in: path
  141. // description: owner of the repo
  142. // type: string
  143. // required: true
  144. // - name: repo
  145. // in: path
  146. // description: name of the repo
  147. // type: string
  148. // required: true
  149. // - name: id
  150. // in: path
  151. // description: id of the label to edit
  152. // type: integer
  153. // format: int64
  154. // required: true
  155. // - name: body
  156. // in: body
  157. // schema:
  158. // "$ref": "#/definitions/EditLabelOption"
  159. // responses:
  160. // "200":
  161. // "$ref": "#/responses/Label"
  162. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  163. if err != nil {
  164. if models.IsErrLabelNotExist(err) {
  165. ctx.Status(404)
  166. } else {
  167. ctx.Error(500, "GetLabelByRepoID", err)
  168. }
  169. return
  170. }
  171. if form.Name != nil {
  172. label.Name = *form.Name
  173. }
  174. if form.Color != nil {
  175. label.Color = *form.Color
  176. }
  177. if err := models.UpdateLabel(label); err != nil {
  178. ctx.ServerError("UpdateLabel", err)
  179. return
  180. }
  181. ctx.JSON(200, label.APIFormat())
  182. }
  183. // DeleteLabel delete a label for a repository
  184. func DeleteLabel(ctx *context.APIContext) {
  185. // swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel
  186. // ---
  187. // summary: Delete a label
  188. // parameters:
  189. // - name: owner
  190. // in: path
  191. // description: owner of the repo
  192. // type: string
  193. // required: true
  194. // - name: repo
  195. // in: path
  196. // description: name of the repo
  197. // type: string
  198. // required: true
  199. // - name: id
  200. // in: path
  201. // description: id of the label to delete
  202. // type: integer
  203. // format: int64
  204. // required: true
  205. // responses:
  206. // "204":
  207. // "$ref": "#/responses/empty"
  208. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  209. ctx.Error(500, "DeleteLabel", err)
  210. return
  211. }
  212. ctx.Status(204)
  213. }