Skip to content

Commit

Permalink
[Github] Fetch all commits in PR for code formatting checks (llvm#69766)
Browse files Browse the repository at this point in the history
This patch makes a couple changes to the PR code formatting check:
- Moves the `changed-files` action to before the checkout to make sure
that it pulls
information from the Github API rather than by running `git diff` to
alleviate some
performance problems.
- Checkout the head of the pull request head instead of the base of the
pull request
to ensure that we have the PR commits inside the checkout.
- Add an additional sparse checkout of the necessary LLVM tools to run
the action
to alleviate security problems introduced by checking out the head of
the pull
request. Only code from the base of the pull request runs.
- Adjust the commit references to be based on `HEAD` as Github doesn't
give
exact commit SHAs for the first commit in the PR.
  • Loading branch information
boomanaiden154 authored Oct 30, 2023
1 parent 3f2ed81 commit 4aa12af
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions .github/workflows/pr-code-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,37 @@ jobs:
code_formatter:
runs-on: ubuntu-latest
steps:
- name: Fetch LLVM sources
uses: actions/checkout@v4
with:
fetch-depth: 2

# Get changed files before checking out the repository to force the action
# to analyze the diff from the Github API rather than looking at the
# shallow clone and erroring out, which is significantly more prone to
# failure.
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v39
with:
separator: ","
fetch_depth: 100 # Fetches only the last 10 commits

- name: Calculate number of commits to fetch
run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"

- name: Fetch PR sources
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: ${{ env.PR_FETCH_DEPTH }}
path: pr-sources

# We need to make sure that we aren't executing/using any code from the
# PR for security reasons as we're using pull_request_target. Checkout
# the target branch with the necessary files.
- name: Fetch LLVM Sources
uses: actions/checkout@v4
with:
sparse-checkout: |
llvm/utils/git/requirements_formatting.txt
llvm/utils/git/code-format-helper.py
sparse-checkout-cone-mode: false
path: llvm-sources

- name: "Listed files"
run: |
Expand All @@ -34,21 +54,21 @@ jobs:
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: 'llvm/utils/git/requirements_formatting.txt'
cache-dependency-path: 'llvm-sources/llvm/utils/git/requirements_formatting.txt'

- name: Install python dependencies
run: pip install -r llvm/utils/git/requirements_formatting.txt
run: pip install -r llvm-sources/llvm/utils/git/requirements_formatting.txt

- name: Run code formatter
env:
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
START_REV: ${{ github.event.pull_request.base.sha }}
END_REV: ${{ github.event.pull_request.head.sha }}
PR_DEPTH: ${{ github.event.pull_request.commits }}
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
working-directory: ./pr-sources
run: |
python llvm/utils/git/code-format-helper.py \
python ../llvm-sources/llvm/utils/git/code-format-helper.py \
--token ${{ secrets.GITHUB_TOKEN }} \
--issue-number $GITHUB_PR_NUMBER \
--start-rev $START_REV \
--end-rev $END_REV \
--start-rev HEAD~$PR_DEPTH \
--end-rev HEAD \
--changed-files "$CHANGED_FILES"

0 comments on commit 4aa12af

Please sign in to comment.