GithubActionReporter.php 3.0 KB

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