Giter VIP home page Giter VIP logo

staabm / annotate-pull-request-from-checkstyle Goto Github PK

View Code? Open in Web Editor NEW
187.0 6.0 23.0 122 KB

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

Home Page: https://staabm.github.io/archive.html#cs2pr

License: MIT License

PHP 100.00%
cs2pr checkstyle-xml-report githubaction checkstyle psalm githubaction-workflow annotations phpstan php-cs-fixer phpunit

annotate-pull-request-from-checkstyle's Introduction

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
php-cs-fixer fix --dry-run --format=checkstyle | cs2pr
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
vendor/bin/parallel-lint . --exclude vendor --checkstyle | cs2pr
- 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

Using cs2pr as a GitHub Action

You can also use cs2pr itself as a GitHub Action. This is useful if you want to for instance use it for a project that does not use PHP or if you want to use it with a custom PHP installation.

See the example at the cs2pr GitHub Action repositiory.

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

annotate-pull-request-from-checkstyle's People

Contributors

allenjb avatar clxmstaab avatar donatj avatar edhgoose avatar glensc avatar greg0ire avatar jampire avatar jrfnl avatar kevinpapst avatar korelstar avatar localheinz avatar mnapoli avatar prisis avatar sasezaki avatar shivammathur avatar staabm avatar tristanbes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

annotate-pull-request-from-checkstyle's Issues

Report checkstyle empty error with phan.

Hello, thanks for your time, excellent tool, you know how I can solve this error, thanks.

Run phan --no-progress-bar --output-mode checkstyle | cs2pr --graceful-warnings --colorize
Error: Expecting xml stream starting with a xml opening tag.

##[error]Process completed with exit code 2.

Filename not shown

I'm using cs2pr with php-cs-fixer. I have installed verison 1.7.1 via the shivammathur/setup-php@v2 action.

Screenshot 2021-11-03 at 16 52 18

I use the --dry-run --using-cache=no --format=checkstyle so that I can just see what is wrong and then I would manually fix it with another commit. However, if something is wrong, I only see the message and not the filename

Screenshot 2021-11-03 at 16 55 15

The actual XML report is

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
  <file name="CareLineLive/app/Http/Controllers/Exports/CarersExportController.php">
    <error severity="warning" source="PHP-CS-Fixer.no_unused_imports" message="Found violation(s) of type: no_unused_imports"/>
  </file>
</checkstyle>

I don't think this is expected behaviour, right?

False positive with PHP Cs fixer 2

Hello,

Running vendor/bin/php-cs-fixer fix --dry-run --format=xml | cs2pr return error code 1.
Here's the output :

Loaded config default from "/home/ubuntu/projects/perso/multistream-tools/.php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
::warning file=,line=::

Problems with empty check style report

First of all: thanks for your tool!

I am running into an issue while setting up a GitHub action in case php-cs-fixer doesn't find any problem. If there is an issue in the code, it is properly annotated and reported in the PR.

But when running cs2pr piped from php-cs-fixer on sources that do not report any issue, I get this message:

  Run php-cs-fixer fix --dry-run --verbose --config=.php_cs.dist --using-cache=no --show-progress=none --format=checkstyle | cs2pr 14s

##[error]Process completed with exit code 2.
Run php-cs-fixer fix --dry-run --verbose --config=.php_cs.dist --using-cache=no --show-progress=none --format=checkstyle | cs2pr
Loaded config default from ".php_cs.dist".
unknown error. expecting checkstyle formatted xml input.
##[error]Process completed with exit code 2.

Running php-cs-fixer on a clean code base reports:

$ php-cs-fixer fix --dry-run --format=checkstyle --config=.php_cs.dist -v --stop-on-violation --using-cache=no
Loaded config default from ".php_cs.dist".
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle/>

Running this:

$ php -r '$a = simplexml_load_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><checkstyle/>"); if (!$a) { var_dump($a); $b = libxml_get_errors(); if (!$b) { var_dump($b); } }'

leads to:

Command line code:1:
class SimpleXMLElement#1 (0) {
}
Command line code:1:
array(0) {
}

So I believe your recent changes here are not catching the case of an empty <checkstyle/> block.

EDIT: This test fails if you change its content from

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
</checkstyle>

to

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle/>

::notice does not annotate

While the Checks API supports notices, workflow commands do not. Outputting these does not annotate anything, they're just another line in the action log (and they're not even dressed up there.) Short of using the checks api, you might consider promoting these to warnings, ignoring them completely, or giving the user the option what to do.

If you go the user option route it might be useful to allow them to specify which severities map to which types of annotation. While valid values for checkstyle's @severity appear to be ignore, info, warning, and error I don't know how well other tools hold to those (the list wasn't super easy to find and doesn't mention the xml format.)

No output in docker-compose

Hey there,

we use docker-compose image: docker/compose in our github actions. If we use this tool in our github action we will get no output.

- name: "Run [phpstan]"
  run: |
      docker-compose exec -T php sh -c "php phpstan analyse --verbose --no-progress --memory-limit=4000M --configuration=phpstan.neon --error-format=checkstyle | cs2pr"

Are we doing something wrong or is docker-compose not supported?

phpcs triggers errors with warning

Hello,

bin/phpcs --report=checkstyle --runtime-set ignore_warnings_on_exit 1 | cs2pr

It generates the output (only warnings):

##[warning]Always use identical comparison unless you need type juggling
##[warning]Always use identical comparison unless you need type juggling
##[warning]Always use identical comparison unless you need type juggling
##[warning]Always use identical comparison unless you need type juggling
##[warning]Always use identical comparison unless you need type juggling
##[warning]Member variable "commonNlv3Manager" contains numbers but this is discouraged
##[error]Process completed with exit code 1.

Warning should be ignored because of the flag --runtime-set ignore_warnings_on_exit 1

I'm not really sure if it's a bug with cs2pr or with phpcs itself combined with the report checkstyle

Add check for <?xml in sdtin input

Hey, maybe my use case is a bit special.

Im have all dev tools in a different folder, and using the --working-dir from composer to call the tools.

use case:

composer --working-dir=./.build cs:check -- --format=checkstyle
> php-cs-fixer fix --config="./../.php_cs" --dry-run '--format=checkstyle'
Loaded config narrowspark from "./../.php_cs".
Using cache file "/var/www/package/.build/php-cs-fixer/.php_cs.cache".
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle/>

error:

Start tag expected, '<' not found
 on line 1, column 1

Possible solution is to search for the <?xml ver.. position and remove everything before this input

Final annotations number does not match reported issues from logs.

Hi there, I'm using this to parse a phpcs checkstyle report using:

bin/phpcs -q --report=checkstyle src | cs2pr --graceful-warnings --notices-as-warnings --prepend-source

On my PR the action logs note 100's of issues, however the PR is only annotated for 10 errors and 11 warnings. I am rerunning this action to try different things but the checks/annotations number never changes.

When running locally I see a significant number of ::error and ::warning lines but these don't all seem to be making it in to the checks tab. Also the annotations that do go in are from a couple of random files in the tree, it's not like the first 10 in the report it just seems to be a few scattered throughout the report.

Workflows are referencing vulnerable actions

Hello, there!

As part of the university research we are currently doing regarding the security of Github Actions, we noticed that one or many of the workflows that are part of this repository are referencing vulnerable versions of the third-party actions. As part of a disclosure process, we decided to open issues to notify GitHub Community.

Please note that there are could be some false positives in our methodology, thus not all of the open issues could be valid. If that is the case, please let us know, so that we can improve on our approach. You can contact me directly using an email: ikoishy [at] ncsu.edu

Thanks in advance

  1. The workflow csfix.yml is referencing action shivammathur/setup-php using references v1. However this reference is missing the commit 7163319 which may contain fix to the vulnerability.

The vulnerability fix that is missing by actions' versions could be related to:
(1) CVE fix
(2) upgrade of vulnerable dependency
(3) fix to secret leak and others.
Please consider updating the reference to the action.

If you end up updating the reference, please let us know. We need the stats for the paper :-)

emit status report

when cs2pr completes and no errors/warnings have to be emitted, the screen stays empty.

as a user I dont know whether everything worked well and nothing had to be reported or there was a error and I dont see it.

at best we could emit a processed x errors and y warnings or similar status message at the very end into STDERR, so we dont destory possible consumers of our gihtub-api checks formatted output

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.