Skip to content

Commit e0a6afb

Browse files
committed
Prevent merge messages from being sorted to the top of email chains
Gitea will currrently resend the same message-id for the closed/merged/reopened messages for issues. This will cause the merged message to leap to the top of an email chain and become out of sync. This PR adds specific suffices for these actions. Fix go-gitea#18560 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 76e3111 commit e0a6afb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

services/mailer/mail.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515
"strings"
1616
texttmpl "text/template"
17+
"time"
1718

1819
"code.gitea.io/gitea/models"
1920
repo_model "code.gitea.io/gitea/models/repo"
@@ -298,13 +299,15 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
298299
}
299300

300301
// Make sure to compose independent messages to avoid leaking user emails
302+
msgID := createReference(ctx.Issue, ctx.Comment, ctx.ActionType)
303+
reference := createReference(ctx.Issue, nil, models.ActionType(0))
304+
301305
msgs := make([]*Message, 0, len(recipients))
302306
for _, recipient := range recipients {
303307
msg := NewMessageFrom([]string{recipient.Email}, ctx.Doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
304308
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
305309

306-
msg.SetHeader("Message-ID", "<"+createReference(ctx.Issue, ctx.Comment)+">")
307-
reference := createReference(ctx.Issue, nil)
310+
msg.SetHeader("Message-ID", "<"+msgID+">")
308311
msg.SetHeader("In-Reply-To", "<"+reference+">")
309312
msg.SetHeader("References", "<"+reference+">")
310313

@@ -318,7 +321,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
318321
return msgs, nil
319322
}
320323

321-
func createReference(issue *models.Issue, comment *models.Comment) string {
324+
func createReference(issue *models.Issue, comment *models.Comment, actionType models.ActionType) string {
322325
var path string
323326
if issue.IsPull {
324327
path = "pulls"
@@ -329,6 +332,17 @@ func createReference(issue *models.Issue, comment *models.Comment) string {
329332
var extra string
330333
if comment != nil {
331334
extra = fmt.Sprintf("/comment/%d", comment.ID)
335+
} else {
336+
switch actionType {
337+
case models.ActionCloseIssue, models.ActionClosePullRequest:
338+
extra = fmt.Sprintf("/close/%d", time.Now().UnixNano()/1e6)
339+
case models.ActionReopenIssue, models.ActionReopenPullRequest:
340+
extra = fmt.Sprintf("/reopen/%d", time.Now().UnixNano()/1e6)
341+
case models.ActionMergePullRequest:
342+
extra = fmt.Sprintf("/merge/%d", time.Now().UnixNano()/1e6)
343+
case models.ActionPullRequestReadyForReview:
344+
extra = fmt.Sprintf("/ready/%d", time.Now().UnixNano()/1e6)
345+
}
332346
}
333347

334348
return fmt.Sprintf("%s/%s/%d%s@%s", issue.Repo.FullName(), path, issue.Index, extra, setting.Domain)

0 commit comments

Comments
 (0)