Debian Codegraph @

vendor/symfony/console/CI/GithubActionReporter.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
20 */
21 class GithubActionReporter
22 {
23 /**
24 * @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L80-L85
25 */
26 private const ESCAPED_DATA = [
27 '%' => '%25',
28 "\r" => '%0D',
29 "\n" => '%0A',
30 ];
31
32 /**
33 * @see https://github.com/actions/toolkit/blob/5e5e1b7aacba68a53836a34db4a288c3c1c1585b/packages/core/src/command.ts#L87-L94
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 * Output an error using the Github annotations format.
55 *
56 * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
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 * Output a warning using the Github annotations format.
65 *
66 * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message
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 * Output a debug log using the Github annotations format.
75 *
76 * @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message
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 // Some values must be encoded.
86 $message = strtr($message, self::ESCAPED_DATA);
87
88 if (!$file) {
89 // No file provided, output the message solely:
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 }