Skip to content

Commit d13ef9c

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 <noreply@anthropic.com>
1 parent a4d536f commit d13ef9c

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

.github/workflows/formatter.yml

Lines changed: 47 additions & 23 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
@@ -59,6 +76,21 @@ jobs:
5976
echo "EOF" >> $GITHUB_OUTPUT
6077
fi
6178
79+
- name: Commit and push changes
80+
id: commit-changes
81+
if: steps.formatter.outputs.modified != '0'
82+
run: |
83+
git config user.name "github-actions[bot]"
84+
git config user.email "github-actions[bot]@users.noreply.github.com"
85+
86+
git add -A
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+
6294
- name: Upload formatter diff
6395
if: steps.formatter.outputs.modified != '0'
6496
uses: actions/upload-artifact@v4
@@ -68,15 +100,14 @@ jobs:
68100
retention-days: 30
69101

70102
- name: Find existing comment
71-
if: steps.formatter.outputs.modified != '0'
72103
uses: peter-evans/find-comment@v3
73104
id: find-comment
74105
with:
75106
issue-number: ${{ github.event.pull_request.number }}
76107
comment-author: 'github-actions[bot]'
77108
body-includes: '<!-- tc-formatter -->'
78109

79-
- name: Create or update comment
110+
- name: Create or update comment for committed changes
80111
if: steps.formatter.outputs.modified != '0'
81112
uses: peter-evans/create-or-update-comment@v4
82113
with:
@@ -85,26 +116,19 @@ jobs:
85116
edit-mode: replace
86117
body: |
87118
<!-- 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'
93-
94-
- To see a complete report of formatting issues, download the [differences artifact](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
119+
### ✅ Formatter Auto-Applied
95120
96-
- To fix the build, please run `mvn spotless:apply` in your branch and commit the changes.
121+
Formatting issues were detected and **automatically fixed**. Changes have been committed to this PR.
97122
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:
123+
The formatting changes affected **${{ steps.formatter.outputs.modified }} files**.
103124
125+
Files modified:
104126
```
105127
${{ steps.formatter.outputs.files }}
106128
```
107129
130+
Please pull the latest changes before continuing work on this branch.
131+
108132
- name: Delete comment if formatting is correct
109133
if: steps.formatter.outputs.modified == '0' && steps.find-comment.outputs.comment-id != ''
110134
uses: actions/github-script@v7
@@ -116,8 +140,8 @@ jobs:
116140
comment_id: ${{ steps.find-comment.outputs.comment-id }}
117141
})
118142
119-
- name: Fail if formatting issues exist
120-
if: steps.formatter.outputs.modified != '0'
143+
- name: Set output for no changes
144+
if: steps.formatter.outputs.modified == '0'
121145
run: |
122-
echo "::error::There are ${{ steps.formatter.outputs.modified }} files with format errors"
123-
exit 1
146+
echo "changes_committed=false" >> $GITHUB_OUTPUT
147+
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)