issue_label.go 2.9 KB

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