| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Console\CI;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Utility class for Github actions.
|
|
|
18 |
*
|
|
|
19 |
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
|
|
|
20 |
*/
|
|
|
21 |
class GithubActionReporter
|
|
|
22 |
{
|
|
|
23 |
private OutputInterface $output;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L80-L85
|
|
|
27 |
*/
|
|
|
28 |
private const ESCAPED_DATA = [
|
|
|
29 |
'%' => '%25',
|
|
|
30 |
"\r" => '%0D',
|
|
|
31 |
"\n" => '%0A',
|
|
|
32 |
];
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L87-L94
|
|
|
36 |
*/
|
|
|
37 |
private const ESCAPED_PROPERTIES = [
|
|
|
38 |
'%' => '%25',
|
|
|
39 |
"\r" => '%0D',
|
|
|
40 |
"\n" => '%0A',
|
|
|
41 |
':' => '%3A',
|
|
|
42 |
',' => '%2C',
|
|
|
43 |
];
|
|
|
44 |
|
|
|
45 |
public function __construct(OutputInterface $output)
|
|
|
46 |
{
|
|
|
47 |
$this->output = $output;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public static function isGithubActionEnvironment(): bool
|
|
|
51 |
{
|
|
|
52 |
return false !== getenv('GITHUB_ACTIONS');
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Output an error using the Github annotations format.
|
|
|
57 |
*
|
|
|
58 |
* @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
|
|
|
59 |
*/
|
|
|
60 |
public function error(string $message, string $file = null, int $line = null, int $col = null): void
|
|
|
61 |
{
|
|
|
62 |
$this->log('error', $message, $file, $line, $col);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Output a warning using the Github annotations format.
|
|
|
67 |
*
|
|
|
68 |
* @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message
|
|
|
69 |
*/
|
|
|
70 |
public function warning(string $message, string $file = null, int $line = null, int $col = null): void
|
|
|
71 |
{
|
|
|
72 |
$this->log('warning', $message, $file, $line, $col);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Output a debug log using the Github annotations format.
|
|
|
77 |
*
|
|
|
78 |
* @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message
|
|
|
79 |
*/
|
|
|
80 |
public function debug(string $message, string $file = null, int $line = null, int $col = null): void
|
|
|
81 |
{
|
|
|
82 |
$this->log('debug', $message, $file, $line, $col);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function log(string $type, string $message, string $file = null, int $line = null, int $col = null): void
|
|
|
86 |
{
|
|
|
87 |
// Some values must be encoded.
|
|
|
88 |
$message = strtr($message, self::ESCAPED_DATA);
|
|
|
89 |
|
|
|
90 |
if (!$file) {
|
|
|
91 |
// No file provided, output the message solely:
|
|
|
92 |
$this->output->writeln(sprintf('::%s::%s', $type, $message));
|
|
|
93 |
|
|
|
94 |
return;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$this->output->writeln(sprintf('::%s file=%s,line=%s,col=%s::%s', $type, strtr($file, self::ESCAPED_PROPERTIES), strtr($line ?? 1, self::ESCAPED_PROPERTIES), strtr($col ?? 0, self::ESCAPED_PROPERTIES), $message));
|
|
|
98 |
}
|
|
|
99 |
}
|