label.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, 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. // ListLabels list all the labels of a repository
  14. func ListLabels(c *context.APIContext) {
  15. labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
  16. if err != nil {
  17. c.Error(500, "GetLabelsByRepoID", err)
  18. return
  19. }
  20. apiLabels := make([]*api.Label, len(labels))
  21. for i := range labels {
  22. apiLabels[i] = labels[i].APIFormat()
  23. }
  24. c.JSON(200, &apiLabels)
  25. }
  26. // GetLabel get label by repository and label id
  27. func GetLabel(c *context.APIContext) {
  28. var label *models.Label
  29. var err error
  30. idStr := c.Params(":id")
  31. if id := com.StrTo(idStr).MustInt64(); id > 0 {
  32. label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
  33. } else {
  34. label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
  35. }
  36. if err != nil {
  37. if models.IsErrLabelNotExist(err) {
  38. c.Status(404)
  39. } else {
  40. c.Error(500, "GetLabelByRepoID", err)
  41. }
  42. return
  43. }
  44. c.JSON(200, label.APIFormat())
  45. }
  46. // CreateLabel create a label for a repository
  47. func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
  48. if !c.Repo.IsWriter() {
  49. c.Status(403)
  50. return
  51. }
  52. label := &models.Label{
  53. Name: form.Name,
  54. Color: form.Color,
  55. RepoID: c.Repo.Repository.ID,
  56. }
  57. if err := models.NewLabels(label); err != nil {
  58. c.Error(500, "NewLabel", err)
  59. return
  60. }
  61. c.JSON(201, label.APIFormat())
  62. }
  63. // EditLabel modify a label for a repository
  64. func EditLabel(c *context.APIContext, form api.EditLabelOption) {
  65. if !c.Repo.IsWriter() {
  66. c.Status(403)
  67. return
  68. }
  69. label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  70. if err != nil {
  71. if models.IsErrLabelNotExist(err) {
  72. c.Status(404)
  73. } else {
  74. c.Error(500, "GetLabelByRepoID", err)
  75. }
  76. return
  77. }
  78. if form.Name != nil {
  79. label.Name = *form.Name
  80. }
  81. if form.Color != nil {
  82. label.Color = *form.Color
  83. }
  84. if err := models.UpdateLabel(label); err != nil {
  85. c.Handle(500, "UpdateLabel", err)
  86. return
  87. }
  88. c.JSON(200, label.APIFormat())
  89. }
  90. // DeleteLabel delete a label for a repository
  91. func DeleteLabel(c *context.APIContext) {
  92. if !c.Repo.IsWriter() {
  93. c.Status(403)
  94. return
  95. }
  96. if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  97. c.Error(500, "DeleteLabel", err)
  98. return
  99. }
  100. c.Status(204)
  101. }