diff --git a/src/ChangesReporting/Output/GitlabOutputFormatter.php b/src/ChangesReporting/Output/GitlabOutputFormatter.php new file mode 100644 index 00000000000..45be1766045 --- /dev/null +++ b/src/ChangesReporting/Output/GitlabOutputFormatter.php @@ -0,0 +1,150 @@ +appendSystemErrors($processResult, $configuration), + ...$this->appendFileDiffs($processResult, $configuration), + ]; + + $json = Json::encode($errorsJson, true); + + echo $json . PHP_EOL; + } + + /** + * @return array + */ + private function appendSystemErrors(ProcessResult $processResult, Configuration $configuration): array + { + $errorsJson = []; + + foreach ($processResult->getSystemErrors() as $error) { + $filePath = $configuration->isReportingWithRealPath() + ? ($error->getAbsoluteFilePath() ?? '') + : ($error->getRelativeFilePath() ?? '') + ; + + $fingerprint = $this->filehasher->hash($filePath . ';' . $error->getLine() . ';' . $error->getMessage()); + + $errorsJson[] = [ + 'fingerprint' => $fingerprint, + 'type' => self::ERROR_TYPE_ISSUE, + 'categories' => [self::ERROR_CATEGORY_BUG_RISK], + 'severity' => self::ERROR_SEVERITY_BLOCKER, + 'description' => $error->getMessage(), + 'check_name' => $error->getRectorClass() ?? '', + 'location' => [ + 'path' => $filePath, + 'lines' => [ + 'begin' => $error->getLine() ?? 0, + ], + ], + ]; + } + + return $errorsJson; + } + + /** + * @return array + */ + private function appendFileDiffs(ProcessResult $processResult, Configuration $configuration): array + { + $errorsJson = []; + + $fileDiffs = $processResult->getFileDiffs(); + ksort($fileDiffs); + + foreach ($fileDiffs as $fileDiff) { + $filePath = $configuration->isReportingWithRealPath() + ? ($fileDiff->getAbsoluteFilePath() ?? '') + : ($fileDiff->getRelativeFilePath() ?? '') + ; + + $rectorClasses = implode(' / ', $fileDiff->getRectorShortClasses()); + $fingerprint = $this->filehasher->hash($filePath . ';' . $fileDiff->getDiff()); + + $errorsJson[] = [ + 'fingerprint' => $fingerprint, + 'type' => self::ERROR_TYPE_ISSUE, + 'categories' => [self::ERROR_CATEGORY_STYLE], + 'severity' => self::ERROR_SEVERITY_MINOR, + 'description' => $rectorClasses, + 'content' => [ + 'body' => $fileDiff->getDiff(), + ], + 'check_name' => $rectorClasses, + 'location' => [ + 'path' => $filePath, + 'lines' => [ + 'begin' => $fileDiff->getFirstLineNumber() ?? 0, + ], + ], + ]; + } + + return $errorsJson; + } +} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 3f7231cb054..49101e6d1ff 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -37,6 +37,7 @@ use Rector\Caching\Cache; use Rector\Caching\CacheFactory; use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; +use Rector\ChangesReporting\Output\GitlabOutputFormatter; use Rector\ChangesReporting\Output\ConsoleOutputFormatter; use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper; @@ -329,7 +330,11 @@ final class LazyContainerFactory /** * @var array> */ - private const OUTPUT_FORMATTER_CLASSES = [ConsoleOutputFormatter::class, JsonOutputFormatter::class]; + private const OUTPUT_FORMATTER_CLASSES = [ + ConsoleOutputFormatter::class, + JsonOutputFormatter::class, + GitlabOutputFormatter::class, + ]; /** * @var array>