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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ twigcs /path/to/views --reporter emacs
twigcs /path/to/views --reporter json
twigcs /path/to/views --reporter csv
twigcs /path/to/views --reporter githubAction
twigcs /path/to/views --reporter gitlab
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
```

## Using older twig versions
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-ctype": "*",
"ext-hash": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-simplexml": "*",
Expand Down
6 changes: 6 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
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\Reporter\ReporterInterface;
use FriendsOfTwig\Twigcs\Validator\Validator;

class Container extends \ArrayObject
Expand Down Expand Up @@ -43,6 +45,10 @@ public function __construct()
return new GithubActionReporter(new ConsoleReporter());
};

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

$this['lexer'] = function () {
return new Lexer();
};
Expand Down
65 changes: 65 additions & 0 deletions src/Reporter/GitLabReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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;

final class GitLabReporter implements ReporterInterface
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* {@inheritdoc}
*
* @param Violation[] $violations
*
* @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));
}
}
78 changes: 78 additions & 0 deletions tests/Reporter/GitLabReporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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;

final class GitLabReporterTest extends TestCase
{
public const EXPECTED_REPORT = <<<EOF
[
{
"description": "You are not allowed to do that.",
"fingerprint": "f72519b40b7ced7c6475d20ae6a0f4d26014891d98a29750dde4931e22745796",
"severity": "major",
"location": {
"path": "template.twig",
"lines": {
"begin": 10
}
}
},
{
"description": "You should not do that.",
"fingerprint": "3068464e6cfb4c459076c2b5e910b8d99a80bbbadb1ff1e3e1551a71352ca3e2",
"severity": "minor",
"location": {
"path": "template.twig",
"lines": {
"begin": 10
}
}
},
{
"description": "You might not want to do that.",
"fingerprint": "b2b519c15c4819fe59ccff92544b35c1dd331a41d0ec138dc3f6b966d0362187",
"severity": "info",
"location": {
"path": "template.twig",
"lines": {
"begin": 10
}
}
}
]
EOF;

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.'),
new Violation('template.twig', 10, 20, 'You should not do that.', Violation::SEVERITY_WARNING),
new Violation('template.twig', 10, 20, 'You might not want to do that.', Violation::SEVERITY_INFO),
]);
}

public function testReportWithJsonException(): void
{
$reporter = new GitLabReporter();
$output = $this->createMock(ConsoleOutput::class);

$this->expectException(JsonException::class);
$reporter->report($output, [new Violation('template.twig', 10, 20, "Error message with latin1 character \xE7")]);
frankdekker marked this conversation as resolved.
Show resolved Hide resolved
}
}