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.

action.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 action
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/notification/base"
  11. )
  12. type actionNotifier struct {
  13. base.NullNotifier
  14. }
  15. var (
  16. _ base.Notifier = &actionNotifier{}
  17. )
  18. // NewNotifier create a new actionNotifier notifier
  19. func NewNotifier() base.Notifier {
  20. return &actionNotifier{}
  21. }
  22. func (a *actionNotifier) NotifyNewIssue(issue *models.Issue) {
  23. if err := issue.LoadPoster(); err != nil {
  24. log.Error("issue.LoadPoster: %v", err)
  25. return
  26. }
  27. if err := issue.LoadRepo(); err != nil {
  28. log.Error("issue.LoadRepo: %v", err)
  29. return
  30. }
  31. repo := issue.Repo
  32. if err := models.NotifyWatchers(&models.Action{
  33. ActUserID: issue.Poster.ID,
  34. ActUser: issue.Poster,
  35. OpType: models.ActionCreateIssue,
  36. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  37. RepoID: repo.ID,
  38. Repo: repo,
  39. IsPrivate: repo.IsPrivate,
  40. }); err != nil {
  41. log.Error("NotifyWatchers: %v", err)
  42. }
  43. }
  44. func (a *actionNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
  45. if err := pull.LoadIssue(); err != nil {
  46. log.Error("pull.LoadIssue: %v", err)
  47. return
  48. }
  49. if err := pull.Issue.LoadRepo(); err != nil {
  50. log.Error("pull.Issue.LoadRepo: %v", err)
  51. return
  52. }
  53. if err := pull.Issue.LoadPoster(); err != nil {
  54. log.Error("pull.Issue.LoadPoster: %v", err)
  55. return
  56. }
  57. if err := models.NotifyWatchers(&models.Action{
  58. ActUserID: pull.Issue.Poster.ID,
  59. ActUser: pull.Issue.Poster,
  60. OpType: models.ActionCreatePullRequest,
  61. Content: fmt.Sprintf("%d|%s", pull.Issue.Index, pull.Issue.Title),
  62. RepoID: pull.Issue.Repo.ID,
  63. Repo: pull.Issue.Repo,
  64. IsPrivate: pull.Issue.Repo.IsPrivate,
  65. }); err != nil {
  66. log.Error("NotifyWatchers: %v", err)
  67. }
  68. }
  69. func (a *actionNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
  70. if err := models.NotifyWatchers(&models.Action{
  71. ActUserID: doer.ID,
  72. ActUser: doer,
  73. OpType: models.ActionRenameRepo,
  74. RepoID: repo.ID,
  75. Repo: repo,
  76. IsPrivate: repo.IsPrivate,
  77. Content: oldName,
  78. }); err != nil {
  79. log.Error("notify watchers: %v", err)
  80. } else {
  81. log.Trace("action.renameRepoAction: %s/%s", doer.Name, repo.Name)
  82. }
  83. }
  84. func (a *actionNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  85. if err := models.NotifyWatchers(&models.Action{
  86. ActUserID: doer.ID,
  87. ActUser: doer,
  88. OpType: models.ActionCreateRepo,
  89. RepoID: repo.ID,
  90. Repo: repo,
  91. IsPrivate: repo.IsPrivate,
  92. }); err != nil {
  93. log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
  94. }
  95. }
  96. func (a *actionNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
  97. if err := models.NotifyWatchers(&models.Action{
  98. ActUserID: doer.ID,
  99. ActUser: doer,
  100. OpType: models.ActionCreateRepo,
  101. RepoID: repo.ID,
  102. Repo: repo,
  103. IsPrivate: repo.IsPrivate,
  104. }); err != nil {
  105. log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
  106. }
  107. }
  108. func (a *actionNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
  109. if err := review.LoadReviewer(); err != nil {
  110. log.Error("LoadReviewer '%d/%d': %v", review.ID, review.ReviewerID, err)
  111. return
  112. }
  113. if err := review.LoadCodeComments(); err != nil {
  114. log.Error("LoadCodeComments '%d/%d': %v", review.Reviewer.ID, review.ID, err)
  115. return
  116. }
  117. var actions = make([]*models.Action, 0, 10)
  118. for _, lines := range review.CodeComments {
  119. for _, comments := range lines {
  120. for _, comm := range comments {
  121. actions = append(actions, &models.Action{
  122. ActUserID: review.Reviewer.ID,
  123. ActUser: review.Reviewer,
  124. Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comm.Content, "\n")[0]),
  125. OpType: models.ActionCommentIssue,
  126. RepoID: review.Issue.RepoID,
  127. Repo: review.Issue.Repo,
  128. IsPrivate: review.Issue.Repo.IsPrivate,
  129. Comment: comm,
  130. CommentID: comm.ID,
  131. })
  132. }
  133. }
  134. }
  135. if strings.TrimSpace(comment.Content) != "" {
  136. actions = append(actions, &models.Action{
  137. ActUserID: review.Reviewer.ID,
  138. ActUser: review.Reviewer,
  139. Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comment.Content, "\n")[0]),
  140. OpType: models.ActionCommentIssue,
  141. RepoID: review.Issue.RepoID,
  142. Repo: review.Issue.Repo,
  143. IsPrivate: review.Issue.Repo.IsPrivate,
  144. Comment: comment,
  145. CommentID: comment.ID,
  146. })
  147. }
  148. if err := models.NotifyWatchersActions(actions); err != nil {
  149. log.Error("notify watchers '%d/%d': %v", review.Reviewer.ID, review.Issue.RepoID, err)
  150. }
  151. }