8000 [Translation] XliffLintCommand supports Github Actions annotations · symfony/symfony@8ac2b2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ac2b2a

Browse files
YaFoufabpot
authored andcommitted
[Translation] XliffLintCommand supports Github Actions annotations
1 parent b8d7e1f commit 8ac2b2a

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `github` format & autodetection to render errors as annotations when
8+
running the XLIFF linter command in a Github Actions environment.
9+
410
5.3
511
---
612

src/Symfony/Component/Translation/Command/XliffLintCommand.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Translation\Command;
1313

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

90+
if (null === $this->format) {
91+
$this->format = GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
92+
}
93+
8994
if (['-'] === $filenames) {
9095
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
9196
}
@@ -160,25 +165,34 @@ private function display(SymfonyStyle $io, array $files)
160165
return $this->displayTxt($io, $files);
161166
case 'json':
162167
return $this->displayJson($io, $files);
168+
case 'github':
169+
return $this->displayTxt($io, $files, true);
163170
default:
164171
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
165172
}
166173
}
167174

168-
private function displayTxt(SymfonyStyle $io, array $filesInfo)
175+
private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)
169176
{
170177
$countFiles = \count($filesInfo);
171178
$erroredFiles = 0;
179+
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($io) : null;
172180

173181
foreach ($filesInfo as $info) {
174182
if ($info['valid'] && $this->displayCorrectFiles) {
175183
$io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
176184
} elseif (!$info['valid']) {
177185
++$erroredFiles;
178186
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
179-
$io->listing(array_map(function ($error) {
187+
$io->listing(array_map(function ($error) use ($info, $githubReporter) {
180188
// general document errors have a '-1' line number
181-
return -1 === $error['line'] ? $error['message'] : sprintf('Line %d, Column %d: %s', $error['line'], $error['column'], $error['message']);
189+
$line = -1 === $error['line'] ? null : $error['line'];
190+
191+
if ($githubReporter) {
192+
$githubReporter->error($error['message'], $info['file'], $line, null !== $line ? $error['column'] : null);
193+
}
194+
195+
return null === $line ? $error['message'] : sprintf('Line %d, Column %d: %s', $line, $error['column'], $error['message']);
182196
}, $info['messages']));
183197
}
184198
}

src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ public function testGetHelp()
140140
$this->assertStringContainsString($expected, $command->getHelp());
141141
}
142142

143+
public function testLintIncorrectFileWithGithubFormat()
144+
{
145+
$filename = $this->createFile('note <target>');
146+
$tester = $this->createCommandTester();
147+
$tester->execute(['filename' => [$filename], '--format' => 'github'], ['decorated' => false]);
148+
self::assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
149+
self::assertStringMatchesFormat('%A::error file=%s,line=6,col=47::Opening and ending tag mismatch: target line 6 and source%A', trim($tester->getDisplay()));
150+
}
151+
152+
public function testLintAutodetectsGithubActionEnvironment()
153+
{
154+
$prev = getenv('GITHUB_ACTIONS');
155+
putenv('GITHUB_ACTIONS');
156+
157+
try {
158+
putenv('GITHUB_ACTIONS=1');
159+
160+
$filename = $this->createFile('note <target>');
161+
$tester = $this->createCommandTester();
162+
163+
$tester->execute(['filename' => [$filename]], ['decorated' => false]);
164+
self::assertStringMatchesFormat('%A::error file=%s,line=6,col=47::Opening and ending tag mismatch: target line 6 and source%A', trim($tester->getDisplay()));
165+
} finally {
166+
putenv('GITHUB_ACTIONS'.($prev ? "=$prev" : ''));
167+
}
168+
}
169+
143170
private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf'): string
144171
{
145172
$xliffContent = <<<XLIFF

src/Symfony/Component/Translation/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"symfony/dependency-injection": "<5.0",
4040
"symfony/http-kernel": "<5.0",
4141
"symfony/twig-bundle": "<5.0",
42-
"symfony/yaml": "<4.4"
42+
"symfony/yaml": "<4.4",
43+
"symfony/console": "<5.3"
4344
},
4445
"provide": {
4546
"symfony/translation-implementation": "2.3"

0 commit comments

Comments
 (0)
0