label.go 2.1 KB

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