GithubActionReporter.php 3.1 KB

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