Skip to content

Commit 89cd58a

Browse files
committed
[ci] Add workflow to check c++ code formatting.
Signed-off-by: boschmitt <[email protected]>
1 parent f0e6919 commit 89cd58a

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: PR sanity checks
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types: [opened, synchronize, reopened]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
env:
14+
LLVM_VERSION: 16
15+
16+
jobs:
17+
check-changes:
18+
name: Check changes
19+
runs-on: ubuntu-latest
20+
outputs:
21+
check-cpp: ${{ steps.filter.outputs.check-cpp }}
22+
check-all-cpp: ${{ steps.filter.outputs.check-all-cpp }}
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
with:
27+
set-safe-directory: true
28+
29+
- name: Check what needs testing
30+
uses: dorny/paths-filter@v3
31+
id: filter
32+
with:
33+
filters: |
34+
check-all-cpp:
35+
- '.clang-format'
36+
check-cpp:
37+
- '**/*.cpp'
38+
- '**/*.h'
39+
40+
check-clang-format:
41+
name: Check C++ code formatting
42+
needs: [check-changes]
43+
if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Checkout repository
47+
uses: actions/checkout@v4
48+
with:
49+
set-safe-directory: true
50+
51+
- name: Install clang-format
52+
run: |
53+
# Requirements
54+
sudo apt-get update
55+
sudo apt-get install -y wget software-properties-common gpg
56+
57+
# Obtain VERSION_CODENAME and UBUNTU_CODENAME
58+
source /etc/os-release
59+
60+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
61+
sudo add-apt-repository "deb http://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-${LLVM_VERSION} main"
62+
sudo apt-get update && sudo apt-get install -y --no-install-recommends clang-format-${LLVM_VERSION}
63+
64+
# If the `clang-format` file changes, we require the reformatting of all
65+
# files. See https://github.com/NVIDIA/cudaqx/pull/15#discussion_r1868174072
66+
- name: clang-format all things
67+
if: needs.check-changes.outputs.check-all-cpp == 'true'
68+
run: |
69+
git ls-files '*.cpp' '*.h' | xargs clang-format-${LLVM_VERSION} -i
70+
71+
if ! git diff --exit-code; then
72+
git diff --ignore-submodules > clang-format.patch
73+
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
74+
exit 1
75+
fi
76+
echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY
77+
exit 0
78+
79+
- name: clang-format changed files
80+
if: needs.check-changes.outputs.check-all-cpp != 'true'
81+
run: |
82+
# We did a shallow clone, and thus we need to make sure to fetch the base
83+
# commit.
84+
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
85+
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
86+
87+
git config clangFormat.binary clang-format-$LLVM_VERSION
88+
if ! git clang-format $DIFF_COMMIT_SHA; then
89+
git diff --ignore-submodules > clang-format.patch
90+
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
91+
exit 1
92+
fi
93+
echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY
94+
exit 0
95+
96+
- name: Upload format patch
97+
uses: actions/upload-artifact@v4
98+
continue-on-error: true
99+
if: ${{ failure() }}
100+
with:
101+
name: clang-format-patch
102+
path: clang-*.patch
103+

0 commit comments

Comments
 (0)