From e1ec70a364aca273c088e8d1bb389d43b6a95099 Mon Sep 17 00:00:00 2001 From: quidame Date: Thu, 15 Apr 2021 18:30:52 +0200 Subject: [PATCH] move all tasks from playbook to a dedicated role * add a 'git rebase' before 'git diff' queries --- playbooks/ansible-changelog-fragment/run.yaml | 37 ++--------- .../defaults/main.yml | 11 ++++ roles/check_changelog_fragment/tasks/main.yml | 62 +++++++++++++++++++ 3 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 roles/check_changelog_fragment/defaults/main.yml create mode 100644 roles/check_changelog_fragment/tasks/main.yml diff --git a/playbooks/ansible-changelog-fragment/run.yaml b/playbooks/ansible-changelog-fragment/run.yaml index ac842338..7700ad33 100644 --- a/playbooks/ansible-changelog-fragment/run.yaml +++ b/playbooks/ansible-changelog-fragment/run.yaml @@ -1,35 +1,10 @@ --- -# There is a new changelog fragment, OR: there is a new plugin and no changes -# of existing files. Fail otherwise. - - hosts: localhost gather_facts: false + vars: + check_changelog_fragment__git_directory: "{{ zuul.executor.src_root }}/{{ zuul.project.canonical_name }}" + check_changelog_fragment__target_branch: "{{ zuul.project.default-branch }}" tasks: - - name: Look for changelog fragment - command: - chdir: "{{ zuul.executor.src_root }}/{{ zuul.project.canonical_name }}" - cmd: "git diff {{ zuul.project.default-branch }} --name-status --exit-code --diff-filter=A -- changelogs/fragments/" - register: pr_changelog_fragment - failed_when: pr_changelog_fragment.rc > 1 - - - name: Look for new plugin - command: - chdir: "{{ zuul.executor.src_root }}/{{ zuul.project.canonical_name }}" - cmd: "git diff {{ zuul.project.default-branch }} --name-status --exit-code --diff-filter=A -- plugins/" - register: pr_new_plugin - failed_when: pr_new_plugin.rc > 1 - - - name: Look for modified and deleted files - command: - chdir: "{{ zuul.executor.src_root }}/{{ zuul.project.canonical_name }}" - cmd: "git diff {{ zuul.project.default-branch }} --name-status --exit-code --diff-filter=MD" - register: pr_modified_files - failed_when: pr_modified_files.rc > 1 - - - name: Assert that a new changelog fragment is present if required - assert: - that: - - pr_changelog_fragment.rc == 1 or - pr_new_plugin.rc > pr_modified_files.rc - success_msg: "Your pull-request contains a new {{ pr_changelog_fragment.rc | bool | ternary('changelog fragment', 'plugin') }}." - fail_msg: "Your pull-request is missing a changelog fragment, please add one. It should explain to end users the reason for your change." + - name: Run check_changelog_fragment role + include_role: + name: check_changelog_fragment diff --git a/roles/check_changelog_fragment/defaults/main.yml b/roles/check_changelog_fragment/defaults/main.yml new file mode 100644 index 00000000..bb975166 --- /dev/null +++ b/roles/check_changelog_fragment/defaults/main.yml @@ -0,0 +1,11 @@ +--- +# The root directory of the project +check_changelog_fragment__git_directory: "." + +# The target branch of the pull-request +check_changelog_fragment__target_branch: "main" + +# Option '--exit-code' makes 'git diff' behaves as 'diff': +# - diff output is empty: rc = 0 +# - diff output is not empty: rc = 1 +check_changelog_fragment__git_command: "git diff {{ check_changelog_fragment__target_branch }} --name-status --exit-code" diff --git a/roles/check_changelog_fragment/tasks/main.yml b/roles/check_changelog_fragment/tasks/main.yml new file mode 100644 index 00000000..43a08288 --- /dev/null +++ b/roles/check_changelog_fragment/tasks/main.yml @@ -0,0 +1,62 @@ +--- +# RULES +# * a pull-request needs a new changelog fragment +# * unless: +# * it is a "New plugin" pull-request +# * unless: +# * the PR modifies existing file(s) +# * the PR deletes existing file(s) +# * the PR renames existing file(s) + + +# Rebasing ensures diffs are related to the PR, and only to the PR. If the task +# fails, this is because of conflicts, i.e. the PR is not ready for merging (or +# one of git_directory or target_branch is incorrect). +- name: Rebase the PR branch on the top of the target branch + command: + chdir: "{{ check_changelog_fragment__git_directory }}" + cmd: "git rebase {{ check_changelog_fragment__target_branch }}" + register: check_changelog_fragment__rebase + changed_when: check_changelog_fragment__rebase.stdout_lines | length > 1 + + +- name: Look for a new changelog fragment + command: + cmd: "{{ check_changelog_fragment__git_command }} --diff-filter=A -- changelogs/fragments/" + chdir: "{{ check_changelog_fragment__git_directory }}" + register: check_changelog_fragment__new_fragment + failed_when: check_changelog_fragment__new_fragment.rc > 1 + changed_when: false + + +- name: Look for a new plugin + command: + cmd: "{{ check_changelog_fragment__git_command }} --diff-filter=A -- plugins/" + chdir: "{{ check_changelog_fragment__git_directory }}" + register: check_changelog_fragment__new_plugin + failed_when: check_changelog_fragment__new_plugin.rc > 1 + changed_when: false + + +- name: Look for changes of existing files (modified, deleted, renamed) + command: + cmd: "{{ check_changelog_fragment__git_command }} --diff-filter=MDR" + chdir: "{{ check_changelog_fragment__git_directory }}" + register: check_changelog_fragment__changed_files + failed_when: check_changelog_fragment__changed_files.rc > 1 + changed_when: false + + +# There is a new changelog fragment (rc=1) OR: there is a new plugin (rc=1) AND +# no changes of existing files (rc=0). +- name: Assert that a new changelog fragment is present if required + assert: + that: + - check_changelog_fragment__new_fragment.rc == 1 or + check_changelog_fragment__new_plugin.rc > check_changelog_fragment__changed_files.rc + success_msg: >- + Your pull-request contains a new {{ 'changelog fragment' if + check_changelog_fragment__new_fragment.rc | bool else 'plugin' }}. + fail_msg: >- + Your pull-request is missing a changelog fragment, please add one. + It should explain to end users the reason for your change.