Skip to content

Commit f56c8c2

Browse files
Reland "[Github] Fetch all commits in PR for code formatting checks (llvm#69766)"
This commit relands 4aa12af. This was originally reverted as it caused fetch failures due to the usage of the wrong ref. I didn't catch this in testing as it was attempting to check the branch out from origin, where it didn't exist, but did on my fork as I never tested any cross-repo PRs.
1 parent e98195f commit f56c8c2

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 # Fetches only the last 2 commits
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: 2000 # Fetches only the last 2000 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.sha }}
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)