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
@@ -32,13 +34,16 @@ jobs:
3234 with :
3335 filters : |
3436 check-all-cpp:
35- - '.github/workflows/pr_sanity_checks.yaml'
3637 - '.clang-format'
3738 check-cpp:
3839 - '**/*.cpp'
3940 - '**/*.h'
41+ check-all-python:
42+ - '.style.yapf'
43+ check-python:
44+ - '**/*.py'
4045
41- check-clang-format :
46+ check-cpp :
4247 name : Check C++ code formatting
4348 needs : [check-changes]
4449 if : needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true'
8590 git fetch --recurse-submodules=no origin ${{ github.base_ref }}
8691 DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
8792
88- git config clangFormat.binary clang-format-$LLVM_VERSION
89- if ! git clang-format $DIFF_COMMIT_SHA; then
93+ if ! git clang-format-$LLVM_VERSION $DIFF_COMMIT_SHA; then
9094 git diff --ignore-submodules > clang-format.patch
9195 echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
9296 exit 1
@@ -102,3 +106,92 @@ jobs:
102106 name : clang-format-patch
103107 path : clang-*.patch
104108
109+ check-python :
110+ name : Check Python code formatting
111+ needs : [check-changes]
112+ if : needs.check-changes.outputs.check-python == 'true' || needs.check-changes.outputs.check-all-python == 'true'
113+ runs-on : ubuntu-latest
114+ steps :
115+ - name : Checkout repository
116+ uses : actions/checkout@v4
117+ with :
118+ set-safe-directory : true
119+
120+ - name : Set up Python
121+ uses : actions/setup-python@v4
122+ with :
123+ python-version : ' 3.x'
124+
125+ - name : Install YAPF
126+ run : pip install yapf
127+
128+ - name : YAPF all things
129+ if : needs.check-changes.outputs.check-all-python == 'true'
130+ run : |
131+ git ls-files '*.py' | xargs yapf -i
132+
133+ if ! git diff --exit-code; then
134+ git diff --ignore-submodules > yapf-format.patch
135+ echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
136+ exit 1
137+ fi
138+ echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
139+ exit 0
140+
141+ - name : YAPF changed files
142+ if : needs.check-changes.outputs.check-all-python != 'true'
143+ run : |
144+ # We did a shallow clone, and thus we need to make sure to fetch the base
145+ # commit.
146+ git fetch --recurse-submodules=no origin ${{ github.base_ref }}
147+ DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }})
148+
149+ git diff --diff-filter=d $DIFF_COMMIT_SHA -- '*.py' | yapf-diff --i
150+
151+ if ! git diff --exit-code; then
152+ git diff --ignore-submodules > yapf-format.patch
153+ echo "🟥 YAPF found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY
154+ exit 1
155+ fi
156+ echo "🟩 YAPF found no formatting problems" >> $GITHUB_STEP_SUMMARY
157+ exit 0
158+
159+ - name : Upload format patch
160+ uses : actions/upload-artifact@v4
161+ continue-on-error : true
162+ if : ${{ failure() }}
163+ with :
164+ name : yapf-format-patch
165+ path : yapf-*.patch
166+
167+ # This job is used for branch protection checks.
168+ verify :
169+ name : Sanity check PR
170+ if : ${{ always() }}
171+ needs :
172+ - check-cpp
173+ - check-python
174+ runs-on : ubuntu-latest
175+ steps :
176+ - name : Check results
177+ run : |
178+ status="success"
179+
180+ check_result() {
181+ name=$1
182+ result=$2
183+
184+ # NOTE: "skipped" is considered success.
185+ if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
186+ echo "$name job failed"
187+
188+ status="failed"
189+ fi
190+ }
191+
192+ check_result "check-cpp" "${{needs.check-cpp.result}}"
193+ check_result "check-python" "${{needs.check-python.result}}"
194+
195+ if [[ "$status" != "success" ]]; then
196+ exit 1
197+ fi
0 commit comments