1+ name : Auto-format code
2+
3+ on :
4+ issue_comment :
5+ types : [created]
6+
7+ permissions :
8+ contents : write
9+ pull-requests : read
10+
11+ jobs :
12+ auto-format :
13+ if : >-
14+ github.event.issue.pull_request &&
15+ contains(github.event.comment.body, 'format please')
16+ runs-on : ubuntu-latest
17+ steps :
18+ - name : Get PR details
19+ id : pr
20+ uses : actions/github-script@v7
21+ with :
22+ script : |
23+ const pr = await github.rest.pulls.get({
24+ owner: context.repo.owner,
25+ repo: context.repo.repo,
26+ pull_number: context.issue.number,
27+ });
28+ core.setOutput('ref', pr.data.head.ref);
29+ core.setOutput('repo', pr.data.head.repo.full_name);
30+
31+ - name : Checkout PR branch
32+ uses : actions/checkout@v4
33+ with :
34+ ref : ${{ steps.pr.outputs.ref }}
35+ repository : ${{ steps.pr.outputs.repo }}
36+ token : ${{ secrets.GITHUB_TOKEN }}
37+ fetch-depth : 2
38+
39+ - name : Set up JDK
40+ uses : actions/setup-java@v4
41+ with :
42+ java-version : 11
43+ distribution : ' temurin'
44+ cache : maven
45+
46+ - name : Apply formatting
47+ run : mvn -B -Pformatting spotless:apply
48+
49+ - name : Get last commit author
50+ id : author
51+ run : |
52+ echo "name=$(git log -1 --format='%an')" >> "$GITHUB_OUTPUT"
53+ echo "email=$(git log -1 --format='%ae')" >> "$GITHUB_OUTPUT"
54+
55+ - name : Commit and push formatting changes
56+ run : |
57+ git config user.name "${{ steps.author.outputs.name }}"
58+ git config user.email "${{ steps.author.outputs.email }}"
59+ git add -A
60+ if git diff --cached --quiet; then
61+ echo "No formatting changes needed."
62+ else
63+ git commit -m "Auto-format code"
64+ git push
65+ fi
0 commit comments