|
20 | 20 | outputs: |
21 | 21 | check-cpp: ${{ steps.filter.outputs.check-cpp }} |
22 | 22 | 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 }} |
23 | 25 | steps: |
24 | 26 | - name: Checkout repository |
25 | 27 | uses: actions/checkout@v4 |
|
37 | 39 | check-cpp: |
38 | 40 | - '**/*.cpp' |
39 | 41 | - '**/*.h' |
| 42 | + check-all-python: |
| 43 | + - '.github/workflows/pr_sanity_checks.yaml' |
| 44 | + - '.style.yapf' |
| 45 | + check-python: |
| 46 | + - '**/*.py' |
40 | 47 |
|
41 | | - check-clang-format: |
| 48 | + check-cpp: |
42 | 49 | name: Check C++ code formatting |
43 | 50 | needs: [check-changes] |
44 | 51 | if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true' |
@@ -101,3 +108,92 @@ jobs: |
101 | 108 | name: clang-format-patch |
102 | 109 | path: clang-*.patch |
103 | 110 |
|
| 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