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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // format: int64
  66. // required: true
  67. // responses:
  68. // "200":
  69. // "$ref": "#/responses/Label"
  70. var (
  71. label *models.Label
  72. err error
  73. )
  74. strID := ctx.Params(":id")
  75. if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
  76. label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
  77. } else {
  78. label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
  79. }
  80. if err != nil {
  81. if models.IsErrLabelNotExist(err) {
  82. ctx.Status(404)
  83. } else {
  84. ctx.Error(500, "GetLabelByRepoID", err)
  85. }
  86. return
  87. }
  88. ctx.JSON(200, label.APIFormat())
  89. }
  90. // CreateLabel create a label for a repository
  91. func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
  92. // swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel
  93. // ---
  94. // summary: Create a label
  95. // consumes:
  96. // - application/json
  97. // produces:
  98. // - application/json
  99. // parameters:
  100. // - name: owner
  101. // in: path
  102. // description: owner of the repo
  103. // type: string
  104. // required: true
  105. // - name: repo
  106. // in: path
  107. // description: name of the repo
  108. // type: string
  109. // required: true
  110. // - name: body
  111. // in: body
  112. // schema:
  113. // "$ref": "#/definitions/CreateLabelOption"
  114. // responses:
  115. // "201":
  116. // "$ref": "#/responses/Label"
  117. if !ctx.Repo.IsWriter() {
  118. ctx.Status(403)
  119. return
  120. }
  121. label := &models.Label{
  122. Name: form.Name,
  123. Color: form.Color,
  124. RepoID: ctx.Repo.Repository.ID,
  125. }
  126. if err := models.NewLabel(label); err != nil {
  127. ctx.Error(500, "NewLabel", err)
  128. return
  129. }
  130. ctx.JSON(201, label.APIFormat())
  131. }
  132. // EditLabel modify a label for a repository
  133. func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
  134. // swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel
  135. // ---
  136. // summary: Update a label
  137. // consumes:
  138. // - application/json
  139. // produces:
  140. // - application/json
  141. // parameters:
  142. // - name: owner
  143. // in: path
  144. // description: owner of the repo
  145. // type: string
  146. // required: true
  147. // - name: repo
  148. // in: path
  149. // description: name of the repo
  150. // type: string
  151. // required: true
  152. // - name: id
  153. // in: path
  154. // description: id of the label to edit
  155. // type: integer
  156. // format: int64
  157. // required: true
  158. // - name: body
  159. // in: body
  160. // schema:
  161. // "$ref": "#/definitions/EditLabelOption"
  162. // responses:
  163. // "200":
  164. // "$ref": "#/responses/Label"
  165. if !ctx.Repo.IsWriter() {
  166. ctx.Status(403)
  167. return
  168. }
  169. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  170. if err != nil {
  171. if models.IsErrLabelNotExist(err) {
  172. ctx.Status(404)
  173. } else {
  174. ctx.Error(500, "GetLabelByRepoID", err)
  175. }
  176. return
  177. }
  178. if form.Name != nil {
  179. label.Name = *form.Name
  180. }
  181. if form.Color != nil {
  182. label.Color = *form.Color
  183. }
  184. if err := models.UpdateLabel(label); err != nil {
  185. ctx.ServerError("UpdateLabel", err)
  186. return
  187. }
  188. ctx.JSON(200, label.APIFormat())
  189. }
  190. // DeleteLabel delete a label for a repository
  191. func DeleteLabel(ctx *context.APIContext) {
  192. // swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel
  193. // ---
  194. // summary: Delete a label
  195. // parameters:
  196. // - name: owner
  197. // in: path
  198. // description: owner of the repo
  199. // type: string
  200. // required: true
  201. // - name: repo
  202. // in: path
  203. // description: name of the repo
  204. // type: string
  205. // required: true
  206. // - name: id
  207. // in: path
  208. // description: id of the label to delete
  209. // type: integer
  210. // format: int64
  211. // required: true
  212. // responses:
  213. // "204":
  214. // "$ref": "#/responses/empty"
  215. if !ctx.Repo.IsWriter() {
  216. ctx.Status(403)
  217. return
  218. }
  219. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  220. ctx.Error(500, "DeleteLabel", err)
  221. return
  222. }
  223. ctx.Status(204)
  224. }