xref: /llvm-project/.github/workflows/pr-code-format.yml (revision 858f025a00fd107e9b8f97b630028d40c0c68725)
1name: "Check code formatting"
2
3permissions:
4  contents: read
5
6on:
7  pull_request:
8    branches:
9      - main
10      - 'users/**'
11
12jobs:
13  code_formatter:
14    runs-on: ubuntu-latest
15    timeout-minutes: 30
16    concurrency:
17      group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
18      cancel-in-progress: true
19    if: github.repository == 'llvm/llvm-project'
20    steps:
21      - name: Fetch LLVM sources
22        uses: actions/checkout@v4
23        with:
24          ref: ${{ github.event.pull_request.head.sha }}
25
26      - name: Checkout through merge base
27        uses: rmacklin/fetch-through-merge-base@v0
28        with:
29          base_ref: ${{ github.event.pull_request.base.ref }}
30          head_ref: ${{ github.event.pull_request.head.sha }}
31          deepen_length: 500
32
33      - name: Get changed files
34        id: changed-files
35        uses: tj-actions/changed-files@v39
36        with:
37          separator: ","
38          skip_initial_fetch: true
39
40      # We need to pull the script from the main branch, so that we ensure
41      # we get the latest version of this script.
42      - name: Fetch code formatting utils
43        uses: actions/checkout@v4
44        with:
45          repository: ${{ github.repository }}
46          ref: ${{ github.base_ref }}
47          sparse-checkout: |
48            llvm/utils/git/requirements_formatting.txt
49            llvm/utils/git/code-format-helper.py
50          sparse-checkout-cone-mode: false
51          path: code-format-tools
52
53      - name: "Listed files"
54        env:
55          CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
56        run: |
57          echo "Formatting files:"
58          echo "$CHANGED_FILES"
59
60      - name: Install clang-format
61        uses: aminya/setup-cpp@v1
62        with:
63          clangformat: 19.1.6
64
65      - name: Setup Python env
66        uses: actions/setup-python@v5
67        with:
68          python-version: '3.11'
69          cache: 'pip'
70          cache-dependency-path: 'code-format-tools/llvm/utils/git/requirements_formatting.txt'
71
72      - name: Install python dependencies
73        run: pip install -r code-format-tools/llvm/utils/git/requirements_formatting.txt
74
75      - name: Run code formatter
76        env:
77          GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
78          START_REV: ${{ github.event.pull_request.base.sha }}
79          END_REV: ${{ github.event.pull_request.head.sha }}
80          CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
81        # TODO(boomanaiden154): Once clang v18 is released, we should be able
82        # to take advantage of the new --diff_from_common_commit option
83        # explicitly in code-format-helper.py and not have to diff starting at
84        # the merge base.
85        # Create an empty comments file so the pr-write job doesn't fail.
86        run: |
87          echo "[]" > comments &&
88          python ./code-format-tools/llvm/utils/git/code-format-helper.py \
89            --write-comment-to-file \
90            --token ${{ secrets.GITHUB_TOKEN }} \
91            --issue-number $GITHUB_PR_NUMBER \
92            --start-rev $(git merge-base $START_REV $END_REV) \
93            --end-rev $END_REV \
94            --changed-files "$CHANGED_FILES"
95
96      - uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 #v4.3.0
97        if: always()
98        with:
99          name: workflow-args
100          path: |
101            comments
102