issue_label.go 3.0 KB

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