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

Add regex_match Jinja filter #1556

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Add `regex_match` Jinja filter ([1556](https://github.com/grafana/oncall/pull/1556))

## v1.1.38 (2023-03-14)

### Added
Expand Down
1 change: 1 addition & 0 deletions docs/sources/alert-behavior/alert-templates/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,4 @@ Built-in functions:
- `iso8601_to_time` - converts time from iso8601 (`2015-02-17T18:30:20.000Z`) to datetime
- `datetimeformat` - converts time from datetime to the given format (`%H:%M / %d-%m-%Y` by default)
- `regex_replace` - performs a regex find and replace
- `regex_match` - performs a regex match, returns `True` or `False`. Usage example: `{{ payload.ruleName | regex_match(".*") }}`
7 changes: 7 additions & 0 deletions engine/common/jinja_templater/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ def regex_replace(value, find, replace):
return re.sub(find, replace, value)
except (ValueError, AttributeError, TypeError):
return None


def regex_match(pattern, value):
try:
return bool(re.match(value, pattern))
except (ValueError, AttributeError, TypeError):
return None
3 changes: 2 additions & 1 deletion engine/common/jinja_templater/jinja_template_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from jinja2.exceptions import SecurityError
from jinja2.sandbox import SandboxedEnvironment

from .filters import datetimeformat, iso8601_to_time, regex_replace, to_pretty_json
from .filters import datetimeformat, iso8601_to_time, regex_match, regex_replace, to_pretty_json


def raise_security_exception(name):
Expand All @@ -18,3 +18,4 @@ def raise_security_exception(name):
jinja_template_env.globals["time"] = timezone.now
jinja_template_env.globals["range"] = lambda *args: raise_security_exception("range")
jinja_template_env.filters["regex_replace"] = regex_replace
jinja_template_env.filters["regex_match"] = regex_match
12 changes: 12 additions & 0 deletions engine/common/tests/test_apply_jinja_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ def test_apply_jinja_template():
assert payload == result


def test_apply_jinja_template_regex_match():
payload = {"name": "test"}

assert apply_jinja_template("{{ payload.name | regex_match('.*') }}", payload) == "True"
assert apply_jinja_template("{{ payload.name | regex_match('tes') }}", payload) == "True"
assert apply_jinja_template("{{ payload.name | regex_match('test1') }}", payload) == "False"

# Check that exception is raised when regex is invalid
with pytest.raises(JinjaTemplateError):
apply_jinja_template("{{ payload.name | regex_match('*') }}", payload)


def test_apply_jinja_template_bad_syntax_error():
with pytest.raises(JinjaTemplateError):
apply_jinja_template("{{%", payload={})
Expand Down