From 3cd7ce96101ddc94144e4715900c64c1f2e5e217 Mon Sep 17 00:00:00 2001 From: jctanner Date: Mon, 9 May 2022 19:27:19 -0400 Subject: [PATCH] Use the PR diff for TDD checks (#1257) Use the PR diff url to get the list of changed files instead of making a checkout. The master branch on forked repos isn't always kept up to date and gives false positives on files changed when compared to HEAD of the PR branch. No-Issue Signed-off-by: James Tanner --- .github/workflows/ci_tdd.yml | 6 +++--- dev/common/tdd.py | 28 +++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci_tdd.yml b/.github/workflows/ci_tdd.yml index 508fefc4d1..78c2882283 100644 --- a/.github/workflows/ci_tdd.yml +++ b/.github/workflows/ci_tdd.yml @@ -16,14 +16,14 @@ jobs: with: python-version: "3.8" - - name: make the checkout in a sane way. - run: git clone https://github.com/${{ github.event.pull_request.user.login }}/galaxy_ng; cd galaxy_ng ; git checkout "${{ github.head_ref }}" + - uses: actions/checkout@v3 - name: Ensure changes were made to tests env: PY_COLORS: '1' + GITHUB_PR_NUMBER: ${{github.event.number}} GITHUB_BASE_REF: ${{ github.base_ref }} GITHUB_BRANCH: ${{ github.head_ref }} GITHUB_USER: ${{ github.event.pull_request.user.login }} GITHUB_CONTEXT: ${{ toJson(github) }} - run: cd galaxy_ng; python dev/common/tdd.py + run: python dev/common/tdd.py diff --git a/dev/common/tdd.py b/dev/common/tdd.py index dc779c5734..2a5ddf2e93 100644 --- a/dev/common/tdd.py +++ b/dev/common/tdd.py @@ -20,11 +20,29 @@ def get_current_branch(): def get_changed_files(pr_branch, target_branch="master"): - cmd = f'git diff --name-only {pr_branch}..{target_branch}' - pid = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, check=True) - filenames = pid.stdout.decode('utf-8') - filenames = filenames.split('\n') - filenames = [x.strip() for x in filenames if x.strip()] + if os.environ.get('GITHUB_PR_NUMBER'): + url = "https://patch-diff.githubusercontent.com/raw/ansible/galaxy_ng" + url += f"/pull/{os.environ['GITHUB_PR_NUMBER']}.diff" + print(f'GET diff from {url}') + cmd = f'curl -s -o /tmp/pr.diff {url}' + pid = subprocess.run(cmd, shell=True) + assert pid.returncode == 0 + with open('/tmp/pr.diff', 'r') as f: + raw = f.read() + filenames = raw.split('\n') + filenames = [x for x in filenames if x.startswith('---') or x.startswith('+++')] + filenames = [x.split(None, 1)[1] for x in filenames] + filenames = [x for x in filenames if not x.startswith('/dev/null')] + filenames = [x.lstrip('a/') for x in filenames] + filenames = [x.lstrip('b/') for x in filenames] + filenames = sorted(set(filenames)) + else: + # diffs the local checkout ... + cmd = f'git diff --name-only "{pr_branch}".."{target_branch}"' + pid = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, check=True) + filenames = pid.stdout.decode('utf-8') + filenames = filenames.split('\n') + filenames = [x.strip() for x in filenames if x.strip()] return filenames