Skip to content

Commit

Permalink
CodeClimate output format
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyArt committed Jan 26, 2025
1 parent fcd141b commit 48be2ad
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
132 changes: 132 additions & 0 deletions src/ChangesReporting/Output/GitlabOutputFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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\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';

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() ?? '')
;

$errorsJson[] = [
'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() ?? '')
;

$errorsJson[] = [
'type' => self::ERROR_TYPE_ISSUE,
'categories' => [self::ERROR_CATEGORY_STYLE],
'description' => $fileDiff->getDiff(),
'check_name' => implode(' / ', $fileDiff->getRectorShortClasses()),
'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 48be2ad

Please sign in to comment.