Skip to content

Commit 0353e3e

Browse files
committed
Fix code formatting issues on pull request
1 parent 621323e commit 0353e3e

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

.github/workflows/build-and-test-pr.yml

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
check-format:
3737
name: "Check if code needs formatting"
3838
runs-on: ubuntu-latest
39+
permissions:
40+
# Give the default GITHUB_TOKEN write permission to commit the changed files back to the repository.
41+
contents: write
3942
steps:
4043
- name: "Checkout"
4144
uses: actions/checkout@v4
@@ -64,15 +67,45 @@ jobs:
6467
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
6568
cat mvn.log | grep "ERROR" | sed 's/Check if code needs formatting Check if code aligns with code style [0-9A-Z:.-]\+//' | sed 's/\[ERROR] //' | head -n -11 >> $GITHUB_STEP_SUMMARY
6669
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
67-
echo "Please run \`mvn spotless:apply\` to fix the formatting issues." >> $GITHUB_STEP_SUMMARY
68-
- name: "Fail if code needs formatting"
70+
- name: "Fix code formatting issues by using spotless:apply"
71+
id: apply
6972
if: ${{ steps.check.outcome == 'failure' }}
70-
uses: actions/github-script@v7.0.1
73+
run: mvn --log-file spotless.log spotless:apply
74+
continue-on-error: true
75+
- name: "Upload spotless apply result"
76+
if: ${{ steps.check.outcome == 'failure' }}
77+
uses: actions/upload-artifact@v4
7178
with:
72-
github-token: ${{ secrets.GITHUB_TOKEN }}
73-
script: |
74-
core.setFailed("Formatting issues found! \nRun \`mvn spotless:apply\` to fix.")
75-
79+
name: spotless-result
80+
path: spotless.log
81+
- name: "Generate Summary for successful spotless:apply"
82+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' }}
83+
run: |
84+
echo ":ballot_box_with_check: Kudos! Code formatting issues are fixed by using spotless:apply!" >> $GITHUB_STEP_SUMMARY
85+
- name: "Generate Summary for failed spotless:apply"
86+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'failure' }}
87+
run: |
88+
echo "## :negative_squared_cross_mark: Code formatting issues aren't fixed by using spotless:apply!" >> $GITHUB_STEP_SUMMARY
89+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
90+
cat spotless.log | grep "ERROR" | sed 's/\[ERROR] //' | head -n -11 >> $GITHUB_STEP_SUMMARY
91+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
92+
- name: "Commit code formatting changes back to the PR branch"
93+
id: commit
94+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' }}
95+
uses: stefanzweifel/git-auto-commit-action@v5
96+
with:
97+
commit_message: "Apply code formatting changes with Spotless"
98+
env:
99+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
continue-on-error: true
101+
- name: "Generate Summary for successful commit"
102+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' && steps.commit.outcome == 'success'}}
103+
run: |
104+
echo ":ballot_box_with_check: Kudos! Code formatting changes are committed!" >> $GITHUB_STEP_SUMMARY
105+
- name: "Generate Summary for failed commit"
106+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' && steps.commit.outcome == 'failure'}}
107+
run: |
108+
echo "## :negative_squared_cross_mark: Code formatting changes aren't committed!" >> $GITHUB_STEP_SUMMARY
76109
license:
77110
name: "Check License"
78111
needs: check-format

0 commit comments

Comments
 (0)