Skip to content

Commit 6e460cd

Browse files
committed
[ci] Add workflow to check python code formatting.
Signed-off-by: boschmitt <[email protected]>
1 parent bbbbcc7 commit 6e460cd

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

.github/workflows/pr_sanity_checks.yaml

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
outputs:
2121
check-cpp: ${{ steps.filter.outputs.check-cpp }}
2222
check-all-cpp: ${{ steps.filter.outputs.check-all-cpp }}
23+
check-python: ${{ steps.filter.outputs.check-python }}
24+
check-all-python: ${{ steps.filter.outputs.check-all-python }}
2325
steps:
2426
- name: Checkout repository
2527
uses: actions/checkout@v4
@@ -37,8 +39,13 @@ jobs:
3739
check-cpp:
3840
- '**/*.cpp'
3941
- '**/*.h'
42+
check-all-python:
43+
- '.github/workflows/pr_sanity_checks.yaml'
44+
- '.style.yapf'
45+
check-python:
46+
- '**/*.py'
4047
41-
check-clang-format:
48+
check-cpp:
4249
name: Check C++ code formatting
4350
needs: [check-changes]
4451
if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
@@ -85,8 +92,7 @@ jobs:
8592
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
8693
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
8794
88-
git config clangFormat.binary clang-format-$LLVM_VERSION
89-
if ! git clang-format $DIFF_COMMIT_SHA; then
95+
if ! git clang-format-$LLVM_VERSION $DIFF_COMMIT_SHA; then
9096
git diff --ignore-submodules > clang-format.patch
9197
echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
9298
exit 1
@@ -102,3 +108,92 @@ jobs:
102108
name: clang-format-patch
103109
path: clang-*.patch
104110

111+
check-python:
112+
name: Check Python code formatting
113+
needs: [check-changes]
114+
if: needs.check-changes.outputs.check-python == 'true' || needs.check-changes.outputs.check-all-python == 'true'
115+
runs-on: ubuntu-latest
116+
steps:
117+
- name: Checkout repository
118+
uses: actions/checkout@v4
119+
with:
120+
set-safe-directory: true
121+
122+
- name: Set up Python
123+
uses: actions/setup-python@v4
124+
with:
125+
python-version: '3.x'
126+
127+
- name: Install YAPF
128+
run: pip install yapf
129+
130+
- name: YAPF all things
131+
if: needs.check-changes.outputs.check-all-python == 'true'
132+
run: |
133+
git ls-files '*.py' | xargs yapf -i
134+
135+
if ! git diff --exit-code; then
136+
git diff --ignore-submodules > yapf-format.patch
137+
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
138+
exit 1
139+
fi
140+
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
141+
exit 0
142+
143+
- name: YAPF changed files
144+
if: needs.check-changes.outputs.check-all-python != 'true'
145+
run: |
146+
# We did a shallow clone, and thus we need to make sure to fetch the base
147+
# commit.
148+
git fetch --recurse-submodules=no origin ${{ github.base_ref }}
149+
DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
150+
151+
git diff --diff-filter=d $DIFF_COMMIT_SHA -- '*.py' | yapf-diff -i
152+
153+
if ! git diff --exit-code; then
154+
git diff --ignore-submodules > yapf-format.patch
155+
echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
156+
exit 1
157+
fi
158+
echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
159+
exit 0
160+
161+
- name: Upload format patch
162+
uses: actions/upload-artifact@v4
163+
continue-on-error: true
164+
if: ${{ failure() }}
165+
with:
166+
name: yapf-format-patch
167+
path: yapf-*.patch
168+
169+
# This job is used for branch protection checks.
170+
verify:
171+
name: Sanity check PR
172+
if: ${{ always() }}
173+
needs:
174+
- check-cpp
175+
- check-python
176+
runs-on: ubuntu-latest
177+
steps:
178+
- name: Check results
179+
run: |
180+
status="success"
181+
182+
check_result() {
183+
name=$1
184+
result=$2
185+
186+
# NOTE: "skipped" is considered success.
187+
if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
188+
echo "$name job failed"
189+
190+
status="failed"
191+
fi
192+
}
193+
194+
check_result "check-cpp" "${{needs.check-cpp.result}}"
195+
check_result "check-python" "${{needs.check-python.result}}"
196+
197+
if [[ "$status" != "success" ]]; then
198+
exit 1
199+
fi

0 commit comments

Comments
 (0)