staabm/annotate-pull-request-from-checkstyle
GitHub Action that annotates pull requests with Checkstyle-compatible reports. Converts XML/JSON output into inline review comments so code style, lint, and static analysis issues appear directly on changed lines during PR review.
Installation Add the package to your Laravel project via Composer:
composer require --dev staabm/annotate-pull-request-from-checkstyle
GitHub Action Integration
cs2pr CLI tool in your project’s dev-tools or .github directory (or use a direct download in your workflow).php-cs-fixer or pint).First Use Case Trigger annotations in a GitHub PR by running:
./vendor/bin/cs2pr \
--token "$GITHUB_TOKEN" \
--repo "$GITHUB_REPOSITORY" \
--pr "$PR_NUMBER" \
--file "path/to/checkstyle-result.xml"
Laravel-Specific: Use this in a GitHub Action (e.g., php-cs-fixer.yml) after running static analysis tools.
CI Pipeline Hook
cs2pr after php-cs-fixer or pint in your GitHub Actions workflow:
- name: Run Checkstyle
run: vendor/bin/php-cs-fixer fix --diff --rules=@PSR12 --allow-risky=yes
- name: Generate Report
run: vendor/bin/php-cs-fixer fix --diff --format=checkstyle > checkstyle.xml
- name: Annotate PR
uses: actions/github-script@v6
with:
script: |
const { execSync } = require('child_process');
execSync(`./vendor/bin/cs2pr --token "${{ secrets.GITHUB_TOKEN }}" --repo "${{ github.repository }}" --pr "${{ github.event.pull_request.number }}" --file "checkstyle.xml"`);
Laravel-Specific Adjustments
php-cs-fixer config (php-cs-fixer.dist.php) to include Laravel-specific rules (e.g., no_unused_imports, ordered_imports).php artisan cs-fix --diff --format=checkstyle > checkstyle.xml
Conditional Annotations
main branch or non-PR commits by checking github.event_name in your workflow.--dedupe flag to avoid spamming PRs with duplicate annotations.warning to notice) via CLI flags or a config file.vendor/, storage/) in Checkstyle rules:
<module name="ExcludeFile">
<property name="file" value=".*(vendor|storage)/.*"/>
</module>
Token Permissions
GITHUB_TOKEN has pull-requests: write scope. Add to workflow:
permissions:
pull-requests: write
XML Report Format
cs2pr fails silently if the Checkstyle XML is malformed. Validate with:
xmllint checkstyle.xml --noout
cs2pr may not post anything. Add a fallback:
[ -s checkstyle.xml ] && ./vendor/bin/cs2pr ... || echo "No issues found!"
Rate Limits
--dry-run:
./vendor/bin/cs2pr --dry-run --file checkstyle.xml
./vendor/bin/cs2pr --verbose ...
lineLength) may flood PRs. Tune in php-cs-fixer.dist.php:
return PhpCsFixer\Config::create()
->setRules([
'@PSR12' => true,
'line_ending' => ['line_ending' => 'lf'],
'no_unused_imports' => true,
]);
Custom Annotations
cs2pr CLI or using a wrapper script to preprocess the XML.Laravel-Specific Rules
laravel-missing-migration-return). Example:
<module name="CustomRule">
<property name="message" value="Missing return in migration method"/>
<property name="regexp" value="defineschema.*\n\s+public function.*\n\s+\{.*\n\s+\}"/>
</module>
Webhook Triggers
cs2pr on push, not just PR events, for pre-merge checks.Local Development
github3.py or mockoon.How can I help you explore Laravel packages today?