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.

237 lines
5.4 KiB

  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "strconv"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // ListLabels list all the labels of a repository
  12. func ListLabels(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/labels issue issueListLabels
  14. // ---
  15. // summary: Get all of a repository's labels
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // responses:
  30. // "200":
  31. // "$ref": "#/responses/LabelList"
  32. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
  33. if err != nil {
  34. ctx.Error(500, "GetLabelsByRepoID", err)
  35. return
  36. }
  37. apiLabels := make([]*api.Label, len(labels))
  38. for i := range labels {
  39. apiLabels[i] = labels[i].APIFormat()
  40. }
  41. ctx.JSON(200, &apiLabels)
  42. }
  43. // GetLabel get label by repository and label id
  44. func GetLabel(ctx *context.APIContext) {
  45. // swagger:operation GET /repos/{owner}/{repo}/labels/{id} issue issueGetLabel
  46. // ---
  47. // summary: Get a single label
  48. // produces:
  49. // - application/json
  50. // parameters:
  51. // - name: owner
  52. // in: path
  53. // description: owner of the repo
  54. // type: string
  55. // required: true
  56. // - name: repo
  57. // in: path
  58. // description: name of the repo
  59. // type: string
  60. // required: true
  61. // - name: id
  62. // in: path
  63. // description: id of the label to get
  64. // type: integer
  65. // required: true
  66. // responses:
  67. // "200":
  68. // "$ref": "#/responses/Label"
  69. var (
  70. label *models.Label
  71. err error
  72. )
  73. strID := ctx.Params(":id")
  74. if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
  75. label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
  76. } else {
  77. label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
  78. }
  79. if err != nil {
  80. if models.IsErrLabelNotExist(err) {
  81. ctx.Status(404)
  82. } else {
  83. ctx.Error(500, "GetLabelByRepoID", err)
  84. }
  85. return
  86. }
  87. ctx.JSON(200, label.APIFormat())
  88. }
  89. // CreateLabel create a label for a repository
  90. func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
  91. // swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel
  92. // ---
  93. // summary: Create a label
  94. // consumes:
  95. // - application/json
  96. // produces:
  97. // - application/json
  98. // parameters:
  99. // - name: owner
  100. // in: path
  101. // description: owner of the repo
  102. // type: string
  103. // required: true
  104. // - name: repo
  105. // in: path
  106. // description: name of the repo
  107. // type: string
  108. // required: true
  109. // - name: body
  110. // in: body
  111. // schema:
  112. // "$ref": "#/definitions/CreateLabelOption"
  113. // responses:
  114. // "201":
  115. // "$ref": "#/responses/Label"
  116. if !ctx.Repo.IsWriter() {
  117. ctx.Status(403)
  118. return
  119. }
  120. label := &models.Label{
  121. Name: form.Name,
  122. Color: form.Color,
  123. RepoID: ctx.Repo.Repository.ID,
  124. }
  125. if err := models.NewLabel(label); err != nil {
  126. ctx.Error(500, "NewLabel", err)
  127. return
  128. }
  129. ctx.JSON(201, 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. // required: true
  156. // - name: body
  157. // in: body
  158. // schema:
  159. // "$ref": "#/definitions/EditLabelOption"
  160. // responses:
  161. // "200":
  162. // "$ref": "#/responses/Label"
  163. if !ctx.Repo.IsWriter() {
  164. ctx.Status(403)
  165. return
  166. }
  167. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  168. if err != nil {
  169. if models.IsErrLabelNotExist(err) {
  170. ctx.Status(404)
  171. } else {
  172. ctx.Error(500, "GetLabelByRepoID", err)
  173. }
  174. return
  175. }
  176. if form.Name != nil {
  177. label.Name = *form.Name
  178. }
  179. if form.Color != nil {
  180. label.Color = *form.Color
  181. }
  182. if err := models.UpdateLabel(label); err != nil {
  183. ctx.ServerError("UpdateLabel", err)
  184. return
  185. }
  186. ctx.JSON(200, 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. // required: true
  209. // responses:
  210. // "204":
  211. // "$ref": "#/responses/empty"
  212. if !ctx.Repo.IsWriter() {
  213. ctx.Status(403)
  214. return
  215. }
  216. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  217. ctx.Error(500, "DeleteLabel", err)
  218. return
  219. }
  220. ctx.Status(204)
  221. }