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 : Autofix Pull Request
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 : Setup Copywrite
48+ uses : hashicorp/setup-copywrite@5e3e8a26d7b9f8a508848ad0a069dfd2f7aa5339
49+
50+ - name : Install dependencies
51+ if : steps.check-target.outputs.should_run == 'true'
52+ run : pdm install -d
53+
54+ - name : Run linting
55+ if : steps.check-target.outputs.should_run == 'true'
56+ run : pdm run lint
57+
58+ - name : Run formatting
59+ if : steps.check-target.outputs.should_run == 'true'
60+ run : pdm run format
61+
62+ - name : Export requirements
63+ if : steps.check-target.outputs.should_run == 'true'
64+ run : pdm run export
65+
66+ - name : Add license headers
67+ if : steps.check-target.outputs.should_run == 'true'
68+ run : copywrite headers --config .copywrite.hcl
69+
70+ - name : Check for changes
71+ if : steps.check-target.outputs.should_run == 'true'
72+ id : check-changes
73+ run : |
74+ if [[ -n "$(git status --porcelain)" ]]; then
75+ echo "Has changes that need to be committed"
76+ echo "has_changes=true" >> $GITHUB_OUTPUT
77+ else
78+ echo "No changes detected"
79+ echo "has_changes=false" >> $GITHUB_OUTPUT
80+ fi
81+
82+ - name : Commit changes
83+ if : steps.check-target.outputs.should_run == 'true' && steps.check-changes.outputs.has_changes == 'true'
84+ run : |
85+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
86+ git config --local user.name "github-actions[bot]"
87+ git add -A
88+ git commit -m "Autofix: Lint and format code"
89+
90+ - name : Push changes
91+ if : steps.check-target.outputs.should_run == 'true' && steps.check-changes.outputs.has_changes == 'true'
92+ uses : ad-m/github-push-action@77c5b412c50b723d2a4fbc6d71fb5723bcd439aa
93+ with :
94+ # use PAT if available, otherwise use GITHUB_TOKEN
95+ github_token : ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
96+ branch : ${{ github.head_ref }}
0 commit comments