Send notifications for mentions in pulls, issues, (code-)comments (#14218)

Fixes #14187: mention handling extracted from email notification code
Fixes #14013: add notification for mentions in pull request code comments
Fixes #13450: Not receiving any emails with setting "Only Email on Mention"
This commit is contained in:
Jimmy Praet 2021-01-02 18:04:02 +01:00 committed by GitHub
parent ac88b0ee83
commit e6acce649b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 205 additions and 88 deletions

View file

@ -22,8 +22,11 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
if err != nil {
return nil, err
}
notification.NotifyCreateIssueComment(doer, repo, issue, comment)
mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content)
if err != nil {
return nil, err
}
notification.NotifyCreateIssueComment(doer, repo, issue, comment, mentions)
return comment, nil
}

View file

@ -23,7 +23,12 @@ func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uu
}
}
notification.NotifyNewIssue(issue)
mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), issue.Poster, issue.Content)
if err != nil {
return err
}
notification.NotifyNewIssue(issue, mentions)
return nil
}

View file

@ -5,31 +5,20 @@
package mailer
import (
"fmt"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/references"
)
// MailParticipantsComment sends new comment emails to repository watchers
// and mentioned people.
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue) error {
return mailParticipantsComment(models.DefaultDBContext(), c, opType, issue)
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) error {
return mailParticipantsComment(c, opType, issue, mentions)
}
func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType models.ActionType, issue *models.Issue) (err error) {
rawMentions := references.FindAllMentionsMarkdown(c.Content)
userMentions, err := issue.ResolveMentionsByVisibility(ctx, c.Poster, rawMentions)
if err != nil {
return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", c.IssueID, err)
}
if err = models.UpdateIssueMentions(ctx, c.IssueID, userMentions); err != nil {
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
}
mentions := make([]int64, len(userMentions))
for i, u := range userMentions {
mentions[i] = u.ID
func mailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) (err error) {
mentionedIDs := make([]int64, len(mentions))
for i, u := range mentions {
mentionedIDs[i] = u.ID
}
if err = mailIssueCommentToParticipants(
&mailCommentContext{
@ -38,8 +27,29 @@ func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType mod
ActionType: opType,
Content: c.Content,
Comment: c,
}, mentions); err != nil {
}, mentionedIDs); err != nil {
log.Error("mailIssueCommentToParticipants: %v", err)
}
return nil
}
// MailMentionsComment sends email to users mentioned in a code comment
func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) (err error) {
mentionedIDs := make([]int64, len(mentions))
for i, u := range mentions {
mentionedIDs[i] = u.ID
}
visited := make(map[int64]bool, len(mentions)+1)
visited[c.Poster.ID] = true
if err = mailIssueCommentBatch(
&mailCommentContext{
Issue: pr.Issue,
Doer: c.Poster,
ActionType: models.ActionCommentPull,
Content: c.Content,
Comment: c,
}, mentionedIDs, visited, true); err != nil {
log.Error("mailIssueCommentBatch: %v", err)
}
return nil
}

View file

@ -9,7 +9,6 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/references"
)
func fallbackMailSubject(issue *models.Issue) string {
@ -80,6 +79,12 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e
// Avoid mailing the doer
visited[ctx.Doer.ID] = true
// =========== Mentions ===========
if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil {
return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err)
}
// Avoid mailing explicit unwatched
ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, false)
if err != nil {
@ -93,11 +98,6 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e
return fmt.Errorf("mailIssueCommentBatch(): %v", err)
}
// =========== Mentions ===========
if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil {
return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err)
}
return nil
}
@ -145,22 +145,14 @@ func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, visited map[int
// MailParticipants sends new issue thread created emails to repository watchers
// and mentioned people.
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType) error {
return mailParticipants(models.DefaultDBContext(), issue, doer, opType)
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error {
return mailParticipants(issue, doer, opType, mentions)
}
func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.User, opType models.ActionType) (err error) {
rawMentions := references.FindAllMentionsMarkdown(issue.Content)
userMentions, err := issue.ResolveMentionsByVisibility(ctx, doer, rawMentions)
if err != nil {
return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", issue.ID, err)
}
if err = models.UpdateIssueMentions(ctx, issue.ID, userMentions); err != nil {
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
}
mentions := make([]int64, len(userMentions))
for i, u := range userMentions {
mentions[i] = u.ID
func mailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) (err error) {
mentionedIDs := make([]int64, len(mentions))
for i, u := range mentions {
mentionedIDs[i] = u.ID
}
if err = mailIssueCommentToParticipants(
&mailCommentContext{
@ -169,7 +161,7 @@ func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.Us
ActionType: opType,
Content: issue.Content,
Comment: nil,
}, mentions); err != nil {
}, mentionedIDs); err != nil {
log.Error("mailIssueCommentToParticipants: %v", err)
}
return nil

View file

@ -53,7 +53,12 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
return err
}
notification.NotifyNewPullRequest(pr)
mentions, err := pull.FindAndUpdateIssueMentions(models.DefaultDBContext(), pull.Poster, pull.Content)
if err != nil {
return err
}
notification.NotifyNewPullRequest(pr, mentions)
// add first push codes comment
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())

View file

@ -57,7 +57,12 @@ func CreateCodeComment(doer *models.User, gitRepo *git.Repository, issue *models
return nil, err
}
notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment)
mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content)
if err != nil {
return nil, err
}
notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment, mentions)
return comment, nil
}
@ -226,7 +231,25 @@ func SubmitReview(doer *models.User, gitRepo *git.Repository, issue *models.Issu
return nil, nil, err
}
notification.NotifyPullRequestReview(pr, review, comm)
ctx := models.DefaultDBContext()
mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, comm.Content)
if err != nil {
return nil, nil, err
}
notification.NotifyPullRequestReview(pr, review, comm, mentions)
for _, lines := range review.CodeComments {
for _, comments := range lines {
for _, codeComment := range comments {
mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, codeComment.Content)
if err != nil {
return nil, nil, err
}
notification.NotifyPullRequestCodeComment(pr, codeComment, mentions)
}
}
}
return review, comm, nil
}