Skip to content

Commit da3746a

Browse files
committed
feature #4288 Add Gitlab Reporter (hco)
This PR was squashed before being merged into the 2.15-dev branch (closes #4288). Discussion ---------- Add Gitlab Reporter Gitlab does support a subset of the CodeClimate JSON Format. I initially called the Reporter "CodeClimate", but that would "require" more fields that we currently cannot fill appropriately from a report generator, I think. So I decided to rename it to "Gitlab Reporter". It will result in a pull request annotation like this: ![image](https://user-images.githubusercontent.com/156839/52041883-f8dc5c00-253b-11e9-9f04-07faa7eaa351.png) I will be happy to maintain this reporter, if problems arise. In a next step I would be happy to add meaningful line numbers, if someone can assist me on that, but after reading #3601 it does not seem to be so easy, and I think the current state is enough for an "MVP" ;-) Commits ------- 5b54f5c Add Gitlab Reporter
2 parents 01b8475 + 5b54f5c commit da3746a

File tree

5 files changed

+182
-3
lines changed

5 files changed

+182
-3
lines changed

src/Report/GitlabReporter.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Report;
14+
15+
use Symfony\Component\Console\Formatter\OutputFormatter;
16+
17+
/**
18+
* Generates a report according to gitlabs subset of codeclimate json files.
19+
*
20+
* @author Hans-Christian Otto <c.otto@suora.com>
21+
*
22+
* @internal
23+
*/
24+
final class GitlabReporter implements ReporterInterface
25+
{
26+
public function getFormat()
27+
{
28+
return 'gitlab';
29+
}
30+
31+
/**
32+
* Process changed files array. Returns generated report.
33+
*
34+
* @param ReportSummary $reportSummary
35+
*
36+
* @return string
37+
*/
38+
public function generate(ReportSummary $reportSummary)
39+
{
40+
$report = [];
41+
foreach ($reportSummary->getChanged() as $fileName => $change) {
42+
foreach ($change['appliedFixers'] as $fixerName) {
43+
$report[] = [
44+
'description' => $fixerName,
45+
'location.path' => $fileName,
46+
'fingerprint' => md5($fileName.$fixerName),
47+
'location.lines.begin' => 0, // line numbers are required in the format, but not available to reports
48+
];
49+
}
50+
}
51+
52+
$jsonString = json_encode($report);
53+
54+
return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($jsonString) : $jsonString;
55+
}
56+
}

tests/Console/ConfigurationResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public function testResolveConfigFileChooseFileWithInvalidFile()
287287
public function testResolveConfigFileChooseFileWithInvalidFormat()
288288
{
289289
$this->expectException(InvalidConfigurationException::class);
290-
$this->expectExceptionMessageRegExp('/^The format "xls" is not defined, supported are "checkstyle", "json", "junit", "txt", "xml"\.$/');
290+
$this->expectExceptionMessageRegExp('/^The format "xls" is not defined, supported are "checkstyle", "gitlab", "json", "junit", "txt", "xml"\.$/');
291291

292292
$dirBase = $this->getFixtureDir();
293293

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Tests\Report;
14+
15+
use PhpCsFixer\Report\GitlabReporter;
16+
17+
/**
18+
* @author Hans-Christian Otto <c.otto@suora.com>
19+
*
20+
* @internal
21+
* @covers \PhpCsFixer\Report\GitlabReporter
22+
*/
23+
final class GitlabReporterTest extends AbstractReporterTestCase
24+
{
25+
protected function createReporter()
26+
{
27+
return new GitlabReporter();
28+
}
29+
30+
protected function getFormat()
31+
{
32+
return 'gitlab';
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
protected function createNoErrorReport()
39+
{
40+
return '[]';
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
protected function createSimpleReport()
47+
{
48+
return <<<'JSON'
49+
[{
50+
"description": "some_fixer_name_here",
51+
"location.path": "someFile.php",
52+
"location.lines.begin": 0,
53+
"fingerprint": "ad098ea6ea7a28dd85dfcdfc9e2bded0"
54+
}]
55+
JSON;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
protected function createWithDiffReport()
62+
{
63+
return $this->createSimpleReport();
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
protected function createWithAppliedFixersReport()
70+
{
71+
return <<<'JSON'
72+
[{
73+
"description": "some_fixer_name_here_1",
74+
"location.path": "someFile.php",
75+
"location.lines.begin": 0,
76+
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff"
77+
},{
78+
"description": "some_fixer_name_here_2",
79+
"location.path": "someFile.php",
80+
"location.lines.begin": 0,
81+
"fingerprint": "acad4672140c737a83c18d1474d84074"
82+
}]
83+
JSON;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
protected function createWithTimeAndMemoryReport()
90+
{
91+
return $this->createSimpleReport();
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
protected function createComplexReport()
98+
{
99+
return <<<'JSON'
100+
[{
101+
"description": "some_fixer_name_here_1",
102+
"location.path": "someFile.php",
103+
"location.lines.begin": 0,
104+
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff"
105+
},{
106+
"description": "some_fixer_name_here_2",
107+
"location.path": "someFile.php",
108+
"location.lines.begin": 0,
109+
"fingerprint": "acad4672140c737a83c18d1474d84074"
110+
},{
111+
"description": "another_fixer_name_here",
112+
"location.path": "anotherFile.php",
113+
"location.lines.begin": 0,
114+
"fingerprint": "30e86e533dac0f1b93bbc3a55c6908f8"
115+
}]
116+
JSON;
117+
}
118+
119+
protected function assertFormat($expected, $input)
120+
{
121+
$this->assertJsonStringEqualsJsonString($expected, $input);
122+
}
123+
}

tests/Report/ReporterFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testRegisterBuiltInReports()
5151

5252
$builder->registerBuiltInReporters();
5353
$this->assertSame(
54-
['checkstyle', 'json', 'junit', 'txt', 'xml'],
54+
['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
5555
$builder->getFormats()
5656
);
5757
}

tests/TextDiffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testAllFormatsCovered()
9494
sort($formats);
9595

9696
$this->assertSame(
97-
['checkstyle', 'json', 'junit', 'txt', 'xml'],
97+
['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
9898
$formats
9999
);
100100
}

0 commit comments

Comments
 (0)