-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters