Fix Code Style #2
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: Fix Code Style | |
on: | |
workflow_dispatch: | |
permissions: | |
contents: write | |
pull-requests: write | |
issues: write | |
jobs: | |
fix-code-style: | |
name: Fix Code Style | |
runs-on: ubuntu-latest | |
# Only run if the PR is from a branch in the same repository (not from a fork) | |
if: github.event.pull_request.head.repo.full_name == github.repository | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
ref: ${{ github.head_ref }} | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: '8.4' | |
extensions: mbstring, hash, sodium | |
- name: Install dependencies | |
uses: ramsey/composer-install@v3 | |
- name: Check if code style fixes are needed | |
id: check | |
run: composer lint | |
continue-on-error: true | |
- name: Set fixes needed flag | |
id: fixes | |
run: | | |
if [ "${{ steps.check.outcome }}" = "success" ]; then | |
echo "fixes_needed=false" >> $GITHUB_OUTPUT | |
echo "✅ No code style fixes needed" | |
else | |
echo "fixes_needed=true" >> $GITHUB_OUTPUT | |
echo "🔧 Code style fixes needed" | |
fi | |
- name: Fix code style | |
if: steps.fixes.outputs.fixes_needed == 'true' | |
run: composer fix | |
- name: Check for changes | |
if: steps.fixes.outputs.fixes_needed == 'true' | |
id: changes | |
run: | | |
if [ -n "$(git diff --name-only)" ]; then | |
echo "changes_made=true" >> $GITHUB_OUTPUT | |
echo "📝 Code style changes made:" | |
git diff --name-only | |
else | |
echo "changes_made=false" >> $GITHUB_OUTPUT | |
echo "ℹ️ No changes made after running Pint" | |
fi | |
- name: Commit and push fixes | |
if: steps.fixes.outputs.fixes_needed == 'true' && steps.changes.outputs.changes_made == 'true' | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add . | |
git commit -m "🎨 Fix code style with Laravel Pint | |
Automatically fixed code style violations using Laravel Pint. | |
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>" | |
git push | |
- name: Comment on PR | |
if: steps.fixes.outputs.fixes_needed == 'true' && steps.changes.outputs.changes_made == 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const message = `## 🎨 Code Style Auto-Fix | |
I've automatically fixed code style violations in this PR using Laravel Pint. | |
**Changes made:** | |
- Applied PSR-12 code style standards | |
- Fixed formatting and spacing issues | |
- Organized imports | |
The fixes have been committed to this PR. You can review the changes in the latest commit. | |
--- | |
*This comment was automatically generated by the code style fix workflow.*`; | |
// Check if we already commented on this PR | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
}); | |
const botComment = comments.data.find(comment => | |
comment.user.login === 'github-actions[bot]' && | |
comment.body.includes('Code Style Auto-Fix') | |
); | |
if (botComment) { | |
// Update existing comment | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: message | |
}); | |
} else { | |
// Create new comment | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
body: message | |
}); | |
} |