|
36 | 36 | check-format:
|
37 | 37 | name: "Check if code needs formatting"
|
38 | 38 | 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 |
39 | 42 | steps:
|
40 | 43 | - name: "Checkout"
|
41 | 44 | uses: actions/checkout@v4
|
@@ -64,15 +67,45 @@ jobs:
|
64 | 67 | echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
|
65 | 68 | 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
|
66 | 69 | 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 |
69 | 72 | 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 |
71 | 78 | 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 |
76 | 109 | license:
|
77 | 110 | name: "Check License"
|
78 | 111 | needs: check-format
|
|
0 commit comments