From e6ebec1a17228ac2944550101afe08150f2ae927 Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Mon, 24 Apr 2023 15:44:39 +0800 Subject: [PATCH] Reuse web templates in other templates (#1786) # What this PR does ## Which issue(s) this PR fixes ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [ ] Documentation added (or `pr:no public docs` PR label added if not required) - [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) --- CHANGELOG.md | 1 + .../templaters/alert_templater.py | 31 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90a86ddd..4a5170cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add helm chart support for twilio existing secrets by @atownsend247 ([#1435](https://github.com/grafana/oncall/pull/1435)) +- Add web_title, web_message and web_image_url attributes to templates ([1786](https://github.com/grafana/oncall/pull/1786)) ### Changed diff --git a/engine/apps/alerts/incident_appearance/templaters/alert_templater.py b/engine/apps/alerts/incident_appearance/templaters/alert_templater.py index 176e82b3..8740a9ab 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 @@ class AlertTemplater(ABC): 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,34 @@ class AlertTemplater(ABC): 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 } + # 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: + 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: if attr == "title": return apply_jinja_template(