Add parse_json template function (#3929)
This commit is contained in:
parent
0271f3c32d
commit
2a46e4ff84
5 changed files with 25 additions and 0 deletions
|
|
@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- Support prescribed labels ([#3848](https://github.com/grafana/oncall/pull/3848))
|
||||
- Add status change trigger type to webhooks ([#3920](https://github.com/grafana/oncall/pull/3920))
|
||||
- Add `EMAIL_USE_SSL` environment setting by @WoodyWoodsta ([#3911](https://github.com/grafana/oncall/pull/3911))
|
||||
- Add `parse_json` Jinja2 template filter by @KarimDarwish ([#3929](https://github.com/grafana/oncall/pull/3929))
|
||||
|
||||
### Fixed
|
||||
|
||||
|
|
|
|||
|
|
@ -227,3 +227,5 @@ Built-in functions:
|
|||
- Usage example: `{{ payload.ruleName | regex_match(".*") }}`
|
||||
- `b64decode` - performs a base64 string decode
|
||||
- Usage example: `{{ payload.data | b64decode }}`
|
||||
- `parse_json` - parses a given json string to an object
|
||||
- Usage example: `{{ (payload.data | b64decode | parse_json).name }}`
|
||||
|
|
|
|||
|
|
@ -68,3 +68,10 @@ def b64decode(value):
|
|||
return base64.b64decode(value).decode("utf-8")
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def parse_json(value):
|
||||
try:
|
||||
return json.loads(value)
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from .filters import (
|
|||
datetimeformat_as_timezone,
|
||||
iso8601_to_time,
|
||||
json_dumps,
|
||||
parse_json,
|
||||
regex_match,
|
||||
regex_replace,
|
||||
regex_search,
|
||||
|
|
@ -33,3 +34,4 @@ jinja_template_env.filters["regex_match"] = regex_match
|
|||
jinja_template_env.filters["regex_search"] = regex_search
|
||||
jinja_template_env.filters["json_dumps"] = json_dumps
|
||||
jinja_template_env.filters["b64decode"] = b64decode
|
||||
jinja_template_env.filters["parse_json"] = parse_json
|
||||
|
|
|
|||
|
|
@ -187,3 +187,16 @@ def test_apply_jinja_template_to_alert_payload_and_labels(mock_apply_jinja_templ
|
|||
)
|
||||
def test_templated_value_is_truthy(value, expected):
|
||||
assert templated_value_is_truthy(value) == expected
|
||||
|
||||
|
||||
def test_apply_jinja_template_parse_json():
|
||||
payload = {"message": base64.b64encode(b'{"name": "test"}').decode("utf-8")}
|
||||
expected_name = "test"
|
||||
|
||||
assert (
|
||||
apply_jinja_template(
|
||||
"{{ (payload.message | b64decode | parse_json).name }}",
|
||||
payload,
|
||||
)
|
||||
== expected_name
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue