rtCamp / action-phpcs-code-review

Github Action to perform automated code review on pull requests
https://github.com/rtCamp/github-actions-library
MIT License
101 stars 26 forks source link

Correct path for custom ruleset inclusion? #54

Closed davegreenwp closed 2 years ago

davegreenwp commented 2 years ago

Hello!

We are using this action as part of our workflow, and we're having trouble getting PHPCS to locate the WooCommerce ruleset that we are pulling into the project repository via composer.

Here is our phpcs.xml file which is located in the root of the repository, and refers to vendor/woocommerce/woocommerce-sniffs/src/WooCommerce-Core/ruleset.xml which is the ruleset we are having difficulties with.

<?xml version="1.0"?>
<ruleset name="WooCommerce Coding Standards">
    <description>Generally-applicable sniffs for WooCommerce plugins</description>

    <!-- Configs -->
    <config name="minimum_supported_wp_version" value="4.7"/>
    <config name="testVersion" value="5.6-"/>

    <!-- Rules -->
    <rule ref="WordPress">
        <exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
        <exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
    </rule>

    <rule ref="vendor/woocommerce/woocommerce-sniffs/src/WooCommerce-Core/ruleset.xml"/>
    <rule ref="PHPCompatibility"/>

    <!-- Args -->
    <arg name="extensions" value="php"/>
    <arg value="s"/>

    <!-- Excludes -->
    <exclude-pattern>*/dev-lib/*</exclude-pattern>
    <exclude-pattern>*/node_modules/*</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>
    <exclude-pattern>.github/</exclude-pattern>
</ruleset>

Every time the action runs, we're getting the following:

"ERROR: Referenced sniff \"..\/..\/..\/..\/github-workspace\/vendor\/woocommerce\/woocommerce-sniffs\/src\/WooCommerce-Core\/ruleset.xml\" does not exist\n\nRun \"phpcs --help\" for usage information"

I have tried a number of different variations on the path, and have also tried referencing our repository copy of phpcs but none of these attempts have yielded any results.

Any guidance on how to correctly resolve this path in the context of the environment the action runs in would be greatly appreciated!

P.S As an aside, it may be worth adding the WooCommerce ruleset to the list of already included standards?

mrrobot47 commented 2 years ago

@namespace-dave the error Referenced sniff ... does not exist occurs when the action is not able to find the referenced ruleset in the given path.

Is the vendor directory present when the PHPCS action is running? If not, it needs to be installed in the workflow. Also, as you are using custom coding standard, the path for PHPCS in action needs to be updated.

Ideally, here is how your workflow file should look like to run this properly:

on: pull_request
name: Inspections
jobs:
  runPHPCSInspection:
    name: Run PHPCS inspection
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.event.pull_request.head.sha }}

    # Setup PHP with required version and add extensions if needed any for composer install.
    # Ref: https://github.com/shivammathur/setup-php#heavy_plus_sign-php-extension-support
    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.0'
        coverage: none
        tools: composer

    # Install composer dependencies in the workflow run.
    - name: Composer install
      run: composer install --no-dev --no-progress --no-interaction

    - name: Run PHPCS inspection
      uses: rtCamp/action-phpcs-code-review@master
      env:
        GH_BOT_TOKEN: ${{ secrets.RTBOT_TOKEN }}
        # Add the phpcs path to pickup custom coding standard
        # Ref: https://github.com/rtCamp/action-phpcs-code-review/#custom-coding-standards
        PHPCS_FILE_PATH: vendor/bin/phpcs

Also, along with this, add .vipgoci_phpcs_skip_folders file with .github, vendor and other directories inside it for properly skipping them during the workflow run. Ref: https://github.com/rtCamp/action-phpcs-code-review/#skipping-phpcs-scanning-for-specific-folders. Do not use the env variable SKIP_FOLDERS for custom coding standard based setup.

davegreenwp commented 2 years ago

thanks for the help @mrrobot47, a combination of a few tweaks from your suggestions and referencing WooCommerce-Core as the sniff to include rather than passing in a direct path has resolved the issue. 🎉