(CAAPP-471) Code Style Standard v4 #21
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: Auto-Format Dart Code | |
| on: | |
| push: | |
| branches: | |
| - '**' | |
| pull_request: | |
| branches: | |
| - '**' | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| auto-format: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Flutter | |
| uses: subosito/flutter-action@v2 | |
| with: | |
| flutter-version: '3.32.0' | |
| cache: true | |
| - name: Get dependencies | |
| run: flutter pub get | |
| - name: Auto-format and fix Dart code (if needed) | |
| id: format_step | |
| run: | | |
| echo "Running comprehensive code formatting and fixes..." | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| # Run the comprehensive auto-fix script | |
| echo "Running auto_fix_all.sh script..." | |
| chmod +x ./scripts/auto_fix_all.sh | |
| ./scripts/auto_fix_all.sh | |
| # Check if there are any changes to commit | |
| if git diff --quiet && git diff --staged --quiet; then | |
| echo "All code is properly formatted and follows style guidelines. Nothing to commit." | |
| echo "changes_made=false" >> $GITHUB_OUTPUT | |
| exit 0 | |
| else | |
| echo "Changes detected. Capturing file changes..." | |
| # Capture the list of changed files | |
| changed_files=$(git diff --name-only) | |
| echo "Changed files:" | |
| echo "$changed_files" | |
| # Count changes | |
| files_changed=$(echo "$changed_files" | wc -l) | |
| # Capture diff stats | |
| diff_stats=$(git diff --stat) | |
| echo "Changes detected. Staging and committing fixes..." | |
| # Add all changed files | |
| git add -A | |
| # Double-check there are changes to commit | |
| if git diff --staged --quiet; then | |
| echo "No changes to commit after staging." | |
| echo "changes_made=false" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| git commit -m "Auto-formatted and fixed code style issues [CI]" \ | |
| -m "Applied comprehensive code fixes including:" \ | |
| -m "- Dart formatting (100-character line length)" \ | |
| -m "- One-line if statement formatting" \ | |
| -m "- Trailing whitespace removal" \ | |
| -m "- Final newline enforcement" \ | |
| -m "" \ | |
| -m "Co-authored-by: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>" | |
| git push | |
| echo "Code fixes complete. Changes committed and pushed." | |
| git log --oneline -1 | |
| # Set output variables for the next step | |
| echo "changes_made=true" >> $GITHUB_OUTPUT | |
| echo "files_changed=$files_changed" >> $GITHUB_OUTPUT | |
| # Save changed files list to a temp file for the next step | |
| echo "$changed_files" > /tmp/changed_files.txt | |
| # Save diff stats | |
| echo "$diff_stats" > /tmp/diff_stats.txt | |
| fi | |
| - name: Summary of Changes | |
| if: github.event_name == 'pull_request' && steps.format_step.outputs.changes_made == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const issue_number = context.issue.number; | |
| const fs = require('fs'); | |
| try { | |
| // Read the changed files and diff stats | |
| const changedFiles = fs.readFileSync('/tmp/changed_files.txt', 'utf8').trim().split('\n').filter(f => f); | |
| const diffStats = fs.readFileSync('/tmp/diff_stats.txt', 'utf8').trim(); | |
| const filesChanged = '${{ steps.format_step.outputs.files_changed }}'; | |
| // Build the file list for the comment | |
| const fileList = changedFiles.length <= 10 | |
| ? changedFiles.map(file => ` • \`${file}\``).join('\n') | |
| : changedFiles.slice(0, 10).map(file => ` • \`${file}\``).join('\n') + `\n • ... and ${changedFiles.length - 10} more files`; | |
| const commentBody = `**Auto-formatting Applied** | |
| I've automatically applied code formatting and style fixes to this PR. | |
| ## Changes Made: | |
| - Dart formatting (100-character line length) | |
| - One-line if statement formatting | |
| - Trailing whitespace removal | |
| - Final newline enforcement | |
| ## Files Modified (${filesChanged} total): | |
| ${fileList} | |
| ## Summary: | |
| \`\`\` | |
| ${diffStats} | |
| \`\`\` | |
| The changes have been committed and are ready for review. No action needed on your part! 🎉 | |
| *This comment was automatically generated by the auto-formatting workflow.*`; | |
| await github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number, | |
| body: commentBody | |
| }); | |
| console.log(`PR comment added successfully for ${filesChanged} changed files`); | |
| } catch (error) { | |
| console.error('Error adding PR comment:', error.message); | |
| // Don't fail the workflow if commenting fails | |
| } |