issue_label.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/models/errors"
  9. "github.com/gogits/gogs/pkg/context"
  10. )
  11. func ListIssueLabels(c *context.APIContext) {
  12. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  13. if err != nil {
  14. if errors.IsIssueNotExist(err) {
  15. c.Status(404)
  16. } else {
  17. c.Error(500, "GetIssueByIndex", err)
  18. }
  19. return
  20. }
  21. apiLabels := make([]*api.Label, len(issue.Labels))
  22. for i := range issue.Labels {
  23. apiLabels[i] = issue.Labels[i].APIFormat()
  24. }
  25. c.JSON(200, &apiLabels)
  26. }
  27. func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
  28. if !c.Repo.IsWriter() {
  29. c.Status(403)
  30. return
  31. }
  32. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  33. if err != nil {
  34. if errors.IsIssueNotExist(err) {
  35. c.Status(404)
  36. } else {
  37. c.Error(500, "GetIssueByIndex", err)
  38. }
  39. return
  40. }
  41. labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
  42. if err != nil {
  43. c.Error(500, "GetLabelsInRepoByIDs", err)
  44. return
  45. }
  46. if err = issue.AddLabels(c.User, labels); err != nil {
  47. c.Error(500, "AddLabels", err)
  48. return
  49. }
  50. labels, err = models.GetLabelsByIssueID(issue.ID)
  51. if err != nil {
  52. c.Error(500, "GetLabelsByIssueID", err)
  53. return
  54. }
  55. apiLabels := make([]*api.Label, len(labels))
  56. for i := range labels {
  57. apiLabels[i] = issue.Labels[i].APIFormat()
  58. }
  59. c.JSON(200, &apiLabels)
  60. }
  61. func DeleteIssueLabel(c *context.APIContext) {
  62. if !c.Repo.IsWriter() {
  63. c.Status(403)
  64. return
  65. }
  66. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  67. if err != nil {
  68. if errors.IsIssueNotExist(err) {
  69. c.Status(404)
  70. } else {
  71. c.Error(500, "GetIssueByIndex", err)
  72. }
  73. return
  74. }
  75. label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  76. if err != nil {
  77. if models.IsErrLabelNotExist(err) {
  78. c.Error(422, "", err)
  79. } else {
  80. c.Error(500, "GetLabelInRepoByID", err)
  81. }
  82. return
  83. }
  84. if err := models.DeleteIssueLabel(issue, label); err != nil {
  85. c.Error(500, "DeleteIssueLabel", err)
  86. return
  87. }
  88. c.Status(204)
  89. }
  90. func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
  91. if !c.Repo.IsWriter() {
  92. c.Status(403)
  93. return
  94. }
  95. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  96. if err != nil {
  97. if errors.IsIssueNotExist(err) {
  98. c.Status(404)
  99. } else {
  100. c.Error(500, "GetIssueByIndex", err)
  101. }
  102. return
  103. }
  104. labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
  105. if err != nil {
  106. c.Error(500, "GetLabelsInRepoByIDs", err)
  107. return
  108. }
  109. if err := issue.ReplaceLabels(labels); err != nil {
  110. c.Error(500, "ReplaceLabels", err)
  111. return
  112. }
  113. labels, err = models.GetLabelsByIssueID(issue.ID)
  114. if err != nil {
  115. c.Error(500, "GetLabelsByIssueID", err)
  116. return
  117. }
  118. apiLabels := make([]*api.Label, len(labels))
  119. for i := range labels {
  120. apiLabels[i] = issue.Labels[i].APIFormat()
  121. }
  122. c.JSON(200, &apiLabels)
  123. }
  124. func ClearIssueLabels(c *context.APIContext) {
  125. if !c.Repo.IsWriter() {
  126. c.Status(403)
  127. return
  128. }
  129. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  130. if err != nil {
  131. if errors.IsIssueNotExist(err) {
  132. c.Status(404)
  133. } else {
  134. c.Error(500, "GetIssueByIndex", err)
  135. }
  136. return
  137. }
  138. if err := issue.ClearLabels(c.User); err != nil {
  139. c.Error(500, "ClearLabels", err)
  140. return
  141. }
  142. c.Status(204)
  143. }