Skip to content

Commit 14c4e87

Browse files
committed
🚀 Add GitHub Actions workflow for automatic linting and formatting on PRs
1 parent 70401c4 commit 14c4e87

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

.github/workflows/pr_autofixes.yaml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: PR Autofix
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- '**' # This will run on all branches, and we'll detect the default branch
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
autofix:
11+
name: Lint and Format
12+
runs-on: ubuntu-latest
13+
14+
# Skip if the last 3 commits are from the GitHub Actions bot
15+
if: >
16+
!startsWith(github.event.commits[0].author.name, 'github-actions') ||
17+
!startsWith(github.event.commits[1].author.name, 'github-actions') ||
18+
!startsWith(github.event.commits[2].author.name, 'github-actions')
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v3
23+
with:
24+
fetch-depth: 0 # We need the full history for comparing branches
25+
ref: ${{ github.head_ref }} # Check out the PR branch
26+
27+
- name: Check if PR targets default branch
28+
id: check-target
29+
run: |
30+
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
31+
TARGET_BRANCH="${{ github.base_ref }}"
32+
if [ "$TARGET_BRANCH" = "$DEFAULT_BRANCH" ]; then
33+
echo "PR targets the default branch ($DEFAULT_BRANCH). Proceeding with autofix."
34+
echo "should_run=true" >> $GITHUB_OUTPUT
35+
else
36+
echo "PR does not target the default branch ($DEFAULT_BRANCH). Skipping autofix."
37+
echo "should_run=false" >> $GITHUB_OUTPUT
38+
fi
39+
40+
- name: Setup PDM
41+
uses: pdm-project/setup-pdm@v4
42+
if: steps.check-target.outputs.should_run == 'true'
43+
with:
44+
cache: true
45+
46+
- name: Install dependencies
47+
if: steps.check-target.outputs.should_run == 'true'
48+
run: pdm install -d
49+
50+
- name: Run linting
51+
if: steps.check-target.outputs.should_run == 'true'
52+
run: pdm run lint
53+
54+
- name: Run formatting
55+
if: steps.check-target.outputs.should_run == 'true'
56+
run: pdm run format
57+
58+
- name: Check for changes
59+
if: steps.check-target.outputs.should_run == 'true'
60+
id: check-changes
61+
run: |
62+
if [[ -n "$(git status --porcelain)" ]]; then
63+
echo "Has changes that need to be committed"
64+
echo "has_changes=true" >> $GITHUB_OUTPUT
65+
else
66+
echo "No changes detected"
67+
echo "has_changes=false" >> $GITHUB_OUTPUT
68+
fi
69+
70+
- name: Commit and push changes
71+
if: steps.check-target.outputs.should_run == 'true' && steps.check-changes.outputs.has_changes == 'true'
72+
run: |
73+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
74+
git config --local user.name "github-actions[bot]"
75+
git add -A
76+
git commit -m "Autofix: Lint and format code"
77+
git push
78+
79+
- name: Re-run checks
80+
if: steps.check-target.outputs.should_run == 'true' && steps.check-changes.outputs.has_changes == 'true'
81+
run: |
82+
echo "Re-running checks after autofix"
83+
gh workflow run quality.yaml \
84+
--ref ${{ github.head_ref }} \
85+
env:
86+
GH_TOKEN: ${{ github.token }}

.github/workflows/quality.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Quality Checks
22

3-
on: workflow_call
3+
on:
4+
workflow_call:
5+
workflow_dispatch:
46

57
jobs:
68
quality:

0 commit comments

Comments
 (0)