From aebd98b8f905daed7f78799a6dddfcb9b3400afe Mon Sep 17 00:00:00 2001 From: Michael Derynck Date: Fri, 9 Feb 2024 12:03:25 -0700 Subject: [PATCH] Change email templating to not wrap user defined templates (#3862) # What this PR does Change email notification templates to not wrap the user template in a fixed body. Info has been moved to a footer. The title now also matches the template from the user exactly and will only get a default message if the user template result is empty. Example: ![Screenshot from 2024-02-08 12-16-38](https://github.com/grafana/oncall/assets/28077050/bb59b7a7-7ec8-4d9d-8aef-8aff012c74f7) ## Which issue(s) this PR fixes #3857 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) --- CHANGELOG.md | 1 + engine/apps/email/alert_rendering.py | 12 ++++---- .../email/templates/email_notification.html | 29 ++++++++----------- engine/apps/email/tests/test_notify_user.py | 2 +- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e195992..d735a7c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Allow mobile app to access escalation options endpoints @imtoori ([#3847](https://github.com/grafana/oncall/pull/3847)) - Enable templating for alert escalation mobile app push notifications by @joeyorlando ([#3845](https://github.com/grafana/oncall/pull/3845)) +- Change email notification template to not wrap user template @mderynck ([#3862](https://github.com/grafana/oncall/pull/3862)) ## v1.3.102 (2024-02-06) diff --git a/engine/apps/email/alert_rendering.py b/engine/apps/email/alert_rendering.py index c2897a88..4ac0eac0 100644 --- a/engine/apps/email/alert_rendering.py +++ b/engine/apps/email/alert_rendering.py @@ -39,17 +39,17 @@ def build_subject_and_message(alert_group, emails_left): content = render_to_string( "email_notification.html", { - "url": alert_group.slack_permalink or alert_group.web_link, - "title": str_or_backup(templated_alert.title, title_fallback), - "message": str_or_backup(message, ""), # not render message at all if smth goes wrong - "organization": alert_group.channel.organization.org_title, - "integration": emojize(alert_group.channel.short_name, language="alias"), + "message": str_or_backup(message, title_fallback), "limit_notification": emails_left <= 20, "emails_left": emails_left, + "url": alert_group.slack_permalink or alert_group.web_link, + "alert_group_number": str(alert_group.inside_organization_number), + "integration": emojize(alert_group.channel.short_name, language="alias"), + "instance_url": alert_group.channel.organization.grafana_url, }, ) title = str_or_backup(templated_alert.title, title_fallback) - subject = f"[{title}] You are invited to check an alert group".replace("\n", "") + subject = f"{title}".replace("\n", "") return subject, content diff --git a/engine/apps/email/templates/email_notification.html b/engine/apps/email/templates/email_notification.html index 86ade2bb..6418d6ab 100644 --- a/engine/apps/email/templates/email_notification.html +++ b/engine/apps/email/templates/email_notification.html @@ -1,28 +1,23 @@ -You are invited to check an alert group in Grafana OnCall! -

-Organization: {{ organization }} -
-Integration: {{ integration }} -
-Title: {{ title }} -{% if message %} -
-Message: -
{% autoescape off %} - {{ message }} +{{ message }} {% endautoescape %} -{% endif %} -
-Go to the alert group -
-Your Grafana OnCall + {% if limit_notification %}

{{ emails_left }} emails left for the organization today. Contact your admin. {% endif %} +
+Alert Group #{{ alert_group_number }} from {{ integration }} +| +{{ instance_url }} +| +Docs +

+ - Your Grafana OnCall +

+ {% now "H:i.u e"%} \ No newline at end of file diff --git a/engine/apps/email/tests/test_notify_user.py b/engine/apps/email/tests/test_notify_user.py index fd892e10..f33cdaaf 100644 --- a/engine/apps/email/tests/test_notify_user.py +++ b/engine/apps/email/tests/test_notify_user.py @@ -211,4 +211,4 @@ def test_subject_newlines_removed( make_alert(alert_group, raw_request_data={}) subject, _ = build_subject_and_message(alert_group, 1) - assert subject == "[testnewlines] You are invited to check an alert group" + assert subject == "testnewlines"