Skip to content

fix: fix issue #266 #78

fix: fix issue #266

fix: fix issue #266 #78

Workflow file for this run

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!
![badge](https://img.shields.io/badge/Spotless-Passed-success?logo=checkmarx&style=for-the-badge)
**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
![badge](https://img.shields.io/badge/Spotless-Failed-critical?logo=checkmarx&style=for-the-badge)
**${{ 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