issue_label.go 3.7 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(ctx *context.APIContext) {
  12. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  13. if err != nil {
  14. if errors.IsIssueNotExist(err) {
  15. ctx.Status(404)
  16. } else {
  17. ctx.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. ctx.JSON(200, &apiLabels)
  26. }
  27. func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
  28. if !ctx.Repo.IsWriter() {
  29. ctx.Status(403)
  30. return
  31. }
  32. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  33. if err != nil {
  34. if errors.IsIssueNotExist(err) {
  35. ctx.Status(404)
  36. } else {
  37. ctx.Error(500, "GetIssueByIndex", err)
  38. }
  39. return
  40. }
  41. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  42. if err != nil {
  43. ctx.Error(500, "GetLabelsInRepoByIDs", err)
  44. return
  45. }
  46. if err = issue.AddLabels(ctx.User, labels); err != nil {
  47. ctx.Error(500, "AddLabels", err)
  48. return
  49. }
  50. labels, err = models.GetLabelsByIssueID(issue.ID)
  51. if err != nil {
  52. ctx.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. ctx.JSON(200, &apiLabels)
  60. }
  61. func DeleteIssueLabel(ctx *context.APIContext) {
  62. if !ctx.Repo.IsWriter() {
  63. ctx.Status(403)
  64. return
  65. }
  66. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  67. if err != nil {
  68. if errors.IsIssueNotExist(err) {
  69. ctx.Status(404)
  70. } else {
  71. ctx.Error(500, "GetIssueByIndex", err)
  72. }
  73. return
  74. }
  75. label, err := models.GetLabelOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  76. if err != nil {
  77. if models.IsErrLabelNotExist(err) {
  78. ctx.Error(422, "", err)
  79. } else {
  80. ctx.Error(500, "GetLabelInRepoByID", err)
  81. }
  82. return
  83. }
  84. if err := models.DeleteIssueLabel(issue, label); err != nil {
  85. ctx.Error(500, "DeleteIssueLabel", err)
  86. return
  87. }
  88. ctx.Status(204)
  89. }
  90. func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
  91. if !ctx.Repo.IsWriter() {
  92. ctx.Status(403)
  93. return
  94. }
  95. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  96. if err != nil {
  97. if errors.IsIssueNotExist(err) {
  98. ctx.Status(404)
  99. } else {
  100. ctx.Error(500, "GetIssueByIndex", err)
  101. }
  102. return
  103. }
  104. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  105. if err != nil {
  106. ctx.Error(500, "GetLabelsInRepoByIDs", err)
  107. return
  108. }
  109. if err := issue.ReplaceLabels(labels); err != nil {
  110. ctx.Error(500, "ReplaceLabels", err)
  111. return
  112. }
  113. labels, err = models.GetLabelsByIssueID(issue.ID)
  114. if err != nil {
  115. ctx.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. ctx.JSON(200, &apiLabels)
  123. }
  124. func ClearIssueLabels(ctx *context.APIContext) {
  125. if !ctx.Repo.IsWriter() {
  126. ctx.Status(403)
  127. return
  128. }
  129. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  130. if err != nil {
  131. if errors.IsIssueNotExist(err) {
  132. ctx.Status(404)
  133. } else {
  134. ctx.Error(500, "GetIssueByIndex", err)
  135. }
  136. return
  137. }
  138. if err := issue.ClearLabels(ctx.User); err != nil {
  139. ctx.Error(500, "ClearLabels", err)
  140. return
  141. }
  142. ctx.Status(204)
  143. }