Skip to content

Commit

Permalink
Use the PR diff for TDD checks (#1257)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jctanner authored May 9, 2022
1 parent 7eeca4d commit 3cd7ce9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci_tdd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 23 additions & 5 deletions dev/common/tdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 3cd7ce9

Please sign in to comment.