From 405162178b7f5048c1d25e89f16f2ac94b78b17e Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 18 Apr 2024 12:11:12 +0200 Subject: [PATCH] services: Use proper Message-IDs for release mails When sending notification emails about a release, use a properly formatted, RFC-compliant message id, rather than the release's HTML URL wrapped in angle brackets (which would not be compliant). Fixes #3105. Signed-off-by: Gergely Nagy (cherry picked from commit b0c0167c5487102ef7bb3d917a4d159f83e86739) --- services/mailer/mail.go | 4 ++++ services/mailer/mail_release.go | 4 ++-- services/mailer/mailer_test.go | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 26aebd2583..86bd40ff29 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -389,6 +389,10 @@ func createReference(issue *issues_model.Issue, comment *issues_model.Comment, a return fmt.Sprintf("<%s/%s/%d%s@%s>", issue.Repo.FullName(), path, issue.Index, extra, setting.Domain) } +func createMessageIDForRelease(rel *repo_model.Release) string { + return fmt.Sprintf("<%s/releases/%d@%s>", rel.Repo.FullName(), rel.ID, setting.Domain) +} + func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient *user_model.User) map[string]string { repo := ctx.Issue.Repo diff --git a/services/mailer/mail_release.go b/services/mailer/mail_release.go index 6682774a04..2b0e7cfdc0 100644 --- a/services/mailer/mail_release.go +++ b/services/mailer/mail_release.go @@ -86,11 +86,11 @@ func mailNewRelease(ctx context.Context, lang string, tos []string, rel *repo_mo msgs := make([]*Message, 0, len(tos)) publisherName := rel.Publisher.DisplayName() - relURL := "<" + rel.HTMLURL() + ">" + msgID := createMessageIDForRelease(rel) for _, to := range tos { msg := NewMessageFrom(to, publisherName, setting.MailService.FromEmail, subject, mailBody.String()) msg.Info = subject - msg.SetHeader("Message-ID", relURL) + msg.SetHeader("Message-ID", msgID) msgs = append(msgs, msg) } diff --git a/services/mailer/mailer_test.go b/services/mailer/mailer_test.go index 375ca35daa..253454e89c 100644 --- a/services/mailer/mailer_test.go +++ b/services/mailer/mailer_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -36,3 +37,17 @@ func TestGenerateMessageID(t *testing.T) { gm = m.ToMessage() assert.Equal(t, "", gm.GetHeader("Message-ID")[0]) } + +func TestGenerateMessageIDForRelease(t *testing.T) { + setting.Domain = "localhost" + + rel := repo_model.Release{ + ID: 42, + Repo: &repo_model.Repository{ + OwnerName: "test", + Name: "tag-test", + }, + } + m := createMessageIDForRelease(&rel) + assert.Equal(t, "", m) +}