| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Console\CI; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Output\OutputInterface; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class GithubActionReporter |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private const ESCAPED_DATA = [ |
| 27 |
'%' => '%25', |
| 28 |
"\r" => '%0D', |
| 29 |
"\n" => '%0A', |
| 30 |
]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private const ESCAPED_PROPERTIES = [ |
| 36 |
'%' => '%25', |
| 37 |
"\r" => '%0D', |
| 38 |
"\n" => '%0A', |
| 39 |
':' => '%3A', |
| 40 |
',' => '%2C', |
| 41 |
]; |
| 42 |
|
| 43 |
public function __construct( |
| 44 |
private OutputInterface $output, |
| 45 |
) { |
| 46 |
} |
| 47 |
|
| 48 |
public static function isGithubActionEnvironment(): bool |
| 49 |
{ |
| 50 |
return false !== getenv('GITHUB_ACTIONS'); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function error(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void |
| 59 |
{ |
| 60 |
$this->log('error', $message, $file, $line, $col); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function warning(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void |
| 69 |
{ |
| 70 |
$this->log('warning', $message, $file, $line, $col); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function debug(string $message, ?string $file = null, ?int $line = null, ?int $col = null): void |
| 79 |
{ |
| 80 |
$this->log('debug', $message, $file, $line, $col); |
| 81 |
} |
| 82 |
|
| 83 |
private function log(string $type, string $message, ?string $file = null, ?int $line = null, ?int $col = null): void |
| 84 |
{ |
| 85 |
|
| 86 |
$message = strtr($message, self::ESCAPED_DATA); |
| 87 |
|
| 88 |
if (!$file) { |
| 89 |
|
| 90 |
$this->output->writeln(\sprintf('::%s::%s', $type, $message)); |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$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)); |
| 96 |
} |
| 97 |
} |