issue_label.go 3.7 KB

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