Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use ansible-test's change detection for Pull Requests #46

Merged
merged 12 commits into from
Nov 14, 2022
85 changes: 84 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ inputs:
default: auto
pre-test-cmd:
description: Extra command to invoke before ansible-test
pull-request-change-detection:
description: >-
Whether to use change detection for pull requests. If set to `true`, will
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to have this on by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a breaking change, and different what most collections do right now. Basically only the collections using AZP use change detection, all others simply run all tests.

Also switching this on effectively disables code coverage reporting for pull requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also switching this on effectively disables code coverage reporting for pull requests.

Could you explore if codecov's carryforward feature could fix this?

use change detection to determine changed files against the target
branch, and will not upload code coverage results. If the invocation is
not from a pull request, this option is ignored.
default: 'false'
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
python-version:
description: >-
**(DEPRECATED)** Use `origin-python-version` instead. This is only kept
Expand Down Expand Up @@ -191,6 +198,44 @@ runs:
set +x
shell: bash

- name: Log the next step action
run: echo ▷ Figuring out change detection and coverage
shell: bash
- name: Determine change detection and coverage
id: compute-change-detection-coverage
run: |
# Compute whether to use change detection and coverage
import os
import pathlib
import sys

FILE_APPEND_MODE = 'a'
OUTPUTS_FILE_PATH = pathlib.Path(os.environ['GITHUB_OUTPUT'])

def set_output(name, value):
with OUTPUTS_FILE_PATH.open(FILE_APPEND_MODE) as outputs_file:
outputs_file.writelines(f'{name}={value}{os.linesep}')

# Input from GHA
pull_request_change_detection = '${{
inputs.pull-request-change-detection
}}'
pull_request_branch = '${{ github.event.pull_request.base.ref || '' }}'

# Compute coverage and change detection arguments
coverage_arg = '--coverage'
change_detection_arg = ''
if pull_request_branch and pull_request_change_detection == 'true':
coverage_arg = ''
change_detection_arg = (
f'--changed --base-branch {pull_request_branch}'
)

# Set computed coverage-arg and change-detection-arg
set_output('coverage-arg', coverage_arg)
set_output('change-detection-arg', change_detection_arg)
shell: python

- name: Log the next step action
if: >-
!inputs.collection-src-directory
Expand All @@ -205,6 +250,39 @@ runs:
path: .tmp-ansible-collection-checkout
persist-credentials: false
ref: ${{ inputs.git-checkout-ref }}
fetch-depth: >-
${{
steps.compute-change-detection-coverage.outputs.change-detection-arg
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
&& '0' || '1'
}}

- name: Log the next step action
if: >-
!inputs.collection-src-directory
&& steps.compute-change-detection-coverage.outputs.change-detection-arg
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
run: >-
echo ▷ Create branches for change detection
shell: bash
- name: Create branches for change detection
if: >-
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
!inputs.collection-src-directory
&& steps.compute-change-detection-coverage.outputs.change-detection-arg
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
run: |
# Create a branch for the current HEAD, which happens to be a merge commit
git checkout -b __action_branch_name

# Check out the target branch and name it
git checkout -b ${{
github.event.pull_request.base.ref
}} --track origin/${{
github.event.pull_request.base.ref
}}

# Check out the merge commit
git checkout __action_branch_name
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
working-directory: >-
.tmp-ansible-collection-checkout

- name: Log the next step action
run: >-
Expand Down Expand Up @@ -358,7 +436,8 @@ runs:
;
~/.local/bin/ansible-test ${{ inputs.testing-type }}
-v --color
--coverage
${{ steps.compute-change-detection-coverage.outputs.coverage-arg }}
${{ steps.compute-change-detection-coverage.outputs.change-detection-arg }}
${{
inputs.testing-type == 'sanity'
&& '--junit'
Expand Down Expand Up @@ -408,10 +487,12 @@ runs:
working-directory: ${{ steps.collection-metadata.outputs.checkout-path }}

- name: Log the next step action
if: steps.compute-change-detection-coverage.outputs.coverage-arg != ''
run: >-
echo ▷ Generating a coverage report...
shell: bash
- name: Generate coverage report
if: steps.compute-change-detection-coverage.outputs.coverage-arg != ''
run: >-
set -x
;
Expand All @@ -425,13 +506,15 @@ runs:
working-directory: ${{ steps.collection-metadata.outputs.checkout-path }}

- name: Log the next step action
if: steps.compute-change-detection-coverage.outputs.coverage-arg != ''
run: >-
echo ▷ Sending the coverage data over to
https://codecov.io/gh/${{ github.repository }}...
shell: bash
- name: >-
Send the coverage data over to
https://codecov.io/gh/${{ github.repository }}
if: steps.compute-change-detection-coverage.outputs.coverage-arg != ''
uses: codecov/codecov-action@v3
with:
files: >-
Expand Down