Skip to content

Commit

Permalink
feat: render_without_execution (#3027)
Browse files Browse the repository at this point in the history
Signed-off-by: Matvey Kukuy <[email protected]>
  • Loading branch information
Matvey-Kuk authored Jan 15, 2025
1 parent 9f00d37 commit b127e1d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
22 changes: 22 additions & 0 deletions docs/workflows/syntax/functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ steps:
```

### `raw_render_without_execution`

**Description:** Renders the string without execution of keep instructions inside this string.
**Example:**

```yaml
consts:
yaml: |
keep.is_business_hours(2024-03-25T14:00:00Z)
steps:
- name: example-step
provider:
type: mock
with:
message: "raw_render_without_execution(My yaml is: {{ yaml }}!)"
```

Will output:
```
My yaml is: keep.is_business_hours(2024-03-25T14:00:00Z)!
```

## List and Dictionary Functions

### `first`
Expand Down
10 changes: 9 additions & 1 deletion keep/iohandler/iohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,15 @@ def parse(self, string, safe=False, default=""):

# Now, extract the token if exists -
parsed_string = copy.copy(string)
tokens = self.extract_keep_functions(parsed_string)

if string.startswith("raw_render_without_execution(") and string.endswith(")"):
tokens = []
string = string.replace("raw_render_without_execution(", "", 1)
string = string[::-1].replace(")", "", 1)[::-1] # Remove the last ')'
parsed_string = copy.copy(string)
else:
tokens = self.extract_keep_functions(parsed_string)

if len(tokens) == 0:
return parsed_string
elif len(tokens) == 1:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from keep.api.core.dependencies import SINGLE_TENANT_UUID
from keep.api.models.alert import AlertStatus
from keep.api.models.db.alert import ActionType
from keep.iohandler.iohandler import IOHandler


@pytest.mark.parametrize(
Expand Down Expand Up @@ -757,3 +758,25 @@ def test_is_business_hours_string_input_with_timezone():
assert (
functions.is_business_hours("2024-03-25T20:00:00Z", timezone=paris_tz) == False
)


def test_render_without_execution(mocked_context_manager):
"""
Test rendering a template without executing it's internal keep functions.
"""
template = "My yaml is: {{ yaml }}!"
context = {"yaml": "keep.is_business_hours(2024-03-25T14:00:00Z)"}
mocked_context_manager.get_full_context.return_value = context
iohandler = IOHandler(mocked_context_manager)
with pytest.raises(Exception):
iohandler.render(
template,
safe=True,
)

template = "raw_render_without_execution(My yaml is: {{ yaml }}!)"
rendered = iohandler.render(
template,
safe=True,
)
assert rendered == "My yaml is: keep.is_business_hours(2024-03-25T14:00:00Z)!"

0 comments on commit b127e1d

Please sign in to comment.