Skip to content

Commit e49573e

Browse files
committed
ci: Add format check workflow
Add a CI workflow to report formatting issues on changed files. This is to gradually update files in the repository to be conform with PEP8 formatting. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent d248b05 commit e49573e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

.github/workflows/format.yml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Format check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
paths:
8+
- '**.py'
9+
10+
jobs:
11+
find-changed-files:
12+
runs-on: ubuntu-latest
13+
outputs:
14+
files: ${{ steps.git-diff-files.outputs.files }}
15+
name: Detect added and changed files
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Create json diff
23+
uses: GrantBirki/git-diff-action@v2
24+
id: git-diff
25+
with:
26+
base_branch: origin/main
27+
search_path: '**.py'
28+
json_diff_file_output: diff.json
29+
file_output_only: 'true'
30+
# Ignore deleted files
31+
git_options: '--no-color --diff-filter=d'
32+
33+
- name: Convert json diff to matrix array
34+
id: git-diff-files
35+
env:
36+
JSON_DIFF: ${{ steps.git-diff.outputs.json-diff-path }}
37+
run: |
38+
# Github output expects oneliners, use compact mode
39+
files=$(cat $JSON_DIFF | jq -c -r '[.files[] | {path: .path}]')
40+
echo "files=$files" >> $GITHUB_OUTPUT
41+
42+
ruff-format:
43+
needs: find-changed-files
44+
if: ${{ needs.find-changed-files.outputs.files != '[]' }}
45+
runs-on: ubuntu-latest
46+
# Allow the workflow run to pass when this job fails
47+
continue-on-error: true
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
files: ${{ fromJSON(needs.find-changed-files.outputs.files) }}
52+
53+
name: Check file ${{ matrix.files.path }}
54+
steps:
55+
- uses: actions/checkout@v4
56+
57+
- name: Run ruff format check for ${{ matrix.files.path }}
58+
uses: astral-sh/ruff-action@v1
59+
with:
60+
args: "format --check --diff"
61+
src: "${{ matrix.files.path }}"
62+
63+
- name: Annotate unformatted file
64+
if: ${{ failure() }}
65+
run: |
66+
echo "::warning file=${{ matrix.files.path }},title=File format check failed::Run 'ruff format ${{ matrix.files.path }}'"

0 commit comments

Comments
 (0)