fix: fix issue #266 #78
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Code Formatting Check | |
| on: | |
| pull_request_target: | |
| branches: [main] | |
| types: [opened, synchronize, reopened] | |
| paths-ignore: | |
| - 'website/**' | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | |
| cancel-in-progress: true | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| checks: write | |
| jobs: | |
| spotless: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| fetch-depth: 0 # Important pour git diff | |
| - uses: actions/setup-java@v3 | |
| with: | |
| java-version: 21 | |
| distribution: temurin | |
| - uses: actions/cache@v4 | |
| with: | |
| path: ~/.m2 | |
| key: ${{runner.os}}-m2-${{hashFiles('**/pom.xml')}} | |
| restore-keys: ${{runner.os}}-m2 | |
| - uses: reviewdog/action-setup@v1 | |
| # π― Compare only changes from the PR | |
| - name: Generate formatting diff | |
| id: spotless | |
| run: | | |
| git fetch origin ${{ github.event.pull_request.base.ref }} | |
| CHANGED_JAVA_FILES=$(git diff --name-only --diff-filter=ACMR \ | |
| origin/${{ github.event.pull_request.base.ref }}...HEAD \ | |
| | grep '\.java$' || true) | |
| if [ -z "$CHANGED_JAVA_FILES" ]; then | |
| echo "No Java files changed in this PR" | |
| echo "FILES_COUNT=0" >> "$GITHUB_OUTPUT" | |
| touch spotless.diff | |
| exit 0 | |
| fi | |
| echo "Changed Java files:" | |
| echo "$CHANGED_JAVA_FILES" | |
| mvn spotless:apply || true | |
| git diff -- $CHANGED_JAVA_FILES > spotless.diff | |
| FORMATTED_FILES=$(git diff --name-only -- $CHANGED_JAVA_FILES | wc -l) | |
| echo "FILES_COUNT=$FORMATTED_FILES" >> "$GITHUB_OUTPUT" | |
| if [ -s spotless.diff ]; then | |
| echo "Formatting issues found:" | |
| cat spotless.diff | |
| fi | |
| git checkout . | |
| - name: Post suggestions | |
| if: hashFiles('spotless.diff') != '' | |
| continue-on-error: true | |
| env: | |
| REVIEWDOG_GITHUB_API_TOKEN: ${{secrets.GITHUB_TOKEN}} | |
| run: | | |
| cat spotless.diff | reviewdog \ | |
| -f=diff \ | |
| -f.diff.strip=1 \ | |
| -name="πSpotless - Format" \ | |
| -reporter=github-pr-review \ | |
| -filter-mode=diff_context \ | |
| -level=error | |
| - name: Find previous bot comment | |
| if: always() | |
| id: find-comment | |
| uses: peter-evans/find-comment@v3 | |
| with: | |
| issue-number: ${{ github.event.pull_request.number }} | |
| comment-author: 'github-actions[bot]' | |
| body-includes: '<!-- spotless-check-comment -->' | |
| - name: Post success comment | |
| if: steps.spotless.outputs.FILES_COUNT == '0' | |
| uses: peter-evans/create-or-update-comment@v4 | |
| with: | |
| issue-number: ${{ github.event.pull_request.number }} | |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
| edit-mode: replace | |
| body: |- | |
| <!-- spotless-check-comment --> | |
| ## β Code Formatting Check Passed! | |
|  | |
| **Great job!** π Your code follows the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). | |
| --- | |
| π€ _The DinoBot Team_ π¦ | |
| - name: Post failure comment | |
| if: steps.spotless.outputs.FILES_COUNT != '0' | |
| uses: peter-evans/create-or-update-comment@v4 | |
| with: | |
| issue-number: ${{ github.event.pull_request.number }} | |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
| edit-mode: replace | |
| body: |- | |
| <!-- spotless-check-comment --> | |
| ## π¨ Code Formatting Issues | |
|  | |
| **${{ steps.spotless.outputs. FILES_COUNT }}** Java file(s) need formatting. | |
| ### π‘ Quick fix | |
| **Click the suggestions below:** | |
| 1. Go to **"Files changed"** tab | |
| 2. Click **`β Commit suggestion`** on each **[π Spotless]** comment | |
| **Or run locally:** | |
| ```bash | |
| mvn spotless:apply | |
| git add -A && git commit -m "style: apply Spotless" && git push | |
| ``` | |
| <details> | |
| <summary>π More options</summary> | |
| **Auto-format in your IDE:** | |
| - **IntelliJ**: Install [google-java-format plugin](https://plugins.jetbrains.com/plugin/8527-google-java-format) | |
| - **VS Code**: [Java formatting settings](https://code.visualstudio.com/docs/java/java-linting) | |
| **Style guide:** [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) | |
| </details> | |
| --- | |
| π€ _The DinoBot Team_ π¦ | |
| - name: Fail if formatting issues | |
| if: steps.spotless.outputs.FILES_COUNT != '0' | |
| run: | | |
| echo "::error::Code formatting issues detected. Please fix them before merging." | |
| exit 1 |