label.go 2.3 KB

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