Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99020f8

Browse files
spawniaondrejmirtes
authored andcommitted
Include identifier in gitlab error format
As described in the example in https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format: ```json [ { "description": "'unused' is assigned a value but never used.", "check_name": "no-unused-vars", "fingerprint": "7815696ecbf1c96e6894b779456d330e", "severity": "minor", "location": { "path": "lib/index.js", "lines": { "begin": 42 } } } ] ``` GitLab currently has a bug that prevents `check_name` from showing, see https://gitlab.com/gitlab-org/gitlab/-/issues/578172. Thus, an explanatory comment and a TODO for its eventual removal were added.
1 parent d1f76fc commit 99020f8

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

‎src/Command/ErrorFormatter/GitlabErrorFormatter.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use function implode;
1313

1414
/**
15-
* @see https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool
15+
* @see https://docs.gitlab.com/ci/testing/code_quality#code-quality-report-format
1616
*/
1717
#[AutowiredService(name: 'errorFormatter.gitlab')]
1818
final class GitlabErrorFormatter implements ErrorFormatter
@@ -32,6 +32,12 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
3232
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
3333
$error = [
3434
'description' => $fileSpecificError->getMessage(),
35+
// This field is intended to be shown in the GitLab code quality scan widget and mentioned in the GitLab docs.
36+
// However, due to a regression in GitLab, it is currently not shown - see https://gitlab.com/gitlab-org/gitlab/-/issues/578172.
37+
// There is no harm in having it here, and it allows users to work around the GitLab bug by post-processing the JSON file.
38+
// For example, they might prepend `check_name` to `description`, causing GitLab to show the error identifier as part of the message.
39+
// TODO: remove this comment once the GitLab bug is fixed
40+
'check_name' => $fileSpecificError->getIdentifier(),
3541
'fingerprint' => hash(
3642
'sha256',
3743
implode(

‎tests/PHPStan/Command/ErrorFormatter/GitlabFormatterTest.php‎ renamed to ‎tests/PHPStan/Command/ErrorFormatter/GitlabErrorFormatterTest.php‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use function sprintf;
99

10-
class GitlabFormatterTest extends ErrorFormatterTestCase
10+
class GitlabErrorFormatterTest extends ErrorFormatterTestCase
1111
{
1212

1313
public static function dataFormatterOutputProvider(): iterable
@@ -28,6 +28,7 @@ public static function dataFormatterOutputProvider(): iterable
2828
'[
2929
{
3030
"description": "Foo",
31+
"check_name": null,
3132
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
3233
"severity": "major",
3334
"location": {
@@ -68,6 +69,7 @@ public static function dataFormatterOutputProvider(): iterable
6869
'[
6970
{
7071
"description": "Bar\nBar2",
72+
"check_name": null,
7173
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
7274
"severity": "major",
7375
"location": {
@@ -79,6 +81,7 @@ public static function dataFormatterOutputProvider(): iterable
7981
},
8082
{
8183
"description": "Foo",
84+
"check_name": null,
8285
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
8386
"severity": "major",
8487
"location": {
@@ -90,6 +93,7 @@ public static function dataFormatterOutputProvider(): iterable
9093
},
9194
{
9295
"description": "Foo<Bar>",
96+
"check_name": null,
9397
"fingerprint": "d7002959fc192c81d51fc41b0a3f240617a1aa35361867b5e924ae8d7fec39cb",
9498
"severity": "major",
9599
"location": {
@@ -101,6 +105,7 @@ public static function dataFormatterOutputProvider(): iterable
101105
},
102106
{
103107
"description": "Bar\nBar2",
108+
"check_name": null,
104109
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
105110
"severity": "major",
106111
"location": {
@@ -121,6 +126,7 @@ public static function dataFormatterOutputProvider(): iterable
121126
'[
122127
{
123128
"description": "Bar\nBar2",
129+
"check_name": null,
124130
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
125131
"severity": "major",
126132
"location": {
@@ -132,6 +138,7 @@ public static function dataFormatterOutputProvider(): iterable
132138
},
133139
{
134140
"description": "Foo",
141+
"check_name": null,
135142
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
136143
"severity": "major",
137144
"location": {
@@ -143,6 +150,7 @@ public static function dataFormatterOutputProvider(): iterable
143150
},
144151
{
145152
"description": "Bar\nBar2",
153+
"check_name": null,
146154
"fingerprint": "52d22d9e64bd6c6257b7a0d170ed8c99482043aeedd68c52bac081a80da9800a",
147155
"severity": "major",
148156
"location": {
@@ -154,6 +162,7 @@ public static function dataFormatterOutputProvider(): iterable
154162
},
155163
{
156164
"description": "Foo<Bar>",
165+
"check_name": null,
157166
"fingerprint": "d7002959fc192c81d51fc41b0a3f240617a1aa35361867b5e924ae8d7fec39cb",
158167
"severity": "major",
159168
"location": {
@@ -165,6 +174,7 @@ public static function dataFormatterOutputProvider(): iterable
165174
},
166175
{
167176
"description": "Bar\nBar2",
177+
"check_name": null,
168178
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
169179
"severity": "major",
170180
"location": {
@@ -216,6 +226,7 @@ public static function dataFormatterOutputProvider(): iterable
216226
'[
217227
{
218228
"description": "Bar\nBar2",
229+
"check_name": null,
219230
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
220231
"severity": "major",
221232
"location": {
@@ -227,6 +238,7 @@ public static function dataFormatterOutputProvider(): iterable
227238
},
228239
{
229240
"description": "Foo",
241+
"check_name": null,
230242
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
231243
"severity": "major",
232244
"location": {
@@ -238,6 +250,7 @@ public static function dataFormatterOutputProvider(): iterable
238250
},
239251
{
240252
"description": "Foo<Bar>",
253+
"check_name": null,
241254
"fingerprint": "d7002959fc192c81d51fc41b0a3f240617a1aa35361867b5e924ae8d7fec39cb",
242255
"severity": "major",
243256
"location": {
@@ -249,6 +262,7 @@ public static function dataFormatterOutputProvider(): iterable
249262
},
250263
{
251264
"description": "Bar\nBar2",
265+
"check_name": null,
252266
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
253267
"severity": "major",
254268
"location": {
@@ -280,15 +294,39 @@ public static function dataFormatterOutputProvider(): iterable
280294
}
281295
}
282296
}
297+
]',
298+
];
299+
300+
yield [
301+
'File error with identifier',
302+
1,
303+
[5, 1],
304+
0,
305+
'[
306+
{
307+
"description": "Foobar\\\\Buz",
308+
"check_name": "foobar.buz",
309+
"fingerprint": "5543f2fd4c455f26c1500342ec5045b08c99b7827ab35595151dd6e7715c079c",
310+
"severity": "major",
311+
"location": {
312+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
313+
"lines": {
314+
"begin": 5
315+
}
316+
}
317+
}
283318
]',
284319
];
285320
}
286321

322+
/**
323+
* @param array{int, int}|int $numFileErrors
324+
*/
287325
#[DataProvider('dataFormatterOutputProvider')]
288326
public function testFormatErrors(
289327
string $message,
290328
int $exitCode,
291-
int $numFileErrors,
329+
array|int $numFileErrors,
292330
int $numGenericErrors,
293331
string $expected,
294332
): void

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /