本站源代码
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.

112 line
2.9KB

  1. // Copyright 2019 The Gitea 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 issue
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/notification"
  9. api "code.gitea.io/gitea/modules/structs"
  10. )
  11. func sendLabelUpdatedWebhook(issue *models.Issue, doer *models.User) {
  12. var err error
  13. if err = issue.LoadRepo(); err != nil {
  14. log.Error("LoadRepo: %v", err)
  15. return
  16. }
  17. if err = issue.LoadPoster(); err != nil {
  18. log.Error("LoadPoster: %v", err)
  19. return
  20. }
  21. mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
  22. if issue.IsPull {
  23. if err = issue.LoadPullRequest(); err != nil {
  24. log.Error("loadPullRequest: %v", err)
  25. return
  26. }
  27. if err = issue.PullRequest.LoadIssue(); err != nil {
  28. log.Error("LoadIssue: %v", err)
  29. return
  30. }
  31. err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
  32. Action: api.HookIssueLabelUpdated,
  33. Index: issue.Index,
  34. PullRequest: issue.PullRequest.APIFormat(),
  35. Repository: issue.Repo.APIFormat(models.AccessModeNone),
  36. Sender: doer.APIFormat(),
  37. })
  38. } else {
  39. err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
  40. Action: api.HookIssueLabelUpdated,
  41. Index: issue.Index,
  42. Issue: issue.APIFormat(),
  43. Repository: issue.Repo.APIFormat(mode),
  44. Sender: doer.APIFormat(),
  45. })
  46. }
  47. if err != nil {
  48. log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  49. } else {
  50. go models.HookQueue.Add(issue.RepoID)
  51. }
  52. }
  53. // ClearLabels clears all of an issue's labels
  54. func ClearLabels(issue *models.Issue, doer *models.User) (err error) {
  55. if err = issue.ClearLabels(doer); err != nil {
  56. return
  57. }
  58. notification.NotifyIssueClearLabels(doer, issue)
  59. return nil
  60. }
  61. // AddLabel adds a new label to the issue.
  62. func AddLabel(issue *models.Issue, doer *models.User, label *models.Label) error {
  63. if err := models.NewIssueLabel(issue, label, doer); err != nil {
  64. return err
  65. }
  66. sendLabelUpdatedWebhook(issue, doer)
  67. return nil
  68. }
  69. // AddLabels adds a list of new labels to the issue.
  70. func AddLabels(issue *models.Issue, doer *models.User, labels []*models.Label) error {
  71. if err := models.NewIssueLabels(issue, labels, doer); err != nil {
  72. return err
  73. }
  74. sendLabelUpdatedWebhook(issue, doer)
  75. return nil
  76. }
  77. // RemoveLabel removes a label from issue by given ID.
  78. func RemoveLabel(issue *models.Issue, doer *models.User, label *models.Label) error {
  79. if err := issue.LoadRepo(); err != nil {
  80. return err
  81. }
  82. perm, err := models.GetUserRepoPermission(issue.Repo, doer)
  83. if err != nil {
  84. return err
  85. }
  86. if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
  87. return models.ErrLabelNotExist{}
  88. }
  89. if err := models.DeleteIssueLabel(issue, label, doer); err != nil {
  90. return err
  91. }
  92. sendLabelUpdatedWebhook(issue, doer)
  93. return nil
  94. }
上海开阖软件有限公司 沪ICP备12045867号-1