forgejo/services/mailer/mail_actions.go
Earl Warren 32e8610b20 fix(email): actions notification template confuses branch with PR (#8448)
When a mail notification is sent because of a failed pull request run,
the body will show:

Branch: #661 (f57df45)

where #661 is the number of the pull request and not the branch. This
is because run.PrettyRef() is used and has a misleading special case
returning a PR number instead of a ref.

Remove the textual description as it can easily be discovered from the
run web page linked in the mail.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [ ] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8448
Reviewed-by: Christopher Besch <mail@chris-besch.com>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-07-09 09:43:33 +02:00

87 lines
2.2 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package mailer
import (
"bytes"
actions_model "forgejo.org/models/actions"
user_model "forgejo.org/models/user"
"forgejo.org/modules/base"
"forgejo.org/modules/setting"
"forgejo.org/modules/translation"
)
const (
tplActionNowDone base.TplName = "actions/now_done"
)
var MailActionRun = mailActionRun // make it mockable
func mailActionRun(run *actions_model.ActionRun, priorStatus actions_model.Status, lastRun *actions_model.ActionRun) error {
if setting.MailService == nil {
// No mail service configured
return nil
}
if !run.NotifyEmail {
return nil
}
user := run.TriggerUser
// this happens e.g. when this is a scheduled run
if user.IsSystem() {
user = run.Repo.Owner
}
if user.IsSystem() || user.Email == "" {
return nil
}
if user.EmailNotificationsPreference == user_model.EmailNotificationsDisabled {
return nil
}
return sendMailActionRun(user, run, priorStatus, lastRun)
}
func sendMailActionRun(to *user_model.User, run *actions_model.ActionRun, priorStatus actions_model.Status, lastRun *actions_model.ActionRun) error {
var (
locale = translation.NewLocale(to.Language)
content bytes.Buffer
)
var subject string
if run.Status.IsSuccess() {
subject = locale.TrString("mail.actions.successful_run_after_failure_subject", run.Title, run.Repo.FullName())
} else {
subject = locale.TrString("mail.actions.not_successful_run", run.Title, run.Repo.FullName())
}
commitSHA := run.CommitSHA
if len(commitSHA) > 7 {
commitSHA = commitSHA[:7]
}
data := map[string]any{
"locale": locale,
"Link": run.HTMLURL(),
"Subject": subject,
"Language": locale.Language(),
"RepoFullName": run.Repo.FullName(),
"Run": run,
"TriggerUserLink": run.TriggerUser.HTMLURL(),
"LastRun": lastRun,
"PriorStatus": priorStatus,
"CommitSHA": commitSHA,
"IsSuccess": run.Status.IsSuccess(),
}
if err := bodyTemplates.ExecuteTemplate(&content, string(tplActionNowDone), data); err != nil {
return err
}
msg := NewMessage(to.EmailTo(), subject, content.String())
msg.Info = subject
SendAsync(msg)
return nil
}