GithubActionReporter.php
77 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\Console\CI; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Output\OutputInterface; |
| 14 | /** |
| 15 | * Utility class for Github actions. |
| 16 | * |
| 17 | * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 18 | */ |
| 19 | class GithubActionReporter |
| 20 | { |
| 21 | private $output; |
| 22 | /** |
| 23 | * @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L80-L85 |
| 24 | */ |
| 25 | private const ESCAPED_DATA = ['%' => '%25', "\r" => '%0D', "\n" => '%0A']; |
| 26 | /** |
| 27 | * @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L87-L94 |
| 28 | */ |
| 29 | private const ESCAPED_PROPERTIES = ['%' => '%25', "\r" => '%0D', "\n" => '%0A', ':' => '%3A', ',' => '%2C']; |
| 30 | public function __construct(OutputInterface $output) |
| 31 | { |
| 32 | $this->output = $output; |
| 33 | } |
| 34 | public static function isGithubActionEnvironment() : bool |
| 35 | { |
| 36 | return \false !== getenv('GITHUB_ACTIONS'); |
| 37 | } |
| 38 | /** |
| 39 | * Output an error using the Github annotations format. |
| 40 | * |
| 41 | * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message |
| 42 | */ |
| 43 | public function error(string $message, ?string $file = null, ?int $line = null, ?int $col = null) : void |
| 44 | { |
| 45 | $this->log('error', $message, $file, $line, $col); |
| 46 | } |
| 47 | /** |
| 48 | * Output a warning using the Github annotations format. |
| 49 | * |
| 50 | * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message |
| 51 | */ |
| 52 | public function warning(string $message, ?string $file = null, ?int $line = null, ?int $col = null) : void |
| 53 | { |
| 54 | $this->log('warning', $message, $file, $line, $col); |
| 55 | } |
| 56 | /** |
| 57 | * Output a debug log using the Github annotations format. |
| 58 | * |
| 59 | * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message |
| 60 | */ |
| 61 | public function debug(string $message, ?string $file = null, ?int $line = null, ?int $col = null) : void |
| 62 | { |
| 63 | $this->log('debug', $message, $file, $line, $col); |
| 64 | } |
| 65 | private function log(string $type, string $message, ?string $file = null, ?int $line = null, ?int $col = null) : void |
| 66 | { |
| 67 | // Some values must be encoded. |
| 68 | $message = strtr($message, self::ESCAPED_DATA); |
| 69 | if (!$file) { |
| 70 | // No file provided, output the message solely: |
| 71 | $this->output->writeln(sprintf('::%s::%s', $type, $message)); |
| 72 | return; |
| 73 | } |
| 74 | $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)); |
| 75 | } |
| 76 | } |
| 77 |