issue_label.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/models/errors"
  10. "github.com/gogs/gogs/pkg/context"
  11. )
  12. func ListIssueLabels(c *context.APIContext) {
  13. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  14. if err != nil {
  15. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  16. return
  17. }
  18. apiLabels := make([]*api.Label, len(issue.Labels))
  19. for i := range issue.Labels {
  20. apiLabels[i] = issue.Labels[i].APIFormat()
  21. }
  22. c.JSONSuccess(&apiLabels)
  23. }
  24. func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
  25. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  26. if err != nil {
  27. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  28. return
  29. }
  30. labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
  31. if err != nil {
  32. c.ServerError("GetLabelsInRepoByIDs", err)
  33. return
  34. }
  35. if err = issue.AddLabels(c.User, labels); err != nil {
  36. c.ServerError("AddLabels", err)
  37. return
  38. }
  39. labels, err = models.GetLabelsByIssueID(issue.ID)
  40. if err != nil {
  41. c.ServerError("GetLabelsByIssueID", err)
  42. return
  43. }
  44. apiLabels := make([]*api.Label, len(labels))
  45. for i := range labels {
  46. apiLabels[i] = issue.Labels[i].APIFormat()
  47. }
  48. c.JSONSuccess(&apiLabels)
  49. }
  50. func DeleteIssueLabel(c *context.APIContext) {
  51. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  52. if err != nil {
  53. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  54. return
  55. }
  56. label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  57. if err != nil {
  58. if models.IsErrLabelNotExist(err) {
  59. c.Error(http.StatusUnprocessableEntity, "", err)
  60. } else {
  61. c.ServerError("GetLabelInRepoByID", err)
  62. }
  63. return
  64. }
  65. if err := models.DeleteIssueLabel(issue, label); err != nil {
  66. c.ServerError("DeleteIssueLabel", err)
  67. return
  68. }
  69. c.NoContent()
  70. }
  71. func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
  72. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  73. if err != nil {
  74. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  75. return
  76. }
  77. labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
  78. if err != nil {
  79. c.ServerError("GetLabelsInRepoByIDs", err)
  80. return
  81. }
  82. if err := issue.ReplaceLabels(labels); err != nil {
  83. c.ServerError("ReplaceLabels", err)
  84. return
  85. }
  86. labels, err = models.GetLabelsByIssueID(issue.ID)
  87. if err != nil {
  88. c.ServerError("GetLabelsByIssueID", err)
  89. return
  90. }
  91. apiLabels := make([]*api.Label, len(labels))
  92. for i := range labels {
  93. apiLabels[i] = issue.Labels[i].APIFormat()
  94. }
  95. c.JSONSuccess(&apiLabels)
  96. }
  97. func ClearIssueLabels(c *context.APIContext) {
  98. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  99. if err != nil {
  100. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  101. return
  102. }
  103. if err := issue.ClearLabels(c.User); err != nil {
  104. c.ServerError("ClearLabels", err)
  105. return
  106. }
  107. c.NoContent()
  108. }