Skip to content

Commit

Permalink
Fix issue with looping over always empty list (redhat-cop#920)
Browse files Browse the repository at this point in the history
* fix: loop issue

* fix: loop issue part2

* fix: missing brackets

---------

Co-authored-by: Przemyslaw Kalitowski <przemyslaw@kalitowski.com>
  • Loading branch information
przemkalit and Przemyslaw Kalitowski authored Oct 1, 2024
1 parent 06c5ec9 commit 6ed66b7
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 44 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/filetree_create_loop_issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- Fixed issue with loops that were getting always empty list of objects
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/applications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/applications"
loop: >-
{{ (applications_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([default(organization)] if ((applications_lookvar | map(attribute='summary_fields')
| selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}
loop: "{{ (applications_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([organization] if ((applications_lookvar | map(attribute='summary_fields')
| selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path }}/credentials"
loop: >-
{{ (credentials_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([(organization if organization is defined else 'ORGANIZATIONLESS')] if ((credentials_lookvar |
map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}
loop: "{{ (credentials_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([organization] if ((credentials_lookvar |
map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/job_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/job_templates"
loop: >-
{{ (job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (job_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}
loop: "{{ (job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
([organization] if ((job_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/labels"
loop: "{{ (labels_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((labels_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
+ ([organization] if ((labels_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
Expand Down
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/notification_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/notification_templates"
loop: >-
{{ (notification_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (notification_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}
loop: "{{ (notification_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
([organization] if ((notification_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/projects"
loop: >-
{{ (projects_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (projects_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}"
loop: "{{ (projects_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
([organization] if ((projects_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list) | flatten | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
12 changes: 5 additions & 7 deletions roles/filetree_create/tasks/teams.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/teams"
loop: >-
{{
(teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | unique) +
[organization] if (teams_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | length > 0) else []
}}
loop: "{{ (teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | unique) +
([organization] if ((teams_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down
11 changes: 5 additions & 6 deletions roles/filetree_create/tasks/workflow_job_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/workflow_job_templates/"
loop: >-
{{ (workflow_job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
([organization] if ((workflow_job_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}
loop: "{{ (workflow_job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
([organization] if ((workflow_job_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand Down

0 comments on commit 6ed66b7

Please sign in to comment.