diff --git a/conf/config.neon b/conf/config.neon index 2bdb6ef0e0..5f69929623 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -225,6 +225,7 @@ parameters: editorUrl: null editorUrlTitle: null errorFormat: null + githubErrorLevel: 'error' pro: dnsServers: - '1.1.1.1' @@ -424,6 +425,7 @@ parametersSchema: editorUrl: schema(string(), nullable()) editorUrlTitle: schema(string(), nullable()) errorFormat: schema(string(), nullable()) + githubErrorLevel: string() pro: structure([ dnsServers: schema(listOf(string()), min(1)), ]) @@ -2161,6 +2163,7 @@ services: class: PHPStan\Command\ErrorFormatter\GithubErrorFormatter arguments: relativePathHelper: @simpleRelativePathHelper + errorLevel: %githubErrorLevel% errorFormatter.teamcity: class: PHPStan\Command\ErrorFormatter\TeamcityErrorFormatter diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php index 03dc1aa8d0..c541fc8757 100644 --- a/src/Command/ErrorFormatter/GithubErrorFormatter.php +++ b/src/Command/ErrorFormatter/GithubErrorFormatter.php @@ -2,11 +2,13 @@ namespace PHPStan\Command\ErrorFormatter; +use InvalidArgumentException; use PHPStan\Command\AnalysisResult; use PHPStan\Command\Output; use PHPStan\File\RelativePathHelper; use function array_walk; use function implode; +use function in_array; use function sprintf; use function str_replace; @@ -19,8 +21,12 @@ class GithubErrorFormatter implements ErrorFormatter public function __construct( private RelativePathHelper $relativePathHelper, + private string $errorLevel = 'error', ) { + if (!in_array($errorLevel, ['notice', 'warning', 'error'], true)) { + throw new InvalidArgumentException(sprintf('Invalid error level "%s"', $errorLevel)); + } } public function formatErrors(AnalysisResult $analysisResult, Output $output): int @@ -40,7 +46,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in // see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448 $message = str_replace("\n", '%0A', $message); - $line = sprintf('::error %s::%s', implode(',', $metas), $message); + $line = sprintf('::%s %s::%s', $this->errorLevel, implode(',', $metas), $message); $output->writeRaw($line); $output->writeLineFormatted(''); @@ -51,7 +57,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in // see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448 $notFileSpecificError = str_replace("\n", '%0A', $notFileSpecificError); - $line = sprintf('::error ::%s', $notFileSpecificError); + $line = sprintf('::%s ::%s', $this->errorLevel, $notFileSpecificError); $output->writeRaw($line); $output->writeLineFormatted(''); diff --git a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php index b6a921e06c..21f28b36a8 100644 --- a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php @@ -18,6 +18,7 @@ public function dataFormatterOutputProvider(): iterable 0, 0, '', + 'error', ]; yield [ @@ -27,6 +28,7 @@ public function dataFormatterOutputProvider(): iterable 0, '::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo ', + 'error', ]; yield [ @@ -36,6 +38,7 @@ public function dataFormatterOutputProvider(): iterable 1, '::error ::first generic error ', + 'error', ]; yield [ @@ -48,6 +51,7 @@ public function dataFormatterOutputProvider(): iterable ::error file=foo.php,line=1,col=0::Foo ::error file=foo.php,line=5,col=0::Bar%0ABar2 ', + 'error', ]; yield [ @@ -58,6 +62,7 @@ public function dataFormatterOutputProvider(): iterable '::error ::first generic error ::error ::second generic error ', + 'error', ]; yield [ @@ -72,6 +77,17 @@ public function dataFormatterOutputProvider(): iterable ::error ::first generic error ::error ::second generic error ', + 'error', + ]; + + yield [ + 'One generic error as warning', + 1, + 0, + 1, + '::warning ::first generic error +', + 'warning', ]; } @@ -85,11 +101,13 @@ public function testFormatErrors( int $numFileErrors, int $numGenericErrors, string $expected, + string $errorLevel, ): void { $relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/'); $formatter = new GithubErrorFormatter( $relativePathHelper, + $errorLevel, ); $this->assertSame($exitCode, $formatter->formatErrors(