Merge "Check blocks recursively for loops"

This commit is contained in:
Zuul 2020-05-07 16:02:14 +00:00 committed by Gerrit Code Review
commit 8478544a62
20 changed files with 97 additions and 22 deletions

View File

@ -20,6 +20,25 @@ https://zuul-ci.org/docs/zuul-jobs/policy.html\
if file.get('type') not in ('tasks', 'handlers'):
return results
results.extend(self.handle_play(task))
return results
def handle_play(self, task):
results = []
if 'block' in task:
results.extend(self.handle_playlist(task['block']))
else:
results.extend(self.handle_task(task))
return results
def handle_playlist(self, playlist):
results = []
for play in playlist:
results.extend(self.handle_play(play))
return results
def handle_task(self, task):
results = []
has_loop = 'loop' in task
for key in task.keys():
if key.startswith('with_'):

View File

@ -0,0 +1,6 @@
- block:
- debug:
var: item
loop:
- 1
- 2

View File

@ -0,0 +1,6 @@
- block:
- debug:
var: item
with_items:
- 1
- 2

View File

@ -0,0 +1,7 @@
- block:
- block:
- debug:
var: item
loop:
- 1
- 2

View File

@ -0,0 +1,8 @@
- block:
- debug:
msg: zj_item
loop:
- 1
- 2
loop_control:
loop_var: zj_item

View File

@ -0,0 +1,8 @@
- block:
- debug:
msg: zj_item
with_items:
- 1
- 2
loop_control:
loop_var: zj_item

View File

@ -0,0 +1,5 @@
- debug: # noqa ZUULJOBS0001
var: item
loop:
- 1
- 2

View File

@ -15,28 +15,44 @@
- name: Make sure ansible-lint is installed
command: "{{ ansible_lint_tempdir.path }}/bin/ansible-lint --version"
- name: Make sure missing loopvars fail linting
command: "{{ ansible_lint_tempdir.path }}/bin/ansible-lint test-playbooks/ansible-lint-rules/roles/{{ item }}"
- name: Get faulty playbooks and roles
command: >-
find test-playbooks/ansible-lint-rules/
-mindepth 4
-maxdepth 4
-wholename '*ZUULJOBS*/faulty/*/*'
args:
chdir: "{{ ansible_user_dir}}/{{ zuul.project.src_dir }}"
register: faulty_ansible_items
- name: Get valid playbooks and roles
command: >-
find test-playbooks/ansible-lint-rules/
-mindepth 4
-maxdepth 4
-wholename '*ZUULJOBS*/valid/roles/*'
args:
chdir: "{{ ansible_user_dir}}/{{ zuul.project.src_dir }}"
register: valid_ansible_items
- name: Make sure faulty roles fail linting
command: >-
{{ ansible_lint_tempdir.path }}/bin/ansible-lint
-t {{ item | regex_replace('.*/(ZUULJOBS.*?)/.*', '\1') }}
{{ item }}
args:
chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}"
register: ansible_lint
failed_when: ansible_lint.rc == 0
loop: "{{ faulty_ansible_items.stdout_lines }}"
- name: Make sure valid roles pass linting
command: >-
{{ ansible_lint_tempdir.path }}/bin/ansible-lint
-t {{ item | regex_replace('.*/(ZUULJOBS.*?)/.*', '\1') }}
{{ item }}
args:
chdir: "{{ansible_user_dir}}/{{ zuul.project.src_dir }}"
register: ansible_lint
failed_when: ansible_lint.rc == 0
loop:
- tasks-missing-loopvar-with
- tasks-missing-loopvar-loop
- tasks-include-missing-loopvar-with
- tasks-include-missing-loopvar-loop
- handlers-missing-loopvar-with
- handlers-missing-loopvar-loop
- name: Make sure valid configuration passes linting
command: "{{ ansible_lint_tempdir.path }}/bin/ansible-lint test-playbooks/ansible-lint-rules/roles/{{ item }}"
args:
chdir: "{{ansible_user_dir}}/{{ zuul.project.src_dir }}"
loop:
- tasks-loopvar-with
- tasks-loopvar-loop
- tasks-include-loopvar-with
- tasks-include-loopvar-loop
- handlers-loopvar-with
- handlers-loopvar-loop
failed_when: ansible_lint.rc != 0
loop: "{{ valid_ansible_items.stdout_lines }}"