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.

notify.go 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mailer
  4. import (
  5. "context"
  6. "fmt"
  7. activities_model "code.gitea.io/gitea/models/activities"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/log"
  12. notify_service "code.gitea.io/gitea/services/notify"
  13. )
  14. type mailNotifier struct {
  15. notify_service.NullNotifier
  16. }
  17. var _ notify_service.Notifier = &mailNotifier{}
  18. // NewNotifier create a new mailNotifier notifier
  19. func NewNotifier() notify_service.Notifier {
  20. return &mailNotifier{}
  21. }
  22. func (m *mailNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
  23. issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
  24. ) {
  25. var act activities_model.ActionType
  26. if comment.Type == issues_model.CommentTypeClose {
  27. act = activities_model.ActionCloseIssue
  28. } else if comment.Type == issues_model.CommentTypeReopen {
  29. act = activities_model.ActionReopenIssue
  30. } else if comment.Type == issues_model.CommentTypeComment {
  31. act = activities_model.ActionCommentIssue
  32. } else if comment.Type == issues_model.CommentTypeCode {
  33. act = activities_model.ActionCommentIssue
  34. } else if comment.Type == issues_model.CommentTypePullRequestPush {
  35. act = 0
  36. }
  37. if err := MailParticipantsComment(ctx, comment, act, issue, mentions); err != nil {
  38. log.Error("MailParticipantsComment: %v", err)
  39. }
  40. }
  41. func (m *mailNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
  42. if err := MailParticipants(ctx, issue, issue.Poster, activities_model.ActionCreateIssue, mentions); err != nil {
  43. log.Error("MailParticipants: %v", err)
  44. }
  45. }
  46. func (m *mailNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
  47. var actionType activities_model.ActionType
  48. if issue.IsPull {
  49. if isClosed {
  50. actionType = activities_model.ActionClosePullRequest
  51. } else {
  52. actionType = activities_model.ActionReopenPullRequest
  53. }
  54. } else {
  55. if isClosed {
  56. actionType = activities_model.ActionCloseIssue
  57. } else {
  58. actionType = activities_model.ActionReopenIssue
  59. }
  60. }
  61. if err := MailParticipants(ctx, issue, doer, actionType, nil); err != nil {
  62. log.Error("MailParticipants: %v", err)
  63. }
  64. }
  65. func (m *mailNotifier) IssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
  66. if err := issue.LoadPullRequest(ctx); err != nil {
  67. log.Error("issue.LoadPullRequest: %v", err)
  68. return
  69. }
  70. if issue.IsPull && issues_model.HasWorkInProgressPrefix(oldTitle) && !issue.PullRequest.IsWorkInProgress() {
  71. if err := MailParticipants(ctx, issue, doer, activities_model.ActionPullRequestReadyForReview, nil); err != nil {
  72. log.Error("MailParticipants: %v", err)
  73. }
  74. }
  75. }
  76. func (m *mailNotifier) NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) {
  77. if err := MailParticipants(ctx, pr.Issue, pr.Issue.Poster, activities_model.ActionCreatePullRequest, mentions); err != nil {
  78. log.Error("MailParticipants: %v", err)
  79. }
  80. }
  81. func (m *mailNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, r *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
  82. var act activities_model.ActionType
  83. if comment.Type == issues_model.CommentTypeClose {
  84. act = activities_model.ActionCloseIssue
  85. } else if comment.Type == issues_model.CommentTypeReopen {
  86. act = activities_model.ActionReopenIssue
  87. } else if comment.Type == issues_model.CommentTypeComment {
  88. act = activities_model.ActionCommentPull
  89. }
  90. if err := MailParticipantsComment(ctx, comment, act, pr.Issue, mentions); err != nil {
  91. log.Error("MailParticipantsComment: %v", err)
  92. }
  93. }
  94. func (m *mailNotifier) PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User) {
  95. if err := MailMentionsComment(ctx, pr, comment, mentions); err != nil {
  96. log.Error("MailMentionsComment: %v", err)
  97. }
  98. }
  99. func (m *mailNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
  100. // mail only sent to added assignees and not self-assignee
  101. if !removed && doer.ID != assignee.ID && assignee.EmailNotifications() != user_model.EmailNotificationsDisabled {
  102. ct := fmt.Sprintf("Assigned #%d.", issue.Index)
  103. if err := SendIssueAssignedMail(ctx, issue, doer, ct, comment, []*user_model.User{assignee}); err != nil {
  104. log.Error("Error in SendIssueAssignedMail for issue[%d] to assignee[%d]: %v", issue.ID, assignee.ID, err)
  105. }
  106. }
  107. }
  108. func (m *mailNotifier) PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
  109. if isRequest && doer.ID != reviewer.ID && reviewer.EmailNotifications() != user_model.EmailNotificationsDisabled {
  110. ct := fmt.Sprintf("Requested to review %s.", issue.HTMLURL())
  111. if err := SendIssueAssignedMail(ctx, issue, doer, ct, comment, []*user_model.User{reviewer}); err != nil {
  112. log.Error("Error in SendIssueAssignedMail for issue[%d] to reviewer[%d]: %v", issue.ID, reviewer.ID, err)
  113. }
  114. }
  115. }
  116. func (m *mailNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  117. if err := pr.LoadIssue(ctx); err != nil {
  118. log.Error("LoadIssue: %v", err)
  119. return
  120. }
  121. if err := MailParticipants(ctx, pr.Issue, doer, activities_model.ActionMergePullRequest, nil); err != nil {
  122. log.Error("MailParticipants: %v", err)
  123. }
  124. }
  125. func (m *mailNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  126. if err := pr.LoadIssue(ctx); err != nil {
  127. log.Error("pr.LoadIssue: %v", err)
  128. return
  129. }
  130. if err := MailParticipants(ctx, pr.Issue, doer, activities_model.ActionAutoMergePullRequest, nil); err != nil {
  131. log.Error("MailParticipants: %v", err)
  132. }
  133. }
  134. func (m *mailNotifier) PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) {
  135. var err error
  136. if err = comment.LoadIssue(ctx); err != nil {
  137. log.Error("comment.LoadIssue: %v", err)
  138. return
  139. }
  140. if err = comment.Issue.LoadRepo(ctx); err != nil {
  141. log.Error("comment.Issue.LoadRepo: %v", err)
  142. return
  143. }
  144. if err = comment.Issue.LoadPullRequest(ctx); err != nil {
  145. log.Error("comment.Issue.LoadPullRequest: %v", err)
  146. return
  147. }
  148. if err = comment.Issue.PullRequest.LoadBaseRepo(ctx); err != nil {
  149. log.Error("comment.Issue.PullRequest.LoadBaseRepo: %v", err)
  150. return
  151. }
  152. if err := comment.LoadPushCommits(ctx); err != nil {
  153. log.Error("comment.LoadPushCommits: %v", err)
  154. }
  155. m.CreateIssueComment(ctx, doer, comment.Issue.Repo, comment.Issue, comment, nil)
  156. }
  157. func (m *mailNotifier) PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
  158. if err := comment.Review.LoadReviewer(ctx); err != nil {
  159. log.Error("Error in PullReviewDismiss while loading reviewer for issue[%d], review[%d] and reviewer[%d]: %v", review.Issue.ID, comment.Review.ID, comment.Review.ReviewerID, err)
  160. }
  161. if err := MailParticipantsComment(ctx, comment, activities_model.ActionPullReviewDismissed, review.Issue, nil); err != nil {
  162. log.Error("MailParticipantsComment: %v", err)
  163. }
  164. }
  165. func (m *mailNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) {
  166. if err := rel.LoadAttributes(ctx); err != nil {
  167. log.Error("LoadAttributes: %v", err)
  168. return
  169. }
  170. if rel.IsDraft || rel.IsPrerelease {
  171. return
  172. }
  173. MailNewRelease(ctx, rel)
  174. }
  175. func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) {
  176. if err := SendRepoTransferNotifyMail(ctx, doer, newOwner, repo); err != nil {
  177. log.Error("SendRepoTransferNotifyMail: %v", err)
  178. }
  179. }