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

Commit 433ff8c

Browse filesBrowse files
committed
[Translation] XliffLintCommand supports Github Actions annotations
1 parent e574a24 commit 433ff8c
Copy full SHA for 433ff8c

File tree

4 files changed

+54
-6
lines changed
Filter options

4 files changed

+54
-6
lines changed

‎src/Symfony/Component/Translation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Command/XliffLintCommand.php
+18-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php
+27Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/composer.json
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require-dev": {
2626
"symfony/config": "^4.4|^5.0|^6.0",
27-
"symfony/console": "^4.4|^5.0|^6.0",
27+
"symfony/console": "^5.3|^6.0",
2828
"symfony/dependency-injection": "^5.0|^6.0",
2929
"symfony/http-kernel": "^5.0|^6.0",
3030
"symfony/intl": "^4.4|^5.0|^6.0",
@@ -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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.