Skip to content

Commit 58cd01c

Browse files
Artur-claude
andcommitted
feat: add auto-commit formatter workflow with loop prevention
- Modified formatter workflow to automatically commit and push formatting changes - Added infinite loop detection by checking if last commit author was github-actions bot - Changed permissions to allow contents: write for pushing commits - Updated validation workflow to run after formatter completes - Added conditional execution to skip validation if formatter committed changes - Formatter will retrigger after auto-commit to verify formatting is correct 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a4d536f commit 58cd01c

File tree

2 files changed

+77
-24
lines changed

2 files changed

+77
-24
lines changed

.github/workflows/formatter.yml

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ concurrency:
1010
cancel-in-progress: true
1111

1212
permissions:
13-
contents: read
13+
contents: write
1414
pull-requests: write
1515
issues: write
1616

@@ -20,16 +20,33 @@ jobs:
2020

2121
formatter:
2222
needs: check-permissions
23-
name: Verify Java code format
23+
name: Format and auto-commit
2424
runs-on: ubuntu-latest
2525
timeout-minutes: 120
26+
outputs:
27+
changes_committed: ${{ steps.commit-changes.outputs.changes_committed }}
2628

2729
steps:
2830
- name: Checkout code
2931
uses: actions/checkout@v4
3032
with:
31-
ref: ${{ github.event.pull_request.head.sha }}
32-
fetch-depth: 0
33+
ref: ${{ github.event.pull_request.head.ref }}
34+
repository: ${{ github.event.pull_request.head.repo.full_name }}
35+
token: ${{ secrets.GITHUB_TOKEN }}
36+
fetch-depth: 2
37+
38+
- name: Check if last commit was from formatter bot
39+
id: check-loop
40+
run: |
41+
last_commit_author=$(git log -1 --pretty=format:'%an')
42+
echo "Last commit author: $last_commit_author"
43+
44+
if [ "$last_commit_author" = "github-actions[bot]" ]; then
45+
echo "::error::Last commit was from formatter bot. Possible infinite loop detected!"
46+
echo "loop_detected=true" >> $GITHUB_OUTPUT
47+
exit 1
48+
fi
49+
echo "loop_detected=false" >> $GITHUB_OUTPUT
3350
3451
- name: Set up JDK 21
3552
uses: actions/setup-java@v4
@@ -53,12 +70,32 @@ jobs:
5370
if [ "$modified" -gt 0 ]; then
5471
echo "Modified files:"
5572
echo "$files"
56-
git diff > formatter-diff.txt
5773
echo "files<<EOF" >> $GITHUB_OUTPUT
5874
echo "$files" >> $GITHUB_OUTPUT
5975
echo "EOF" >> $GITHUB_OUTPUT
6076
fi
6177
78+
- name: Commit and push changes
79+
id: commit-changes
80+
if: steps.formatter.outputs.modified != '0'
81+
run: |
82+
git config user.name "github-actions[bot]"
83+
git config user.email "github-actions[bot]@users.noreply.github.com"
84+
85+
# Add only the files modified by the formatter
86+
git add -u
87+
git commit -m "chore: apply code formatting with spotless"
88+
89+
git push
90+
91+
echo "changes_committed=true" >> $GITHUB_OUTPUT
92+
echo "✅ Formatting changes committed and pushed" >> $GITHUB_STEP_SUMMARY
93+
94+
- name: Generate diff for artifact
95+
if: steps.formatter.outputs.modified != '0'
96+
run: |
97+
git diff HEAD~1 > formatter-diff.txt
98+
6299
- name: Upload formatter diff
63100
if: steps.formatter.outputs.modified != '0'
64101
uses: actions/upload-artifact@v4
@@ -68,15 +105,14 @@ jobs:
68105
retention-days: 30
69106

70107
- name: Find existing comment
71-
if: steps.formatter.outputs.modified != '0'
72108
uses: peter-evans/find-comment@v3
73109
id: find-comment
74110
with:
75111
issue-number: ${{ github.event.pull_request.number }}
76112
comment-author: 'github-actions[bot]'
77113
body-includes: '<!-- tc-formatter -->'
78114

79-
- name: Create or update comment
115+
- name: Create or update comment for committed changes
80116
if: steps.formatter.outputs.modified != '0'
81117
uses: peter-evans/create-or-update-comment@v4
82118
with:
@@ -85,26 +121,19 @@ jobs:
85121
edit-mode: replace
86122
body: |
87123
<!-- tc-formatter -->
88-
### Format Checker Report
89-
90-
![BLOCKER][BLOCKER] There are **${{ steps.formatter.outputs.modified }} files** with format errors
91-
92-
[BLOCKER]: https://sonarsource.github.io/sonar-github/severity-blocker.png 'Severity: BLOCKER'
124+
### ✅ Formatter Auto-Applied
93125
94-
- To see a complete report of formatting issues, download the [differences artifact](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
126+
Formatting issues were detected and **automatically fixed**. Changes have been committed to this PR.
95127
96-
- To fix the build, please run `mvn spotless:apply` in your branch and commit the changes.
97-
98-
- Optionally you might add the following line in your `.git/hooks/pre-commit` file:
99-
100-
mvn spotless:apply
101-
102-
Here is the list of files with format issues in your PR:
128+
The formatting changes affected **${{ steps.formatter.outputs.modified }} files**.
103129
130+
Files modified:
104131
```
105132
${{ steps.formatter.outputs.files }}
106133
```
107134
135+
Please pull the latest changes before continuing work on this branch.
136+
108137
- name: Delete comment if formatting is correct
109138
if: steps.formatter.outputs.modified == '0' && steps.find-comment.outputs.comment-id != ''
110139
uses: actions/github-script@v7
@@ -116,8 +145,8 @@ jobs:
116145
comment_id: ${{ steps.find-comment.outputs.comment-id }}
117146
})
118147
119-
- name: Fail if formatting issues exist
120-
if: steps.formatter.outputs.modified != '0'
148+
- name: Set output for no changes
149+
if: steps.formatter.outputs.modified == '0'
121150
run: |
122-
echo "::error::There are ${{ steps.formatter.outputs.modified }} files with format errors"
123-
exit 1
151+
echo "changes_committed=false" >> $GITHUB_OUTPUT
152+
echo "✅ Code formatting is correct" >> $GITHUB_STEP_SUMMARY

.github/workflows/validation.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
workflow_dispatch:
66
pull_request_target:
77
types: [opened, synchronize, reopened, edited]
8+
workflow_run:
9+
workflows: ["Formatter"]
10+
types: [completed]
811
permissions:
912
contents: read
1013
concurrency:
@@ -15,7 +18,28 @@ env:
1518
REF_NAME: ${{ github.ref_name }}
1619
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
1720
jobs:
21+
check-formatter-status:
22+
if: github.event_name == 'workflow_run'
23+
runs-on: ubuntu-latest
24+
outputs:
25+
should_run: ${{ steps.check.outputs.should_run }}
26+
steps:
27+
- name: Check formatter workflow result
28+
id: check
29+
run: |
30+
if [ "${{ github.event.workflow_run.conclusion }}" != "success" ]; then
31+
echo "Formatter workflow did not succeed, skipping validation"
32+
echo "should_run=false" >> $GITHUB_OUTPUT
33+
else
34+
echo "should_run=true" >> $GITHUB_OUTPUT
35+
fi
36+
1837
check-permissions:
38+
if: |
39+
always() &&
40+
(github.event_name != 'workflow_run' ||
41+
(needs.check-formatter-status.result == 'success' && needs.check-formatter-status.outputs.should_run == 'true'))
42+
needs: [check-formatter-status]
1943
uses: ./.github/workflows/check-permissions.yml
2044

2145
build:

0 commit comments

Comments
 (0)