Skip to content

Commit 8c92b3c

Browse files
boschmittjustinlietz
authored andcommitted
[ci] Add workflow to check c++ code formatting. (NVIDIA#15)
Signed-off-by: boschmitt <[email protected]>
1 parent a836dc3 commit 8c92b3c

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+
- '.github/workflows/pr_sanity_checks.yaml'
36+
- '.clang-format'
37+
check-cpp:
38+
- '**/*.cpp'
39+
- '**/*.h'
40+
41+
check-clang-format:
42+
name: Check C++ code formatting
43+
needs: [check-changes]
44+
if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Checkout repository
48+
uses: actions/checkout@v4
49+
with:
50+
set-safe-directory: true
51+
52+
- name: Install clang-format
53+
run: |
54+
# Requirements
55+
sudo apt-get update
56+
sudo apt-get install -y wget software-properties-common gpg
57+
58+
# Obtain VERSION_CODENAME and UBUNTU_CODENAME
59+
source /etc/os-release
60+
61+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
62+
sudo add-apt-repository "deb http://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-${LLVM_VERSION} main"
63+
sudo apt-get update && sudo apt-get install -y --no-install-recommends clang-format-${LLVM_VERSION}
64+
65+
# If the `clang-format` file changes, we require the reformatting of all
66+
# files. See https://github.com/NVIDIA/cudaqx/pull/15#discussion_r1868174072
67+
- name: clang-format all things
68+
if: needs.check-changes.outputs.check-all-cpp == 'true'
69+
run: |
70+
git ls-files '*.cpp' '*.h' | xargs clang-format-${LLVM_VERSION} -i
71+
72+
if ! git diff --exit-code; then
73+
git diff --ignore-submodules > clang-format.patch
74+
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
75+
exit 1
76+
fi
77+
echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY
78+
exit 0
79+
80+
- name: clang-format changed files
81+
if: needs.check-changes.outputs.check-all-cpp != 'true'
82+
run: |
83+
# We did a shallow clone, and thus we need to make sure to fetch the base
84+
# commit.
85+
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
86+
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
87+
88+
if ! git clang-format-$LLVM_VERSION $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)