Skip to content

Commit 26bc5c3

Browse files
committed
Fix code formatting issues on pull request
1 parent 621323e commit 26bc5c3

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ 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 and push the
41+
# added or changed files to the repository.
42+
contents: write
3943
steps:
4044
- name: "Checkout"
4145
uses: actions/checkout@v4
46+
# Checkout the PR branch at the fork repository for git-auto-commit-action, see details at https://github.com/stefanzweifel/git-auto-commit-action/issues/211
47+
with:
48+
repository: ${{ github.event.pull_request.head.repo.full_name }}
49+
ref: ${{ github.head_ref }}
4250
- name: "Setup maven"
4351
uses: actions/setup-java@v4
4452
with:
@@ -64,15 +72,45 @@ jobs:
6472
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
6573
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
6674
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"
75+
- name: "Fix code formatting issues by using spotless:apply"
76+
id: apply
77+
if: ${{ steps.check.outcome == 'failure' }}
78+
run: mvn --log-file spotless.log spotless:apply
79+
continue-on-error: true
80+
- name: "Upload spotless apply result"
6981
if: ${{ steps.check.outcome == 'failure' }}
70-
uses: actions/github-script@v7.0.1
82+
uses: actions/upload-artifact@v4
7183
with:
72-
github-token: ${{ secrets.GITHUB_TOKEN }}
73-
script: |
74-
core.setFailed("Formatting issues found! \nRun \`mvn spotless:apply\` to fix.")
75-
84+
name: spotless-result
85+
path: spotless.log
86+
- name: "Generate Summary for successful spotless:apply"
87+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' }}
88+
run: |
89+
echo ":ballot_box_with_check: Kudos! Code formatting issues are fixed by using spotless:apply!" >> $GITHUB_STEP_SUMMARY
90+
- name: "Generate Summary for failed spotless:apply"
91+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'failure' }}
92+
run: |
93+
echo "## :negative_squared_cross_mark: Code formatting issues aren't fixed by using spotless:apply!" >> $GITHUB_STEP_SUMMARY
94+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
95+
cat spotless.log | grep "ERROR" | sed 's/\[ERROR] //' | head -n -11 >> $GITHUB_STEP_SUMMARY
96+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
97+
- name: "Commit code formatting changes back to the PR branch"
98+
id: commit
99+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' }}
100+
uses: stefanzweifel/git-auto-commit-action@v5
101+
with:
102+
commit_message: "Apply code formatting changes with Spotless"
103+
env:
104+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
continue-on-error: true
106+
- name: "Generate Summary for successful commit"
107+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' && steps.commit.outcome == 'success'}}
108+
run: |
109+
echo ":ballot_box_with_check: Kudos! Code formatting changes are committed!" >> $GITHUB_STEP_SUMMARY
110+
- name: "Generate Summary for failed commit"
111+
if: ${{ steps.check.outcome == 'failure' && steps.apply.outcome == 'success' && steps.commit.outcome == 'failure'}}
112+
run: |
113+
echo "## :negative_squared_cross_mark: Code formatting changes aren't committed!" >> $GITHUB_STEP_SUMMARY
76114
license:
77115
name: "Check License"
78116
needs: check-format

0 commit comments

Comments
 (0)