Skip to content

Commit 4aa12af

Browse files
[Github] Fetch all commits in PR for code formatting checks (#69766)
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.
1 parent 3f2ed81 commit 4aa12af

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

.github/workflows/pr-code-format.yml

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,37 @@ jobs:
77
code_formatter:
88
runs-on: ubuntu-latest
99
steps:
10-
- name: Fetch LLVM sources
11-
uses: actions/checkout@v4
12-
with:
13-
fetch-depth: 2
14-
10+
# Get changed files before checking out the repository to force the action
11+
# to analyze the diff from the Github API rather than looking at the
12+
# shallow clone and erroring out, which is significantly more prone to
13+
# failure.
1514
- name: Get changed files
1615
id: changed-files
1716
uses: tj-actions/changed-files@v39
1817
with:
1918
separator: ","
20-
fetch_depth: 100 # Fetches only the last 10 commits
19+
20+
- name: Calculate number of commits to fetch
21+
run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
22+
23+
- name: Fetch PR sources
24+
uses: actions/checkout@v4
25+
with:
26+
ref: ${{ github.event.pull_request.head.ref }}
27+
fetch-depth: ${{ env.PR_FETCH_DEPTH }}
28+
path: pr-sources
29+
30+
# We need to make sure that we aren't executing/using any code from the
31+
# PR for security reasons as we're using pull_request_target. Checkout
32+
# the target branch with the necessary files.
33+
- name: Fetch LLVM Sources
34+
uses: actions/checkout@v4
35+
with:
36+
sparse-checkout: |
37+
llvm/utils/git/requirements_formatting.txt
38+
llvm/utils/git/code-format-helper.py
39+
sparse-checkout-cone-mode: false
40+
path: llvm-sources
2141

2242
- name: "Listed files"
2343
run: |
@@ -34,21 +54,21 @@ jobs:
3454
with:
3555
python-version: '3.11'
3656
cache: 'pip'
37-
cache-dependency-path: 'llvm/utils/git/requirements_formatting.txt'
57+
cache-dependency-path: 'llvm-sources/llvm/utils/git/requirements_formatting.txt'
3858

3959
- name: Install python dependencies
40-
run: pip install -r llvm/utils/git/requirements_formatting.txt
60+
run: pip install -r llvm-sources/llvm/utils/git/requirements_formatting.txt
4161

4262
- name: Run code formatter
4363
env:
4464
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
45-
START_REV: ${{ github.event.pull_request.base.sha }}
46-
END_REV: ${{ github.event.pull_request.head.sha }}
65+
PR_DEPTH: ${{ github.event.pull_request.commits }}
4766
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
67+
working-directory: ./pr-sources
4868
run: |
49-
python llvm/utils/git/code-format-helper.py \
69+
python ../llvm-sources/llvm/utils/git/code-format-helper.py \
5070
--token ${{ secrets.GITHUB_TOKEN }} \
5171
--issue-number $GITHUB_PR_NUMBER \
52-
--start-rev $START_REV \
53-
--end-rev $END_REV \
72+
--start-rev HEAD~$PR_DEPTH \
73+
--end-rev HEAD \
5474
--changed-files "$CHANGED_FILES"

0 commit comments

Comments
 (0)