From 76dbc8cb71b8c2f6cbed9f6891c24a371997b2db Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Tue, 18 Apr 2023 20:59:13 +0800 Subject: [PATCH 1/4] Reuse web templates in other templates --- .../templaters/alert_templater.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py index 176e82b3c4..1e205d41f8 100644 --- a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py +++ b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py @@ -76,7 +76,7 @@ def __init__(self, alert): self.alert = alert self.slack_formatter = SlackFormatter(alert.group.channel.organization) self.template_manager = TemplateLoader() - self.incident_id = self.alert.group.inside_organization_number + self.alert_group_id = self.alert.group.inside_organization_number self.link = self.alert.group.web_link def render(self): @@ -168,13 +168,31 @@ def _render_attribute_with_template(self, attr, data, channel, templated_alert): attr_template = self.template_manager.get_attr_template(attr, channel, self._render_for()) if attr_template is not None: context = { - "grafana_oncall_incident_id": self.incident_id, - "grafana_oncall_link": self.link, "integration_name": channel.verbal_name, "source_link": templated_alert.source_link, - "amixr_incident_id": self.incident_id, # TODO: decide on variable names - "amixr_link": self.link, # TODO: decide on variable names + "grafana_oncall_alert_group_id": self.alert_group_id, + "grafana_oncall_incident_id": self.alert_group_id, # Keep for backward compatibility + "amixr_incident_id": self.alert_group_id, # Keep for backward compatibility + "grafana_oncall_link": self.link, + "amixr_link": self.link, # Keep for backward compatibility } + if self._render_for() != "web": + added_context = { + "web_title_template": apply_jinja_template( + self.template_manager.get_attr_template("title", channel, "web"), + data, + result_length_limit=settings.JINJA_RESULT_TITLE_MAX_LENGTH, + **context, + ), + "web_message_template": apply_jinja_template( + self.template_manager.get_attr_template("message", channel, "web"), data, **context + ), + "web_image_url_template": apply_jinja_template( + self.template_manager.get_attr_template("image_url", channel, "web"), data, **context + ), + } + context = {**context, **added_context} + try: if attr == "title": return apply_jinja_template( From 1c6e348ea4a20060d88445d11f4d18f427665202 Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Wed, 19 Apr 2023 10:48:30 +0800 Subject: [PATCH 2/4] Add rendered web templates to other templates as available variables --- .../templaters/alert_templater.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py index 1e205d41f8..81401c96de 100644 --- a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py +++ b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py @@ -176,19 +176,22 @@ def _render_attribute_with_template(self, attr, data, channel, templated_alert): "grafana_oncall_link": self.link, "amixr_link": self.link, # Keep for backward compatibility } - if self._render_for() != "web": + # Hardcoding, as AlertWebTemplater.RENDER_FOR_WEB cause circular import + render_for_web = "web" + # Propagate rendered web templates to the other templates + if self._render_for() != render_for_web: added_context = { - "web_title_template": apply_jinja_template( - self.template_manager.get_attr_template("title", channel, "web"), + "web_title": apply_jinja_template( + self.template_manager.get_attr_template("title", channel, render_for_web), data, result_length_limit=settings.JINJA_RESULT_TITLE_MAX_LENGTH, **context, ), - "web_message_template": apply_jinja_template( - self.template_manager.get_attr_template("message", channel, "web"), data, **context + "web_message": apply_jinja_template( + self.template_manager.get_attr_template("message", channel, render_for_web), data, **context ), - "web_image_url_template": apply_jinja_template( - self.template_manager.get_attr_template("image_url", channel, "web"), data, **context + "web_image_url": apply_jinja_template( + self.template_manager.get_attr_template("image_url", channel, render_for_web), data, **context ), } context = {**context, **added_context} From 1affd9160cf0ac119623ec4e4d8d23e53fdbf746 Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Mon, 24 Apr 2023 14:58:37 +0800 Subject: [PATCH 3/4] Fix bug --- .../templaters/alert_templater.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py index 81401c96de..8740a9ab29 100644 --- a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py +++ b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py @@ -179,21 +179,21 @@ def _render_attribute_with_template(self, attr, data, channel, templated_alert): # Hardcoding, as AlertWebTemplater.RENDER_FOR_WEB cause circular import render_for_web = "web" # Propagate rendered web templates to the other templates + added_context = {} if self._render_for() != render_for_web: - added_context = { - "web_title": apply_jinja_template( - self.template_manager.get_attr_template("title", channel, render_for_web), - data, - result_length_limit=settings.JINJA_RESULT_TITLE_MAX_LENGTH, - **context, - ), - "web_message": apply_jinja_template( - self.template_manager.get_attr_template("message", channel, render_for_web), data, **context - ), - "web_image_url": apply_jinja_template( - self.template_manager.get_attr_template("image_url", channel, render_for_web), data, **context - ), - } + for attr in ["title", "message", "image_url"]: + added_attr_template = self.template_manager.get_attr_template(attr, channel, render_for_web) + if added_attr_template is not None: + result_length_limit = ( + settings.JINJA_RESULT_TITLE_MAX_LENGTH + if attr == "title" + else settings.JINJA_RESULT_MAX_LENGTH + ) + added_context[f"web_{attr}"] = apply_jinja_template( + added_attr_template, data, result_length_limit=result_length_limit, **context + ) + else: + added_context[f"web_{attr}"] = f"web_{attr} is not set" context = {**context, **added_context} try: From 87a231bc833ec090730257ab47fafa576d067dde Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Mon, 24 Apr 2023 15:07:08 +0800 Subject: [PATCH 4/4] Changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae247d7ca5..b66ec082c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Add web_title, web_message and web_image_url attributes to templates ([1786](https://github.com/grafana/oncall/pull/1786)) + ## v1.2.13 (2023-04-18) ### Changed