Skip to content

Commit 747fbfc

Browse files
authored
Add regex_match Jinja filter (#1556)
# What this PR does Adds a new `regex_match` filter to Jinja environment. ## Which issue(s) this PR fixes This should be useful on its own, and also helpful for #1555. ## Checklist - [x] Tests updated - [x] Documentation added - [x] `CHANGELOG.md` updated
1 parent bd12d38 commit 747fbfc

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- Add `regex_match` Jinja filter ([1556](https://github.com/grafana/oncall/pull/1556))
13+
1014
### Changed
1115

1216
- Allow passing `null` as a value for `escalation_chain` when creating routes via the public API ([1557](https://github.com/grafana/oncall/pull/1557))

docs/sources/alert-behavior/alert-templates/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,4 @@ Built-in functions:
176176
- `iso8601_to_time` - converts time from iso8601 (`2015-02-17T18:30:20.000Z`) to datetime
177177
- `datetimeformat` - converts time from datetime to the given format (`%H:%M / %d-%m-%Y` by default)
178178
- `regex_replace` - performs a regex find and replace
179+
- `regex_match` - performs a regex match, returns `True` or `False`. Usage example: `{{ payload.ruleName | regex_match(".*") }}`

engine/common/jinja_templater/filters.py

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ def regex_replace(value, find, replace):
3030
return re.sub(find, replace, value)
3131
except (ValueError, AttributeError, TypeError):
3232
return None
33+
34+
35+
def regex_match(pattern, value):
36+
try:
37+
return bool(re.match(value, pattern))
38+
except (ValueError, AttributeError, TypeError):
39+
return None

engine/common/jinja_templater/jinja_template_env.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from jinja2.exceptions import SecurityError
44
from jinja2.sandbox import SandboxedEnvironment
55

6-
from .filters import datetimeformat, iso8601_to_time, regex_replace, to_pretty_json
6+
from .filters import datetimeformat, iso8601_to_time, regex_match, regex_replace, to_pretty_json
77

88

99
def raise_security_exception(name):
@@ -18,3 +18,4 @@ def raise_security_exception(name):
1818
jinja_template_env.globals["time"] = timezone.now
1919
jinja_template_env.globals["range"] = lambda *args: raise_security_exception("range")
2020
jinja_template_env.filters["regex_replace"] = regex_replace
21+
jinja_template_env.filters["regex_match"] = regex_match

engine/common/tests/test_apply_jinja_template.py

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ def test_apply_jinja_template():
1414
assert payload == result
1515

1616

17+
def test_apply_jinja_template_regex_match():
18+
payload = {"name": "test"}
19+
20+
assert apply_jinja_template("{{ payload.name | regex_match('.*') }}", payload) == "True"
21+
assert apply_jinja_template("{{ payload.name | regex_match('tes') }}", payload) == "True"
22+
assert apply_jinja_template("{{ payload.name | regex_match('test1') }}", payload) == "False"
23+
24+
# Check that exception is raised when regex is invalid
25+
with pytest.raises(JinjaTemplateError):
26+
apply_jinja_template("{{ payload.name | regex_match('*') }}", payload)
27+
28+
1729
def test_apply_jinja_template_bad_syntax_error():
1830
with pytest.raises(JinjaTemplateError):
1931
apply_jinja_template("{{%", payload={})

0 commit comments

Comments
 (0)