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

Appearance settings

[TwigBridge] LintCommand supports Github Actions annotations #39826

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

Merged
merged 1 commit into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[TwigBridge] LintCommand supports Github Actions annotations
  • Loading branch information
YaFou committed Jun 27, 2021
commit 202eb26ea0c748cc9dcccf62f5116f89d60b6897
6 changes: 6 additions & 0 deletions 6 src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

5.4
---

* Add `github` format & autodetection to render errors as annotations when
running the Twig linter command in a Github Actions environment.

5.3
---

Expand Down
28 changes: 23 additions & 5 deletions 28 src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Twig\Command;

use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
Expand Down Expand Up @@ -39,6 +40,11 @@ class LintCommand extends Command

private $twig;

/**
YaFou marked this conversation as resolved.
Show resolved Hide resolved
* @var string|null
*/
private $format;

public function __construct(Environment $twig)
{
parent::__construct();
Expand All @@ -50,7 +56,7 @@ protected function configure()
{
$this
->setDescription(self::$defaultDescription)
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format')
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors')
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -80,6 +86,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io = new SymfonyStyle($input, $output);
$filenames = $input->getArgument('filename');
$showDeprecations = $input->getOption('show-deprecations');
$this->format = $input->getOption('format');

if (null === $this->format) {
YaFou marked this conversation as resolved.
Show resolved Hide resolved
$this->format = GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
}

if (['-'] === $filenames) {
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), uniqid('sf_', true))]);
Expand Down Expand Up @@ -169,26 +180,29 @@ private function validate(string $template, string $file): array

private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files)
{
switch ($input->getOption('format')) {
switch ($this->format) {
case 'txt':
return $this->displayTxt($output, $io, $files);
case 'json':
return $this->displayJson($output, $files);
case 'github':
return $this->displayTxt($output, $io, $files, true);
default:
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
}
}

private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo)
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)
{
$errors = 0;
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($output) : null;

foreach ($filesInfo as $info) {
if ($info['valid'] && $output->isVerbose()) {
$io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
} elseif (!$info['valid']) {
++$errors;
$this->renderException($io, $info['template'], $info['exception'], $info['file']);
$this->renderException($io, $info['template'], $info['exception'], $info['file'], $githubReporter);
}
}

Expand Down Expand Up @@ -220,10 +234,14 @@ private function displayJson(OutputInterface $output, array $filesInfo)
return min($errors, 1);
}

private function renderException(SymfonyStyle $output, string $template, Error $exception, string $file = null)
private function renderException(SymfonyStyle $output, string $template, Error $exception, string $file = null, ?GithubActionReporter $githubReporter = null)
{
$line = $exception->getTemplateLine();

if ($githubReporter) {
$githubReporter->error($exception->getRawMessage(), $file, $line <= 0 ? null : $line);
}

if ($file) {
$output->text(sprintf('<error> ERROR </error> in %s (line %s)', $file, $line));
} else {
Expand Down
27 changes: 27 additions & 0 deletions 27 src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ public function testLintDefaultPaths()
self::assertStringContainsString('OK in', trim($tester->getDisplay()));
}

public function testLintIncorrectFileWithGithubFormat()
{
$filename = $this->createFile('{{ foo');
$tester = $this->createCommandTester();
$tester->execute(['filename' => [$filename], '--format' => 'github'], ['decorated' => false]);
self::assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
self::assertStringMatchesFormat('%A::error file=%s,line=1,col=0::Unexpected token "end of template" ("end of print statement" expected).%A', trim($tester->getDisplay()));
}

public function testLintAutodetectsGithubActionEnvironment()
{
$prev = getenv('GITHUB_ACTIONS');
putenv('GITHUB_ACTIONS');

try {
putenv('GITHUB_ACTIONS=1');

$filename = $this->createFile('{{ foo');
$tester = $this->createCommandTester();

$tester->execute(['filename' => [$filename]], ['decorated' => false]);
self::assertStringMatchesFormat('%A::error file=%s,line=1,col=0::Unexpected token "end of template" ("end of print statement" expected).%A', trim($tester->getDisplay()));
} finally {
putenv('GITHUB_ACTIONS'.($prev ? "=$prev" : ''));
}
}

private function createCommandTester(): CommandTester
{
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"symfony/security-http": "^4.4|^5.0|^6.0",
"symfony/serializer": "^5.2|^6.0",
"symfony/stopwatch": "^4.4|^5.0|^6.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/console": "^5.3|^6.0",
"symfony/expression-language": "^4.4|^5.0|^6.0",
"symfony/web-link": "^4.4|^5.0|^6.0",
"symfony/workflow": "^5.2|^6.0",
Expand All @@ -55,7 +55,7 @@
"conflict": {
"phpdocumentor/reflection-docblock": "<3.2.2",
"phpdocumentor/type-resolver": "<1.4.0",
"symfony/console": "<4.4",
"symfony/console": "<5.3",
"symfony/form": "<5.3",
"symfony/http-foundation": "<5.3",
"symfony/http-kernel": "<4.4",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.