From 59d516d08efa003c8535698c7ad95d2255af9c80 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Mon, 23 Mar 2020 16:53:03 -0700 Subject: [PATCH 01/14] Create GithubActionsAnnotations.php --- src/Reports/GithubActionsAnnotations.php | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/Reports/GithubActionsAnnotations.php diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php new file mode 100644 index 0000000000..79d649b73a --- /dev/null +++ b/src/Reports/GithubActionsAnnotations.php @@ -0,0 +1,88 @@ + + * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Reports; + +use PHP_CodeSniffer\Files\File; + +class GithubActionsAnnotations implements Report +{ + + + /** + * Generate a partial report for a single processed file. + * + * Function should return TRUE if it printed or stored data about the file + * and FALSE if it ignored the file. Returning TRUE indicates that the file and + * its data should be counted in the grand totals. + * + * @param array $report Prepared report data. + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. + * @param bool $showSources Show sources? + * @param int $width Maximum allowed line width. + * + * @return bool + */ + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) + { + if ($report['errors'] === 0 && $report['warnings'] === 0) { + // Nothing to print. + return false; + } + + foreach ($report['messages'] as $line => $lineErrors) { + foreach ($lineErrors as $column => $colErrors) { + foreach ($colErrors as $error) { + $filename = str_replace('"', '\"', $report['filename']); + $message = str_replace('"', '\"', $error['message']); + $type = strtolower($error['type']); + $source = $error['source']; + echo "::{$type} file={$filename},line={$line},col=$column::{$message}\n$source".PHP_EOL; + } + } + } + + return true; + + }//end generateFileReport() + + + /** + * Generates a csv report. + * + * @param string $cachedData Any partial report data that was returned from + * generateFileReport during the run. + * @param int $totalFiles Total number of files processed during the run. + * @param int $totalErrors Total number of errors found during the run. + * @param int $totalWarnings Total number of warnings found during the run. + * @param int $totalFixable Total number of problems that can be fixed. + * @param bool $showSources Show sources? + * @param int $width Maximum allowed line width. + * @param bool $interactive Are we running in interactive mode? + * @param bool $toScreen Is the report being printed to screen? + * + * @return void + */ + public function generate( + $cachedData, + $totalFiles, + $totalErrors, + $totalWarnings, + $totalFixable, + $showSources=false, + $width=80, + $interactive=false, + $toScreen=true + ) { + // echo $cachedData; + + }//end generate() + + +}//end class From 120bfa7286d4ba942f30476caaff7e75c809cbe6 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Mon, 23 Mar 2020 17:02:51 -0700 Subject: [PATCH 02/14] echo ! --- src/Reports/GithubActionsAnnotations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index 79d649b73a..5f262b967f 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -80,7 +80,7 @@ public function generate( $interactive=false, $toScreen=true ) { - // echo $cachedData; + echo $cachedData; }//end generate() From 2ce5ee4050cf7c0bb85c01884afeebda0eae8fb7 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 25 Mar 2020 15:02:44 -0700 Subject: [PATCH 03/14] Show fixable errors as warnings --- src/Reports/GithubActionsAnnotations.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index 5f262b967f..3d1498a916 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -39,10 +39,14 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { - $filename = str_replace('"', '\"', $report['filename']); + // Note this does not correspond to PHPCS warning/error, rather is used to annotate which + // errors can be automatically fixed in CI and which need attention. + $type = (boolean) $error['fixable'] ? 'warning' : 'error'; + + $filename = str_replace('"', '\"', $report['filename']); $message = str_replace('"', '\"', $error['message']); - $type = strtolower($error['type']); $source = $error['source']; + echo "::{$type} file={$filename},line={$line},col=$column::{$message}\n$source".PHP_EOL; } } From 5235bae06e7716f8334495f0b04d578c846a7709 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Fri, 27 Mar 2020 22:03:55 -0700 Subject: [PATCH 04/14] Add multiline annotations --- src/Reports/GithubActionsAnnotations.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index 3d1498a916..a2a6f2b2ac 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -43,11 +43,13 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, // errors can be automatically fixed in CI and which need attention. $type = (boolean) $error['fixable'] ? 'warning' : 'error'; - $filename = str_replace('"', '\"', $report['filename']); - $message = str_replace('"', '\"', $error['message']); + $filename = $report['filename']; + $message = explode("\n", $error['message']); $source = $error['source']; - echo "::{$type} file={$filename},line={$line},col=$column::{$message}\n$source".PHP_EOL; + $log = $message . '%0A' . $source; + + echo "::{$type} file={$filename},line={$line},col=$column::{$log}".PHP_EOL; } } } From d141cd600b2a0e40881f99ddd0e6a4f261c73fb3 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Fri, 27 Mar 2020 22:04:06 -0700 Subject: [PATCH 05/14] Update doc --- src/Reports/GithubActionsAnnotations.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index a2a6f2b2ac..7f3e64e3fb 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -1,7 +1,8 @@ * @author Greg Sherwood * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence @@ -39,6 +40,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { + // Note this does not correspond to PHPCS warning/error, rather is used to annotate which // errors can be automatically fixed in CI and which need attention. $type = (boolean) $error['fixable'] ? 'warning' : 'error'; @@ -60,7 +62,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, /** - * Generates a csv report. + * Generates a report with lines parsable by GitHub Actions. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. From a52897e7ad8e87e7c2b01d82524912f28f036002 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 12:14:22 -0700 Subject: [PATCH 06/14] Fix GitHub capital H --- src/Reports/GithubActionsAnnotations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index 7f3e64e3fb..e872f1a1f7 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -12,7 +12,7 @@ use PHP_CodeSniffer\Files\File; -class GithubActionsAnnotations implements Report +class GitHubActionsAnnotations implements Report { From 0e830574468a5a707b71b14af7a8f1f819e7a6f6 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 12:47:27 -0700 Subject: [PATCH 07/14] string/array bug fixed --- src/Reports/GithubActionsAnnotations.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index e872f1a1f7..2e8578b8de 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -46,10 +46,8 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, $type = (boolean) $error['fixable'] ? 'warning' : 'error'; $filename = $report['filename']; - $message = explode("\n", $error['message']); - $source = $error['source']; + $log = $error['message'].'%0A'.$error['source']; - $log = $message . '%0A' . $source; echo "::{$type} file={$filename},line={$line},col=$column::{$log}".PHP_EOL; } From 4e1f4eda8e78edd2102971a0766a6c128cd29eb1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 12:47:34 -0700 Subject: [PATCH 08/14] PHPCS fixes --- src/Reports/GithubActionsAnnotations.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index 2e8578b8de..f3d6b69421 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -40,19 +40,22 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { + // Note this does not correspond to PHPCS warning/error, rather is used to annotate which + // errors can be automatically fixed in CI and which need attention. + if ($error['fixable'] === true) { + $type = 'warning'; + } else { + $type = 'error'; + } - // Note this does not correspond to PHPCS warning/error, rather is used to annotate which - // errors can be automatically fixed in CI and which need attention. - $type = (boolean) $error['fixable'] ? 'warning' : 'error'; + $filename = $report['filename']; - $filename = $report['filename']; $log = $error['message'].'%0A'.$error['source']; - - echo "::{$type} file={$filename},line={$line},col=$column::{$log}".PHP_EOL; + echo "::{$type} file={$filename},line={$line},col=$column::{$log}".PHP_EOL; } } - } + }//end foreach return true; @@ -86,7 +89,7 @@ public function generate( $interactive=false, $toScreen=true ) { - echo $cachedData; + echo $cachedData; }//end generate() From 782b13ccf8cc7469f630d2f05bc9eb1c4a7b37b0 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 12:52:17 -0700 Subject: [PATCH 09/14] Add blank line for legibility --- src/Reports/GithubActionsAnnotations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GithubActionsAnnotations.php index f3d6b69421..e706d231cb 100644 --- a/src/Reports/GithubActionsAnnotations.php +++ b/src/Reports/GithubActionsAnnotations.php @@ -50,7 +50,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, $filename = $report['filename']; - $log = $error['message'].'%0A'.$error['source']; + $log = $error['message'].'%0A%0A'.$error['source']; echo "::{$type} file={$filename},line={$line},col=$column::{$log}".PHP_EOL; } From db16dc21cb4a03b8d2f07097fa6945903da99138 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 18:47:47 -0700 Subject: [PATCH 10/14] Fix filename GitHub capitalisation --- ...{GithubActionsAnnotations.php => GitHubActionsAnnotations.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Reports/{GithubActionsAnnotations.php => GitHubActionsAnnotations.php} (100%) diff --git a/src/Reports/GithubActionsAnnotations.php b/src/Reports/GitHubActionsAnnotations.php similarity index 100% rename from src/Reports/GithubActionsAnnotations.php rename to src/Reports/GitHubActionsAnnotations.php From 46cdfdb5762a55bb598a1f3a28c366581133e552 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 18:48:12 -0700 Subject: [PATCH 11/14] Add GitHubActionsAnnotations to package.xml --- package.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/package.xml b/package.xml index a9293e0887..46175e2869 100644 --- a/package.xml +++ b/package.xml @@ -172,6 +172,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + From c4403fb23a185ca3688ca0430579c810e16c1396 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 19:58:08 -0700 Subject: [PATCH 12/14] Add limitations to doc --- src/Reports/GitHubActionsAnnotations.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Reports/GitHubActionsAnnotations.php b/src/Reports/GitHubActionsAnnotations.php index e706d231cb..2c7b294e44 100644 --- a/src/Reports/GitHubActionsAnnotations.php +++ b/src/Reports/GitHubActionsAnnotations.php @@ -2,6 +2,9 @@ /** * GitHub Actions annotations format report for PHP_CodeSniffer. * + * There is a maximum "10 warning annotations and 10 error annotations per [GitHub Actions workflow] step". + * @see https://github.community/t5/GitHub-Actions/Maximum-number-of-annotations-that-can-be-created-using-GitHub/m-p/40663/highlight/true#M4310 + * * @author Brian Henry * @author Greg Sherwood * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) From 5f8521705dcb486fd0a1ad3d6512cffb66bf0ff2 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 28 Mar 2020 23:21:21 -0700 Subject: [PATCH 13/14] @see annotation removed --- src/Reports/GitHubActionsAnnotations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reports/GitHubActionsAnnotations.php b/src/Reports/GitHubActionsAnnotations.php index 2c7b294e44..953c35d928 100644 --- a/src/Reports/GitHubActionsAnnotations.php +++ b/src/Reports/GitHubActionsAnnotations.php @@ -3,7 +3,7 @@ * GitHub Actions annotations format report for PHP_CodeSniffer. * * There is a maximum "10 warning annotations and 10 error annotations per [GitHub Actions workflow] step". - * @see https://github.community/t5/GitHub-Actions/Maximum-number-of-annotations-that-can-be-created-using-GitHub/m-p/40663/highlight/true#M4310 + * as per https://github.community/t5/GitHub-Actions/Maximum-number-of-annotations-that-can-be-created-using-GitHub/m-p/40663/highlight/true#M4310 * * @author Brian Henry * @author Greg Sherwood From 044bb6db198df756fa3571e61ccce6ab601ff841 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 31 Mar 2020 11:30:12 -0700 Subject: [PATCH 14/14] Set error/warning to true PHPCS error/warning --- src/Reports/GitHubActionsAnnotations.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Reports/GitHubActionsAnnotations.php b/src/Reports/GitHubActionsAnnotations.php index 953c35d928..95488561f7 100644 --- a/src/Reports/GitHubActionsAnnotations.php +++ b/src/Reports/GitHubActionsAnnotations.php @@ -43,13 +43,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { - // Note this does not correspond to PHPCS warning/error, rather is used to annotate which - // errors can be automatically fixed in CI and which need attention. - if ($error['fixable'] === true) { - $type = 'warning'; - } else { - $type = 'error'; - } + $type = strtolower($error['type']); $filename = $report['filename'];