From 1decb7733296cbab21fe29b77a6f8d651d0b2dfa Mon Sep 17 00:00:00 2001 From: Paul Kevan Date: Tue, 21 May 2024 15:16:29 +0100 Subject: [PATCH] Add new PHPCS config --- .github/bin/phpcs-branch.php | 79 ++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 4 ++ 2 files changed, 83 insertions(+) create mode 100644 .github/bin/phpcs-branch.php diff --git a/.github/bin/phpcs-branch.php b/.github/bin/phpcs-branch.php new file mode 100644 index 0000000..45ff9da --- /dev/null +++ b/.github/bin/phpcs-branch.php @@ -0,0 +1,79 @@ +#!/usr/bin/php + $name.diff" ); + exec( "$git show $base_branch:$file | $bin_dir/phpcs --standard=./phpcs.xml.dist --report=json -nq > $name.orig.phpcs" ); + exec( "cat $file | $bin_dir/phpcs --standard=./phpcs.xml.dist --report=json -nq > $name.phpcs" ); + + $cmd = "$bin_dir/phpcs-changed --diff $name.diff --phpcs-orig $name.orig.phpcs --phpcs-new $name.phpcs"; + exec( $cmd, $output, $exec_exit_status ); + echo implode( "\n", $output ); + echo "\n"; + + exec( "rm $name.diff $name.orig.phpcs $name.phpcs" ); + return $exec_exit_status; +} + +function main() { + $base_branch = 'remotes/origin/' . getenv( 'BASE_REF' ); + $git_dir = dirname( dirname( __DIR__ ) ); + $bin_dir = dirname( dirname( __DIR__ ) ) . '/public_html/wp-content/mu-plugins/vendor/bin'; + $git = "git -C $git_dir"; + + try { + echo "\nScanning changed files...\n"; + $status = 0; + + $affected_files = shell_exec( "$git diff $base_branch --name-status --diff-filter=AM 2>&1 | grep .php$" ); + $affected_files = explode( "\n", trim( $affected_files ) ); + + foreach ( $affected_files as $record ) { + if ( ! $record ) { + continue; + } + + list( $change, $file ) = explode( "\t", trim( $record ) ); + $cmd_status = 0; + + switch ( $change ) { + case 'M': + $cmd_status = run_phpcs_changed( $file, $git, $base_branch, $bin_dir ); + break; + + case 'A': + $cmd_status = run_phpcs( $file, $bin_dir ); + break; + } + + // If any cmd exits with 1, we want to exit with 1. + $status |= $cmd_status; + } + + } catch ( Exception $exception ) { + echo "\nAborting because of error: {$exception->getMessage()} \n"; + $status = 1; + + } + + exit( $status ); +} + +main(); diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa6452f..aff4eb1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,6 +43,10 @@ jobs: composer install --prefer-dist --no-suggest --no-progress --no-ansi --no-interaction echo "${PWD}/mu-plugins/vendor/bin" >> $GITHUB_PATH + - name: Lint PHP + run: | + BASE_REF=${{ github.base_ref }} php .github/bin/phpcs-branch.php + phpunit: name: PHP Unit Tests