-
Notifications
You must be signed in to change notification settings - Fork 11
199 lines (170 loc) · 6.51 KB
/
pr_sanity_checks.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
name: PR sanity checks
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
LLVM_VERSION: 16
jobs:
check-changes:
name: Check changes
runs-on: ubuntu-latest
outputs:
check-cpp: ${{ steps.filter.outputs.check-cpp }}
check-all-cpp: ${{ steps.filter.outputs.check-all-cpp }}
check-python: ${{ steps.filter.outputs.check-python }}
check-all-python: ${{ steps.filter.outputs.check-all-python }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
set-safe-directory: true
- name: Check what needs testing
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
check-all-cpp:
- '.github/workflows/pr_sanity_checks.yaml'
- '.clang-format'
check-cpp:
- '**/*.cpp'
- '**/*.h'
check-all-python:
- '.github/workflows/pr_sanity_checks.yaml'
- '.style.yapf'
check-python:
- '**/*.py'
check-cpp:
name: Check C++ code formatting
needs: [check-changes]
if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
set-safe-directory: true
- name: Install clang-format
run: |
# Requirements
sudo apt-get update
sudo apt-get install -y wget software-properties-common gpg
# Obtain VERSION_CODENAME and UBUNTU_CODENAME
source /etc/os-release
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
sudo add-apt-repository "deb http://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-${LLVM_VERSION} main"
sudo apt-get update && sudo apt-get install -y --no-install-recommends clang-format-${LLVM_VERSION}
# If the `clang-format` file changes, we require the reformatting of all
# files. See https://github.com/NVIDIA/cudaqx/pull/15#discussion_r1868174072
- name: clang-format all things
if: needs.check-changes.outputs.check-all-cpp == 'true'
run: |
git ls-files '*.cpp' '*.h' | xargs clang-format-${LLVM_VERSION} -i
if ! git diff --exit-code; then
git diff --ignore-submodules > clang-format.patch
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY
exit 0
- name: clang-format changed files
if: needs.check-changes.outputs.check-all-cpp != 'true'
run: |
# We did a shallow clone, and thus we need to make sure to fetch the base
# commit.
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
if ! git clang-format-$LLVM_VERSION $DIFF_COMMIT_SHA; then
git diff --ignore-submodules > clang-format.patch
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY
exit 0
- name: Upload format patch
uses: actions/upload-artifact@v4
continue-on-error: true
if: ${{ failure() }}
with:
name: clang-format-patch
path: clang-*.patch
check-python:
name: Check Python code formatting
needs: [check-changes]
if: needs.check-changes.outputs.check-python == 'true' || needs.check-changes.outputs.check-all-python == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
set-safe-directory: true
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install YAPF
run: pip install yapf
- name: YAPF all things
if: needs.check-changes.outputs.check-all-python == 'true'
run: |
git ls-files '*.py' | xargs yapf -i
if ! git diff --exit-code; then
git diff --ignore-submodules > yapf-format.patch
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
exit 0
- name: YAPF changed files
if: needs.check-changes.outputs.check-all-python != 'true'
run: |
# We did a shallow clone, and thus we need to make sure to fetch the base
# commit.
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
git diff --diff-filter=d $DIFF_COMMIT_SHA -- '*.py' | yapf-diff -i
if ! git diff --exit-code; then
git diff --ignore-submodules > yapf-format.patch
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
exit 0
- name: Upload format patch
uses: actions/upload-artifact@v4
continue-on-error: true
if: ${{ failure() }}
with:
name: yapf-format-patch
path: yapf-*.patch
# This job is used for branch protection checks.
verify:
name: Sanity check PR
if: ${{ always() }}
needs:
- check-cpp
- check-python
runs-on: ubuntu-latest
steps:
- name: Check results
run: |
status="success"
check_result() {
name=$1
result=$2
# NOTE: "skipped" is considered success.
if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
echo "$name job failed"
status="failed"
fi
}
check_result "check-cpp" "${{needs.check-cpp.result}}"
check_result "check-python" "${{needs.check-python.result}}"
if [[ "$status" != "success" ]]; then
exit 1
fi