Skip to content

Commit

Permalink
Merge pull request #220 from ghostwriter/feature/debug-github-actions
Browse files Browse the repository at this point in the history
Enable debug mode for GitHub Actions
  • Loading branch information
Ocramius authored Dec 16, 2022
2 parents e6400c5 + 37e8f0e commit 49d9580
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Environment/EnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Laminas\AutomaticReleases\Gpg\ImportGpgKeyFromString;
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
use Psl;
use Psl\Env;
use Psl\Iter;
use Psl\Str;

use function Psl\Env\get_var;
use function Psl\invariant;
use function Psl\Iter\contains;
use function Psl\Str\format;

/** @psalm-immutable */
class EnvironmentVariables implements Variables
Expand Down Expand Up @@ -51,8 +52,8 @@ private function __construct(
private readonly string $logLevel,
) {
/** @psalm-suppress ImpureFunctionCall the {@see \Psl\Iter\contains()} API is conditionally pure */
Psl\invariant(
Iter\contains(self::LOG_LEVELS, $logLevel),
invariant(
contains(self::LOG_LEVELS, $logLevel),
'LOG_LEVEL env MUST be a valid monolog/monolog log level constant name or value;'
. ' see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels',
);
Expand All @@ -62,25 +63,33 @@ public static function fromEnvironment(ImportGpgKeyFromString $importKey): self
{
return new self(
self::getenv('GITHUB_TOKEN'),
$importKey->__invoke(self::getenv('SIGNING_SECRET_KEY')),
($importKey)(self::getenv('SIGNING_SECRET_KEY')),
self::getenv('GIT_AUTHOR_NAME'),
self::getenv('GIT_AUTHOR_EMAIL'),
self::getenv('GITHUB_EVENT_PATH'),
self::getenv('GITHUB_WORKSPACE'),
self::getenvWithFallback('LOG_LEVEL', 'INFO'),
self::getenvWithFallback(
'LOG_LEVEL',
self::isDebugMode() ? 'DEBUG' : 'INFO',
),
);
}

private static function isDebugMode(): bool
{
return get_var('ACTIONS_RUNNER_DEBUG') !== null;
}

/**
* @psalm-param non-empty-string $key
*
* @psalm-return non-empty-string
*/
private static function getenv(string $key): string
{
$value = Env\get_var($key);
$value = get_var($key);

Psl\invariant($value !== null && $value !== '', Str\format('Could not find a value for environment variable "%s"', $key));
invariant($value !== null && $value !== '', format('Could not find a value for environment variable "%s"', $key));

return $value;
}
Expand All @@ -93,7 +102,7 @@ private static function getenv(string $key): string
*/
private static function getenvWithFallback(string $key, string $default): string
{
$value = Env\get_var($key);
$value = get_var($key);

return $value === null || $value === '' ? $default : $value;
}
Expand All @@ -120,7 +129,6 @@ public function gitAuthorEmail(): string

public function githubEventPath(): string
{
// @TODO test me
return $this->githubEventPath;
}

Expand Down
36 changes: 36 additions & 0 deletions test/unit/Environment/EnvironmentVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,40 @@ public function testFailsOnMissingEnvironmentVariables(): void

EnvironmentVariables::fromEnvironment($importKey);
}

public function testDebugModeOffEnvironmentVariables(): void
{
// commented to signify a missing env variable.
// Env\set_var('ACTIONS_RUNNER_DEBUG', '');
Env\set_var('GITHUB_TOKEN', 'token');
Env\set_var('SIGNING_SECRET_KEY', 'aaa');
Env\set_var('GITHUB_ORGANISATION', 'bbb');
Env\set_var('GIT_AUTHOR_NAME', 'ccc');
Env\set_var('GIT_AUTHOR_EMAIL', 'ddd@eee.ff');
Env\set_var('GITHUB_EVENT_PATH', '/tmp/event');
Env\set_var('GITHUB_WORKSPACE', '/tmp');

$importKey = $this->createMock(ImportGpgKeyFromString::class);
$importKey->method('__invoke')->willReturn(SecretKeyId::fromBase16String('aabbccdd'));
$variables = EnvironmentVariables::fromEnvironment($importKey);
self::assertEquals('INFO', $variables->logLevel());
}

public function testDebugModeOnEnvironmentVariables(): void
{
Env\set_var('ACTIONS_RUNNER_DEBUG', 'TRUE');
Env\set_var('GITHUB_TOKEN', 'token');
Env\set_var('SIGNING_SECRET_KEY', 'aaa');
Env\set_var('GITHUB_ORGANISATION', 'bbb');
Env\set_var('GIT_AUTHOR_NAME', 'ccc');
Env\set_var('GIT_AUTHOR_EMAIL', 'ddd@eee.ff');
Env\set_var('GITHUB_EVENT_PATH', '/tmp/event');
Env\set_var('GITHUB_WORKSPACE', '/tmp');

$importKey = $this->createMock(ImportGpgKeyFromString::class);
$importKey->method('__invoke')->willReturn(SecretKeyId::fromBase16String('aabbccdd'));
$variables = EnvironmentVariables::fromEnvironment($importKey);

self::assertEquals('DEBUG', $variables->logLevel());
}
}

0 comments on commit 49d9580

Please sign in to comment.