-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathGitlabOutputFormatter.php
More file actions
153 lines (127 loc) · 4.55 KB
/
GitlabOutputFormatter.php
File metadata and controls
153 lines (127 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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
{
private const string NAME = 'gitlab';
private const string ERROR_TYPE_ISSUE = 'issue';
private const string ERROR_CATEGORY_BUG_RISK = 'Bug Risk';
private const string ERROR_CATEGORY_STYLE = 'Style';
private const string ERROR_SEVERITY_BLOCKER = 'blocker';
private const string 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 $systemError) {
$filePath = $configuration->isReportingWithRealPath()
? ($systemError->getAbsoluteFilePath() ?? '')
: ($systemError->getRelativeFilePath() ?? '')
;
$fingerprint = $this->filehasher->hash(
$filePath . ';' . $systemError->getLine() . ';' . $systemError->getMessage()
);
$errorsJson[] = [
'fingerprint' => $fingerprint,
'type' => self::ERROR_TYPE_ISSUE,
'categories' => [self::ERROR_CATEGORY_BUG_RISK],
'severity' => self::ERROR_SEVERITY_BLOCKER,
'description' => $systemError->getMessage(),
'check_name' => $systemError->getRectorClass() ?? '',
'location' => [
'path' => $filePath,
'lines' => [
'begin' => $systemError->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;
}
}