Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gitlab reporter #243

Merged
merged 12 commits into from
May 13, 2022
5 changes: 5 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FriendsOfTwig\Twigcs\Reporter\CsvReporter;
use FriendsOfTwig\Twigcs\Reporter\EmacsReporter;
use FriendsOfTwig\Twigcs\Reporter\GithubActionReporter;
use FriendsOfTwig\Twigcs\Reporter\GitlabReporter;
use FriendsOfTwig\Twigcs\Reporter\JsonReporter;
use FriendsOfTwig\Twigcs\Reporter\JUnitReporter;
use FriendsOfTwig\Twigcs\Validator\Validator;
Expand Down Expand Up @@ -43,6 +44,10 @@ public function __construct()
return new GithubActionReporter(new ConsoleReporter());
};

$this['reporter.gitlab'] = function () {
return new GitlabReporter();
};

$this['lexer'] = function () {
return new Lexer();
};
Expand Down
63 changes: 63 additions & 0 deletions src/Reporter/GitlabReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);

namespace FriendsOfTwig\Twigcs\Reporter;

use FriendsOfTwig\Twigcs\Validator\Violation;
use JsonException;
use Symfony\Component\Console\Output\OutputInterface;
use function hash;
use function implode;
use function json_encode;

class GitlabReporter implements ReporterInterface
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* {@inheritdoc}
* @param Violation[] $violations
*
localheinz marked this conversation as resolved.
Show resolved Hide resolved
* @throws JsonException
*/
public function report(OutputInterface $output, array $violations): void
{
$errors = [];

foreach ($violations as $violation) {
switch($violation->getSeverity()) {
case Violation::SEVERITY_INFO:
$severity = 'info';
break;
case Violation::SEVERITY_WARNING:
$severity = 'minor';
break;
default:
$severity = 'major';
break;
}

$errors[] = [
'description' => $violation->getReason(),
'fingerprint' => hash(
'sha256',
implode(
[
$violation->getFilename(),
$violation->getLine(),
$violation->getColumn(),
$violation->getReason(),
]
)
),
'severity' => $severity,
'location' => [
'path' => $violation->getFilename(),
'lines' => [
'begin' => $violation->getLine(),
],
],
];
}

$output->writeln(json_encode($errors, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
}
}
48 changes: 48 additions & 0 deletions tests/Reporter/GitlabReporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace FriendsOfTwig\Twigcs\Tests\Reporter;

use FriendsOfTwig\Twigcs\Reporter\GitlabReporter;
use FriendsOfTwig\Twigcs\Validator\Violation;
use JsonException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\ConsoleOutput;

class GitlabReporterTest extends TestCase
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
{
public const EXPECTED_REPORT = <<<EOF
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
[
{
"description": "You are not allowed to do that.",
"fingerprint": "f72519b40b7ced7c6475d20ae6a0f4d26014891d98a29750dde4931e22745796",
"severity": "major",
"location": {
"path": "template.twig",
"lines": {
"begin": 10
}
}
}
]
EOF;

/**
* @throws JsonException
*/
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
public function testReport(): void
{
$reporter = new GitlabReporter();
$output = $this->createMock(ConsoleOutput::class);

$output
->expects($this->once())
->method('writeln')
->with(self::EXPECTED_REPORT)
;

$reporter->report($output, [
new Violation('template.twig', 10, 20, 'You are not allowed to do that.'),
]);
}
}