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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 mail
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/notification/base"
  10. "code.gitea.io/gitea/services/mailer"
  11. )
  12. type mailNotifier struct {
  13. base.NullNotifier
  14. }
  15. var (
  16. _ base.Notifier = &mailNotifier{}
  17. )
  18. // NewNotifier create a new mailNotifier notifier
  19. func NewNotifier() base.Notifier {
  20. return &mailNotifier{}
  21. }
  22. func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
  23. issue *models.Issue, comment *models.Comment) {
  24. var act models.ActionType
  25. if comment.Type == models.CommentTypeClose {
  26. act = models.ActionCloseIssue
  27. } else if comment.Type == models.CommentTypeReopen {
  28. act = models.ActionReopenIssue
  29. } else if comment.Type == models.CommentTypeComment {
  30. act = models.ActionCommentIssue
  31. } else if comment.Type == models.CommentTypeCode {
  32. act = models.ActionCommentIssue
  33. } else if comment.Type == models.CommentTypePullPush {
  34. act = 0
  35. }
  36. if err := mailer.MailParticipantsComment(comment, act, issue); err != nil {
  37. log.Error("MailParticipantsComment: %v", err)
  38. }
  39. }
  40. func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) {
  41. if err := mailer.MailParticipants(issue, issue.Poster, models.ActionCreateIssue); err != nil {
  42. log.Error("MailParticipants: %v", err)
  43. }
  44. }
  45. func (m *mailNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
  46. var actionType models.ActionType
  47. issue.Content = ""
  48. if issue.IsPull {
  49. if isClosed {
  50. actionType = models.ActionClosePullRequest
  51. } else {
  52. actionType = models.ActionReopenPullRequest
  53. }
  54. } else {
  55. if isClosed {
  56. actionType = models.ActionCloseIssue
  57. } else {
  58. actionType = models.ActionReopenIssue
  59. }
  60. }
  61. if err := mailer.MailParticipants(issue, doer, actionType); err != nil {
  62. log.Error("MailParticipants: %v", err)
  63. }
  64. }
  65. func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
  66. if err := mailer.MailParticipants(pr.Issue, pr.Issue.Poster, models.ActionCreatePullRequest); err != nil {
  67. log.Error("MailParticipants: %v", err)
  68. }
  69. }
  70. func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, comment *models.Comment) {
  71. var act models.ActionType
  72. if comment.Type == models.CommentTypeClose {
  73. act = models.ActionCloseIssue
  74. } else if comment.Type == models.CommentTypeReopen {
  75. act = models.ActionReopenIssue
  76. } else if comment.Type == models.CommentTypeComment {
  77. act = models.ActionCommentPull
  78. }
  79. if err := mailer.MailParticipantsComment(comment, act, pr.Issue); err != nil {
  80. log.Error("MailParticipantsComment: %v", err)
  81. }
  82. }
  83. func (m *mailNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
  84. // mail only sent to added assignees and not self-assignee
  85. if !removed && doer.ID != assignee.ID && assignee.EmailNotifications() == models.EmailNotificationsEnabled {
  86. ct := fmt.Sprintf("Assigned #%d.", issue.Index)
  87. mailer.SendIssueAssignedMail(issue, doer, ct, comment, []string{assignee.Email})
  88. }
  89. }
  90. func (m *mailNotifier) NotifyPullReviewRequest(doer *models.User, issue *models.Issue, reviewer *models.User, isRequest bool, comment *models.Comment) {
  91. if isRequest && doer.ID != reviewer.ID && reviewer.EmailNotifications() == models.EmailNotificationsEnabled {
  92. ct := fmt.Sprintf("Requested to review #%d.", issue.Index)
  93. mailer.SendIssueAssignedMail(issue, doer, ct, comment, []string{reviewer.Email})
  94. }
  95. }
  96. func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  97. if err := pr.LoadIssue(); err != nil {
  98. log.Error("pr.LoadIssue: %v", err)
  99. return
  100. }
  101. pr.Issue.Content = ""
  102. if err := mailer.MailParticipants(pr.Issue, doer, models.ActionMergePullRequest); err != nil {
  103. log.Error("MailParticipants: %v", err)
  104. }
  105. }
  106. func (m *mailNotifier) NotifyPullRequestPushCommits(doer *models.User, pr *models.PullRequest, comment *models.Comment) {
  107. var err error
  108. if err = comment.LoadIssue(); err != nil {
  109. log.Error("comment.LoadIssue: %v", err)
  110. return
  111. }
  112. if err = comment.Issue.LoadRepo(); err != nil {
  113. log.Error("comment.Issue.LoadRepo: %v", err)
  114. return
  115. }
  116. if err = comment.Issue.LoadPullRequest(); err != nil {
  117. log.Error("comment.Issue.LoadPullRequest: %v", err)
  118. return
  119. }
  120. if err = comment.Issue.PullRequest.LoadBaseRepo(); err != nil {
  121. log.Error("comment.Issue.PullRequest.LoadBaseRepo: %v", err)
  122. return
  123. }
  124. if err := comment.LoadPushCommits(); err != nil {
  125. log.Error("comment.LoadPushCommits: %v", err)
  126. }
  127. comment.Content = ""
  128. m.NotifyCreateIssueComment(doer, comment.Issue.Repo, comment.Issue, comment)
  129. }