Skip to content

Commit

Permalink
CodeClimate output format (#6697)
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyArt authored Jan 28, 2025
1 parent d3deb35 commit da5685d
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 1 deletion.
150 changes: 150 additions & 0 deletions src/ChangesReporting/Output/GitlabOutputFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* CodeClimate Specification:
* - https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md
*/

declare(strict_types=1);

namespace Rector\ChangesReporting\Output;

use Nette\Utils\Json;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Util\FileHasher;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\ProcessResult;

final readonly class GitlabOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
public const NAME = 'gitlab';

private const ERROR_TYPE_ISSUE = 'issue';
private const ERROR_CATEGORY_BUG_RISK = 'Bug Risk';
private const ERROR_CATEGORY_STYLE = 'Style';
private const ERROR_SEVERITY_BLOCKER = 'blocker';
private const ERROR_SEVERITY_MINOR = 'minor';

public function __construct(
private Filehasher $filehasher,
) {
}

public function getName(): string
{
return self::NAME;
}

public function report(ProcessResult $processResult, Configuration $configuration): void
{
$errorsJson = [
...$this->appendSystemErrors($processResult, $configuration),
...$this->appendFileDiffs($processResult, $configuration),
];

$json = Json::encode($errorsJson, true);

echo $json . PHP_EOL;
}

/**
* @return array<array{
* type: 'issue',
* categories: array{'Bug Risk'},
* severity: 'blocker',
* description: string,
* check_name: string,
* location: array{
* path: string,
* lines: array{
* begin: int,
* },
* },
* }>
*/
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<array{
* type: 'issue',
* categories: array{'Style'},
* description: string,
* check_name: string,
* location: array{
* path: string,
* lines: array{
* begin: int,
* },
* },
* }>
*/
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;
}
}
7 changes: 6 additions & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -329,7 +330,11 @@ final class LazyContainerFactory
/**
* @var array<class-string<OutputFormatterInterface>>
*/
private const OUTPUT_FORMATTER_CLASSES = [ConsoleOutputFormatter::class, JsonOutputFormatter::class];
private const OUTPUT_FORMATTER_CLASSES = [
ConsoleOutputFormatter::class,
JsonOutputFormatter::class,
GitlabOutputFormatter::class,
];

/**
* @var array<class-string<NodeTypeResolverInterface>>
Expand Down

0 comments on commit da5685d

Please sign in to comment.