Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Fixed

Contributed by @momokuri-3

* Fix exception thrown if action parameter contains {{ or {% and no closing jinja characters. #5556

contributed by @guzzijones12

Added
~~~~~

Expand Down
33 changes: 19 additions & 14 deletions st2common/st2common/util/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import six
import networkx as nx

from jinja2 import meta
from jinja2 import meta, exceptions
from oslo_config import cfg
from st2common import log as logging
from st2common.util.config_loader import get_config
Expand Down Expand Up @@ -133,19 +133,24 @@ def _process(G, name, value):
) or jinja_utils.is_jinja_expression(complex_value_str)

if is_jinja_expr:
G.add_node(name, template=value)

template_ast = ENV.parse(value)
LOG.debug("Template ast: %s", template_ast)
# Dependencies of the node represent jinja variables used in the template
# We're connecting nodes with an edge for every depencency to traverse them
# in the right order and also make sure that we don't have missing or cyclic
# dependencies upfront.
dependencies = meta.find_undeclared_variables(template_ast)
LOG.debug("Dependencies: %s", dependencies)
if dependencies:
for dependency in dependencies:
G.add_edge(dependency, name)
try:
template_ast = ENV.parse(value)
G.add_node(name, template=value)

LOG.debug("Template ast: %s", template_ast)
# Dependencies of the node represent jinja variables used in the template
# We're connecting nodes with an edge for every depencency to traverse them
# in the right order and also make sure that we don't have missing or cyclic
# dependencies upfront.
dependencies = meta.find_undeclared_variables(template_ast)
LOG.debug("Dependencies: %s", dependencies)
if dependencies:
for dependency in dependencies:
G.add_edge(dependency, name)
except exceptions.TemplateSyntaxError:
G.add_node(name, value=value)
# not jinja after all
# is_jinga_expression only checks for {{ or {{% for speed
else:
G.add_node(name, value=value)

Expand Down
20 changes: 20 additions & 0 deletions st2common/tests/unit/test_param_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ class ParamsUtilsTest(DbTestCase):
action_system_default_db = FIXTURES["actions"]["action_system_default.yaml"]
runnertype_db = FIXTURES["runners"]["testrunner1.yaml"]

def test_process_jinja_exception(self):

action_context = {"api_user": "noob"}
config = {}
G = param_utils._create_graph(action_context, config)
name = "a1"
value = {"test": "http://someurl?value={{a"}
param_utils._process(G, name, value)
self.assertEquals(G.nodes.get(name, {}).get("value"), value)

def test_process_jinja_template(self):

action_context = {"api_user": "noob"}
config = {}
G = param_utils._create_graph(action_context, config)
name = "a1"
value = "http://someurl?value={{a}}"
param_utils._process(G, name, value)
self.assertEquals(G.nodes.get(name, {}).get("template"), value)

def test_get_finalized_params(self):
params = {
"actionstr": "foo",
Expand Down