label.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitlab.com/gitote/com"
  11. api "gitlab.com/gitote/go-gitote-client"
  12. )
  13. func ListLabels(c *context.APIContext) {
  14. labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
  15. if err != nil {
  16. c.Error(500, "GetLabelsByRepoID", err)
  17. return
  18. }
  19. apiLabels := make([]*api.Label, len(labels))
  20. for i := range labels {
  21. apiLabels[i] = labels[i].APIFormat()
  22. }
  23. c.JSON(200, &apiLabels)
  24. }
  25. func GetLabel(c *context.APIContext) {
  26. var label *models.Label
  27. var err error
  28. idStr := c.Params(":id")
  29. if id := com.StrTo(idStr).MustInt64(); id > 0 {
  30. label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
  31. } else {
  32. label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
  33. }
  34. if err != nil {
  35. if models.IsErrLabelNotExist(err) {
  36. c.Status(404)
  37. } else {
  38. c.Error(500, "GetLabelByRepoID", err)
  39. }
  40. return
  41. }
  42. c.JSON(200, label.APIFormat())
  43. }
  44. func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
  45. if !c.Repo.IsWriter() {
  46. c.Status(403)
  47. return
  48. }
  49. label := &models.Label{
  50. Name: form.Name,
  51. Color: form.Color,
  52. RepoID: c.Repo.Repository.ID,
  53. }
  54. if err := models.NewLabels(label); err != nil {
  55. c.Error(500, "NewLabel", err)
  56. return
  57. }
  58. c.JSON(201, label.APIFormat())
  59. }
  60. func EditLabel(c *context.APIContext, form api.EditLabelOption) {
  61. if !c.Repo.IsWriter() {
  62. c.Status(403)
  63. return
  64. }
  65. label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  66. if err != nil {
  67. if models.IsErrLabelNotExist(err) {
  68. c.Status(404)
  69. } else {
  70. c.Error(500, "GetLabelByRepoID", err)
  71. }
  72. return
  73. }
  74. if form.Name != nil {
  75. label.Name = *form.Name
  76. }
  77. if form.Color != nil {
  78. label.Color = *form.Color
  79. }
  80. if err := models.UpdateLabel(label); err != nil {
  81. c.Handle(500, "UpdateLabel", err)
  82. return
  83. }
  84. c.JSON(200, label.APIFormat())
  85. }
  86. func DeleteLabel(c *context.APIContext) {
  87. if !c.Repo.IsWriter() {
  88. c.Status(403)
  89. return
  90. }
  91. if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  92. c.Error(500, "DeleteLabel", err)
  93. return
  94. }
  95. c.Status(204)
  96. }