Skip to content

Commit

Permalink
Update regex to jinja route conversion to correctly escape double quo…
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb authored Jul 19, 2024
1 parent 87d7982 commit 1491e28
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion engine/apps/api/serializers/channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def get_filtering_term_as_jinja2(self, obj):
elif obj.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_REGEX:
# Four curly braces will result in two curly braces in the final string
# rf"..." is a raw f string, to keep original filtering_term
return rf'{{{{ payload | json_dumps | regex_search("{obj.filtering_term}") }}}}'
escaped_quotes = obj.filtering_term.replace('"', '\\"') if obj.filtering_term else ""
return rf'{{{{ payload | json_dumps | regex_search("{escaped_quotes}") }}}}'
elif obj.filtering_labels and obj.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_LABELS:
# required labels
labels = [
Expand Down
6 changes: 4 additions & 2 deletions engine/apps/api/tests/test_channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,10 @@ def test_channel_filter_convert_from_regex_to_jinja2(

make_channel_filter(alert_receive_channel, is_default=True)

# r"..." used to keep this string as raw string
regex_filtering_term = r"\".*\": \"This alert was sent by user for demonstration purposes\""
# regex as set by Terraform/API (not a raw string, but a string with escaped characters)
# see ChannelFilterSerializer in apps.public_api.serializers.routes.py
regex_filtering_term = '".*": "This alert was sent by user for demonstration purposes"'
# r"..." to define the expected jinja2 template translation
final_filtering_term = r'{{ payload | json_dumps | regex_search("\".*\": \"This alert was sent by user for demonstration purposes\"") }}'
payload = {"description": "This alert was sent by user for demonstration purposes"}

Expand Down

0 comments on commit 1491e28

Please sign in to comment.