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+ persist-credentials : false
27+
28+ - name : Check if PR targets default branch
29+ id : check-target
30+ run : |
31+ DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
32+ TARGET_BRANCH="${{ github.base_ref }}"
33+ if [ "$TARGET_BRANCH" = "$DEFAULT_BRANCH" ]; then
34+ echo "PR targets the default branch ($DEFAULT_BRANCH). Proceeding with autofix."
35+ echo "should_run=true" >> $GITHUB_OUTPUT
36+ else
37+ echo "PR does not target the default branch ($DEFAULT_BRANCH). Skipping autofix."
38+ echo "should_run=false" >> $GITHUB_OUTPUT
39+ fi
40+
41+ - name : Setup PDM
42+ uses : pdm-project/setup-pdm@v4
43+ if : steps.check-target.outputs.should_run == 'true'
44+ with :
45+ cache : true
46+
47+ - name : Install dependencies
48+ if : steps.check-target.outputs.should_run == 'true'
49+ run : pdm install -d
50+
51+ - name : Run linting
52+ if : steps.check-target.outputs.should_run == 'true'
53+ run : pdm run lint
54+
55+ - name : Run formatting
56+ if : steps.check-target.outputs.should_run == 'true'
57+ run : pdm run format
58+
59+ - name : Check for changes
60+ if : steps.check-target.outputs.should_run == 'true'
61+ id : check-changes
62+ run : |
63+ if [[ -n "$(git status --porcelain)" ]]; then
64+ echo "Has changes that need to be committed"
65+ echo "has_changes=true" >> $GITHUB_OUTPUT
66+ else
67+ echo "No changes detected"
68+ echo "has_changes=false" >> $GITHUB_OUTPUT
69+ fi
70+
71+ - name : Commit changes
72+ if : steps.check-target.outputs.should_run == 'true' && steps.check-changes.outputs.has_changes == 'true'
73+ run : |
74+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
75+ git config --local user.name "github-actions[bot]"
76+ git add -A
77+ git commit -m "Autofix: Lint and format code"
78+
79+ - name : Push changes
80+ uses : ad-m/github-push-action@77c5b412c50b723d2a4fbc6d71fb5723bcd439aa
81+ with :
82+ # use PAT if available, otherwise use GITHUB_TOKEN
83+ github_token : ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
84+ branch : ${{ github.head_ref }}
0 commit comments