Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
move all tasks from playbook to a dedicated role
Browse files Browse the repository at this point in the history
* add a 'git rebase' before 'git diff' queries
  • Loading branch information
quidame committed Apr 15, 2021
1 parent 86cc435 commit e1ec70a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
37 changes: 6 additions & 31 deletions playbooks/ansible-changelog-fragment/run.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions roles/check_changelog_fragment/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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"
62 changes: 62 additions & 0 deletions roles/check_changelog_fragment/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit e1ec70a

Please sign in to comment.