Skip to content

Commit 98a1ab5

Browse files
authored
fix: skip labels if empty string or None (#832)
If the labels are resolved to be empty or None they will be skipped. Also if there are duplicate labels only one of them will be added. https://issues.redhat.com/browse/AAP-52204
1 parent dc7c03e commit 98a1ab5

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

ansible_rulebook/job_template_runner.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,13 @@ async def _get_label_ids_from_names(
353353

354354
all_labels = settings.eda_labels
355355
if labels:
356-
all_labels = settings.eda_labels + labels
357-
for label in all_labels:
356+
# Drop any empty strings or non str objects
357+
all_labels = settings.eda_labels + list(
358+
filter(lambda s: isinstance(s, str) and s != "", labels)
359+
)
360+
361+
# Drop duplicates if any from the label list
362+
for label in set(all_labels):
358363
label_obj = await self._get_or_create_label(
359364
label, organization_obj
360365
)
@@ -375,7 +380,6 @@ async def _get_labels_for_job(
375380
ask_labels_on_launch: bool,
376381
labels: Optional[list[str]],
377382
) -> list[int]:
378-
379383
if ask_labels_on_launch:
380384
return await self._get_label_ids_from_names(organization, labels)
381385

docs/actions.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Run a job template.
191191
- An optional string based lock ensures sequential execution of this action when execution strategy is set to parallel. It can also be a string field from the event payload. The locks are per ruleset, if a lock is in place all actions that use the same lock will wait till the earlier action has completed.
192192
- No
193193
* - labels
194-
- Optional list of strings as labels, which can be added to the job in the controller. Requires that Prompt on launch for Labels is enabled. If its not enabled the labels are ignored. ansible-rulebook will add a default label called "Activated by Event-Driven Ansible"
194+
- Optional list of strings as labels, which can be added to the job in the controller. Requires that Prompt on launch for Labels is enabled. If its not enabled the labels are ignored. ansible-rulebook will add a default label called "Activated by Event-Driven Ansible". If the label gets resolved as None or an empty string it will be dropped. If there are duplicate labels the duplicate ones will be removed. e.g {{ event.payload.my_label | default(None) }} if the attribute doesn't exist we will skip the label.
195195
- No
196196

197197
run_workflow_template
@@ -255,7 +255,9 @@ Run a workflow template.
255255
* - lock
256256
- An optional string based lock ensures sequential execution of this action when execution strategy is set to parallel. It can also be a string field from the event payload. The locks are per ruleset, if a lock is in place all actions that use the same lock will wait till the earlier action has completed.
257257
- No
258-
258+
* - labels
259+
- Optional list of strings as labels, which can be added to the job in the controller. Requires that Prompt on launch for Labels is enabled. If its not enabled the labels are ignored. ansible-rulebook will add a default label called "Activated by Event-Driven Ansible". If the label gets resolved as None or an empty string it will be dropped. If there are duplicate labels the duplicate ones will be removed. e.g {{ event.payload.my_label | default(None) }} if the attribute doesn't exist we will skip the label.
260+
- No
259261
post_event
260262
**********
261263
.. list-table:: Post an event to a running rule set in the rules engine

tests/test_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def add_job_templates_pages(mocked, host, page1, page2):
188188
{},
189189
),
190190
(
191-
[CUSTOMER_LABEL],
191+
[CUSTOMER_LABEL, "", None, "", {"a": 1}, [CUSTOMER_LABEL]],
192192
UNIFIED_JOB_TEMPLATE_PAGE1_RESPONSE,
193193
UNIFIED_JOB_TEMPLATE_PAGE2_RESPONSE,
194194
False,

0 commit comments

Comments
 (0)