You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.9 KiB

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