8000 [Translation] XliffLintCommand supports Github Actions annotations by YaFou · Pull Request #39828 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] XliffLintCommand supports Github Actions annotations #39828

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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Translation/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 XLIFF linter command in a Github Actions environment.

5.3
---

Expand Down
22 changes: 18 additions & 4 deletions src/Symfony/Component/Translation/Command/XliffLintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Translation\Command;

use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected function configure()
$this
->setDescription(self::$defaultDescription)
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format')
->setHelp(<<<EOF
The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT
the first encountered syntax error.
Expand Down Expand Up @@ -86,6 +87,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->format = $input->getOption('format');
$this->displayCorrectFiles = $output->isVerbose();

if (null === $this->format) {
$this->format = GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
}

if (['-'] === $filenames) {
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
}
Expand Down Expand Up @@ -160,25 +165,34 @@ private function display(SymfonyStyle $io, array $files)
return $this->displayTxt($io, $files);
case 'json':
return $this->displayJson($io, $files);
case 'github':
return $this->displayTxt($io, $files, true);
default:
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
}
}

private function displayTxt(SymfonyStyle $io, array $filesInfo)
private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)
{
$countFiles = \count($filesInfo);
$erroredFiles = 0;
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($io) : null;

foreach ($filesInfo as $info) {
if ($info['valid'] && $this->displayCorrectFiles) {
$io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
} elseif (!$info['valid']) {
++$erroredFiles;
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
$io->listing(array_map(function ($error) {
$io->listing(array_map(function ($error) use ($info, $githubReporter) {
// general document errors have a '-1' line number
return -1 === $error['line'] ? $error['message'] : sprintf('Line %d, Column %d: %s', $error['line'], $error['column'], $error['message']);
$line = -1 === $error['line'] ? null : $error['line'];

if ($githubReporter) {
$githubReporter->error($error['message'], $info['file'], $line, null !== $line ? $error['column'] : null);
}

return null === $line ? $error['message'] : sprintf('Line %d, Column %d: %s', $line, $error['column'], $error['message']);
}, $info['messages']));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ public function testGetHelp()
$this->assertStringContainsString($expected, $command->getHelp());
}

public function testLintIncorrectFileWithGithubFormat()
{
$filename = $this->createFile('note <target>');
$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=6,col=47::Opening and ending tag mismatch: target line 6 and source%A', trim($tester->getDisplay()));
}

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

try {
putenv('GITHUB_ACTIONS=1');

$filename = $this->createFile('note <target>');
$tester = $this->createCommandTester();

$tester->execute(['filename' => [$filename]], ['decorated' => false]);
self::assertStringMatchesFormat('%A::error file=%s,line=6,col=47::Opening and ending tag mismatch: target line 6 and source%A', trim($tester->getDisplay()));
} finally {
putenv('GITHUB_ACTIONS'.($prev ? "=$prev" : ''));
}
}

private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf'): string
{
$xliffContent = <<<XLIFF
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"symfony/dependency-injection": "<5.0",
"symfony/http-kernel": "<5.0",
"symfony/twig-bundle": "<5.0",
"symfony/yaml": "<4.4"
"symfony/yaml": "<4.4",
"symfony/console": "<5.3"
},
"provide": {
"symfony/translation-implementation": "2.3"
Expand Down
0