Skip to content

Commit 4c045b3

Browse files
nzloshemedvedev
authored andcommitted
Fixes #3063. Stopped directly accessing dict keys to avoid KeyError
exception and provide sane default values. Normalised representation keys provided as strings types to be returned as list types.
1 parent a4b4d77 commit 4c045b3

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

st2common/st2common/util/actionalias_matching.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ def list_format_strings_from_aliases(aliases):
3939
for alias in aliases:
4040
for format_ in alias.formats:
4141
display, representations = normalise_alias_format_string(format_)
42-
patterns.extend([(display, representation) for representation in representations])
42+
if display and len(representations) == 0:
43+
patterns.extend([(display, [])])
44+
else:
45+
patterns.extend([(display, representation) for representation in representations])
4346
return patterns
4447

4548

4649
def normalise_alias_format_string(alias_format):
4750
'''
48-
StackStorm action aliases can have two types;
49-
1. A simple string holding the format
50-
2. A dictionary which hold numerous alias format "representation(s)"
51-
With a single "display" for help about the action alias.
51+
StackStorm action aliases come in two forms;
52+
1. A string holding the format, which is also used as the help string.
53+
2. A dictionary containing "display" and/or "representation" keys.
54+
"representation": a list of numerous alias format "representation(s)"
55+
"display": a help string to be displayed.
5256
This function processes both forms and returns a standardized form.
5357
5458
:param alias_format: The alias format
@@ -64,8 +68,10 @@ def normalise_alias_format_string(alias_format):
6468
display = alias_format
6569
representation.append(alias_format)
6670
elif isinstance(alias_format, dict):
67-
display = alias_format['display']
68-
representation = alias_format['representation']
71+
display = alias_format.get('display')
72+
representation = alias_format.get('representation') or []
73+
if isinstance(representation, six.string_types):
74+
representation = [representation]
6975
else:
7076
raise TypeError("alias_format '%s' is neither a dictionary or string type."
7177
% repr(alias_format))

st2common/tests/unit/test_util_actionalias_matching.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ def test_list_format_strings_from_aliases_with_display(self, mock):
6565
self.assertEqual(result[2][0], "How do I feel? I feel... {{status}}!")
6666
self.assertEqual(result[2][1], "How do I feel? I feel... {{status}}!")
6767

68+
def test_list_format_strings_from_aliases_with_display_only(self, mock):
69+
ALIASES = [
70+
MemoryActionAliasDB(name='andy', ref='the_goonies.1', formats=[
71+
{'display': 'Watch this.'}]),
72+
MemoryActionAliasDB(name='andy', ref='the_goonies.2', formats=[
73+
{'display': "He's just like his {{relation}}."}])
74+
]
75+
result = matching.list_format_strings_from_aliases(ALIASES)
76+
self.assertEqual(len(result), 2)
77+
self.assertEqual(result[0][0], 'Watch this.')
78+
self.assertEqual(result[0][1], [])
79+
self.assertEqual(result[1][0], "He's just like his {{relation}}.")
80+
self.assertEqual(result[1][1], [])
81+
82+
def test_list_format_strings_from_aliases_with_representation_only(self, mock):
83+
ALIASES = [
84+
MemoryActionAliasDB(name='data', ref='the_goonies.1', formats=[
85+
{'representation': "That's okay daddy. You can't hug a {{object}}."}]),
86+
MemoryActionAliasDB(name='mr_wang', ref='the_goonies.2', formats=[
87+
{'representation': 'You are my greatest invention.'}])
88+
]
89+
result = matching.list_format_strings_from_aliases(ALIASES)
90+
self.assertEqual(len(result), 2)
91+
self.assertEqual(result[0][0], None)
92+
self.assertEqual(result[0][1], "That's okay daddy. You can't hug a {{object}}.")
93+
self.assertEqual(result[1][0], None)
94+
self.assertEqual(result[1][1], 'You are my greatest invention.')
95+
6896
def test_normalise_alias_format_string(self, mock):
6997
result = matching.normalise_alias_format_string(
7098
'Quite an experience to live in fear, isn\'t it?')

0 commit comments

Comments
 (0)