• Stars
    star
    188
  • Rank 205,563 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

cs2pr - Annotate a GitHub Pull Request based on a Checkstyle XML-report within your GitHub Action

Annotate a Pull Request based on a Checkstyle XML-report

Continuous Integration Continuous Deployment

Turns checkstyle based XML-Reports into GitHub Pull Request Annotations via the Checks API. This script is meant for use within your GitHub Action.

That means you no longer search thru your GitHub Action logfiles. No need to interpret messages which are formatted differently with every tool. Instead you can focus on your Pull Request, and you don't need to leave the Pull Request area.

Logs Example

Context Example Images from https://github.com/mheap/phpunit-github-actions-printer

DEMO - See how Pull Request warnings/errors are rendered in action

Installation

Install the binary via composer

composer require staabm/annotate-pull-request-from-checkstyle --dev

💌 Give back some love

Consider supporting the project, so we can make this tool even better even faster for everyone.

Example Usage

cs2pr can be used on a already existing checkstyle-report xml-file. Alternatively you might use it in the unix-pipe notation to chain it into your existing cli command.

Run one of the following commands within your GitHub Action workflow:

Process a checkstyle formatted file

cs2pr /path/to/checkstyle-report.xml

Available Options

  • --graceful-warnings: Don't exit with error codes if there are only warnings
  • --colorize: Colorize the output. Useful if the same lint script should be used locally on the command line and remote on GitHub Actions. With this option, errors and warnings are better distinguishable on the command line and the output is still compatible with GitHub Annotations
  • --notices-as-warnings Converts notices to warnings. This can be useful because GitHub does not annotate notices.
  • --prepend-filename Prepend the filename to the output message
  • --prepend-source When the checkstyle generating tool provides a source attribute, prepend the source to the output message.

Pipe the output of another commmand

... works for any command which produces a checkstyle-formatted report.

Examples can bee seen below:

Using PHPStan

phpstan analyse --error-format=checkstyle | cs2pr

Phpstan 0.12.32 introduced native github actions support, therefore you might use this instead:

phpstan analyse

Using Psalm

psalm --output-format=checkstyle | cs2pr

Psalm even supports the required format natively, therefore you might use this instead:

psalm --output-format=github

Using PHP Coding Standards Fixer

php-cs-fixer fix --dry-run --format=checkstyle | cs2pr

Using PHP_CodeSniffer

phpcs --report=checkstyle -q /path/to/code | cs2pr

Note: the -q option means that no output will be shown in the action logs anymore. To see the output both in the PR as well as in the action logs, use two steps, like so:

      - name: Check PHP code style
        id: phpcs
        run: phpcs --report-full --report-checkstyle=./phpcs-report.xml

      - name: Show PHPCS results in PR
        if: ${{ always() && steps.phpcs.outcome == 'failure' }}
        run: cs2pr ./phpcs-report.xml

Using PHP Parallel Lint

vendor/bin/parallel-lint . --exclude vendor --checkstyle | cs2pr

Using Laravel Pint

- name: Show Pint results in PR
run: pint --test --format=checkstyle | cs2pr

Note: if you want to have both logs and annotations you need to run pint twice:

- name: Check PHP code style
id: cs-check
run: pint --test

- name: Generate Annotations on CS errors
if: failure() && steps.cs-check.outcome != 'success'
run: pint --test --format=checkstyle | cs2pr

phpunit support?

PHPUnit does not support checkstyle, therefore cs2pr will not work for you.

you might instead try

Example GithubAction workflow

If you're using shivammathur/setup-php to setup PHP, cs2pr binary is shipped within:

# ...
jobs:
    phpstan-analysis:
      name: phpstan static code analysis
      runs-on: ubuntu-latest
      steps:
          - uses: actions/checkout@v2
          - name: Setup PHP
            uses: shivammathur/setup-php@v1
            with:
                php-version: 7.3
                coverage: none # disable xdebug, pcov
                tools: cs2pr
          - run: |
                composer install # install your apps dependencies
                vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr

If you use a custom PHP installation, then your project needs to require staabm/annotate-pull-request-from-checkstyle

# ...
jobs:
    phpstan-analysis:
      name: phpstan static code analysis
      runs-on: ubuntu-latest
      steps:
          - uses: actions/checkout@v2
          - name: Setup PHP
            run: # custom PHP installation 
          - run: |
                composer install # install your apps dependencies
                composer require staabm/annotate-pull-request-from-checkstyle # install cs2pr
                vendor/bin/phpstan analyse --error-format=checkstyle | vendor/bin/cs2pr

Resources

GithubAction Problem Matchers

Idea

This script is based on a suggestion of Benjamin Eberlei

The Code is inspired by https://github.com/mheap/phpunit-github-actions-printer

More Repositories

1

phpstan-dba

PHPStan based SQL static analysis and type inference for the database access layer
PHP
250
star
2

phpstan-todo-by

Todo comments with expiration
PHP
173
star
3

phpstan-baseline-analysis

Analyzes PHPStan baseline files and creates aggregated error trend-reports
PHP
73
star
4

staabm.github.io

Personal blog, about my open source activies.
HTML
7
star
5

soap-client-generator

7
star
6

oss-contribs

simple contributions statistics generator, find pull requests, referenced issues and reactions
PHP
6
star
7

thincache

Thin abstraction layer for often used cache backends.
PHP
4
star
8

redaxo-addon-installer

Composer installer package for redaxo5 cms addons
PHP
4
star
9

SchemaValidationBehavior

A Propel behavior which creates validation rules based on the database schema
PHP
4
star
10

phpstan-deployer6

phpstan stubs for Deployer version 6
4
star
11

SysMonitor

Monitors a php app and send notifications on certain error/exception/resource-exhausting/custom events
PHP
4
star
12

gh-annotation-example

example repo to show github annotations in pull requests
3
star
13

annotate-pull-request-from-checkstyle-action

cs2pr GitHub Action
Shell
3
star
14

staabm

3
star
15

http-cache-control

http cache-control tools, to ease http handling for common caching use-cases
PHP
3
star
16

secure_dotenv

A secure .env handler with encrypted key/value storage
PHP
3
star
17

rector-view-scope

PHP
2
star
18

zf-select-strip

PHP
2
star
19

AnyWhereInDB

Needle in a Haystack: Comprehensive Search on MYSQL Database
PHP
2
star
20

cronschedule

App which transforms a cronexpression into a schedule table
PHP
1
star
21

gh-pr-payload

1
star
22

phpstan-repro813

1
star
23

complex-library-installer

Composer installer package for Complex proprietary rocket framework
PHP
1
star
24

phpunit-cross-os

contains a few tools usefull for making your test-expectations agnostic to operating system specifics
PHP
1
star
25

rector-repro1

PHP
1
star
26

hungry-spl-file-info

PHP
1
star