label.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. api "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/context"
  9. "github.com/gogits/gogs/routers/api/v1/convert"
  10. )
  11. func ListLabels(ctx *context.APIContext) {
  12. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID)
  13. if err != nil {
  14. ctx.Error(500, "GetLabelsByRepoID", err)
  15. return
  16. }
  17. apiLabels := make([]*api.Label, len(labels))
  18. for i := range labels {
  19. apiLabels[i] = convert.ToLabel(labels[i])
  20. }
  21. ctx.JSON(200, &apiLabels)
  22. }
  23. func GetLabel(ctx *context.APIContext) {
  24. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  25. if err != nil {
  26. if models.IsErrLabelNotExist(err) {
  27. ctx.Status(404)
  28. } else {
  29. ctx.Error(500, "GetLabelByRepoID", err)
  30. }
  31. return
  32. }
  33. ctx.JSON(200, convert.ToLabel(label))
  34. }
  35. func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
  36. if !ctx.Repo.IsWriter() {
  37. ctx.Status(403)
  38. return
  39. }
  40. label := &models.Label{
  41. Name: form.Name,
  42. Color: form.Color,
  43. RepoID: ctx.Repo.Repository.ID,
  44. }
  45. if err := models.NewLabel(label); err != nil {
  46. ctx.Error(500, "NewLabel", err)
  47. return
  48. }
  49. ctx.JSON(201, convert.ToLabel(label))
  50. }
  51. func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
  52. if !ctx.Repo.IsWriter() {
  53. ctx.Status(403)
  54. return
  55. }
  56. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  57. if err != nil {
  58. if models.IsErrLabelNotExist(err) {
  59. ctx.Status(404)
  60. } else {
  61. ctx.Error(500, "GetLabelByRepoID", err)
  62. }
  63. return
  64. }
  65. if form.Name != nil {
  66. label.Name = *form.Name
  67. }
  68. if form.Color != nil {
  69. label.Color = *form.Color
  70. }
  71. if err := models.UpdateLabel(label); err != nil {
  72. ctx.Handle(500, "UpdateLabel", err)
  73. return
  74. }
  75. ctx.JSON(200, convert.ToLabel(label))
  76. }
  77. func DeleteLabel(ctx *context.APIContext) {
  78. if !ctx.Repo.IsWriter() {
  79. ctx.Status(403)
  80. return
  81. }
  82. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  83. ctx.Error(500, "DeleteLabel", err)
  84. return
  85. }
  86. ctx.Status(204)
  87. }