Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse web templates in other templates #1786

Merged
merged 6 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -168,13 +168,34 @@ 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
}
# 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(
Expand Down