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)
This commit is contained in:
Ildar Iskhakov 2023-04-24 15:44:39 +08:00 committed by GitHub
parent 6e61643750
commit e6ebec1a17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View file

@ -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

View file

@ -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(