Skip to content

Commit 37e8f0e

Browse files
committed
Enable debug mode for GitHub Actions
Signed-off-by: Nathanael Esayeas <[email protected]>
1 parent e6400c5 commit 37e8f0e

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

src/Environment/EnvironmentVariables.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
use Laminas\AutomaticReleases\Gpg\ImportGpgKeyFromString;
88
use Laminas\AutomaticReleases\Gpg\SecretKeyId;
9-
use Psl;
10-
use Psl\Env;
11-
use Psl\Iter;
12-
use Psl\Str;
9+
10+
use function Psl\Env\get_var;
11+
use function Psl\invariant;
12+
use function Psl\Iter\contains;
13+
use function Psl\Str\format;
1314

1415
/** @psalm-immutable */
1516
class EnvironmentVariables implements Variables
@@ -51,8 +52,8 @@ private function __construct(
5152
private readonly string $logLevel,
5253
) {
5354
/** @psalm-suppress ImpureFunctionCall the {@see \Psl\Iter\contains()} API is conditionally pure */
54-
Psl\invariant(
55-
Iter\contains(self::LOG_LEVELS, $logLevel),
55+
invariant(
56+
contains(self::LOG_LEVELS, $logLevel),
5657
'LOG_LEVEL env MUST be a valid monolog/monolog log level constant name or value;'
5758
. ' see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels',
5859
);
@@ -62,25 +63,33 @@ public static function fromEnvironment(ImportGpgKeyFromString $importKey): self
6263
{
6364
return new self(
6465
self::getenv('GITHUB_TOKEN'),
65-
$importKey->__invoke(self::getenv('SIGNING_SECRET_KEY')),
66+
($importKey)(self::getenv('SIGNING_SECRET_KEY')),
6667
self::getenv('GIT_AUTHOR_NAME'),
6768
self::getenv('GIT_AUTHOR_EMAIL'),
6869
self::getenv('GITHUB_EVENT_PATH'),
6970
self::getenv('GITHUB_WORKSPACE'),
70-
self::getenvWithFallback('LOG_LEVEL', 'INFO'),
71+
self::getenvWithFallback(
72+
'LOG_LEVEL',
73+
self::isDebugMode() ? 'DEBUG' : 'INFO',
74+
),
7175
);
7276
}
7377

78+
private static function isDebugMode(): bool
79+
{
80+
return get_var('ACTIONS_RUNNER_DEBUG') !== null;
81+
}
82+
7483
/**
7584
* @psalm-param non-empty-string $key
7685
*
7786
* @psalm-return non-empty-string
7887
*/
7988
private static function getenv(string $key): string
8089
{
81-
$value = Env\get_var($key);
90+
$value = get_var($key);
8291

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

8594
return $value;
8695
}
@@ -93,7 +102,7 @@ private static function getenv(string $key): string
93102
*/
94103
private static function getenvWithFallback(string $key, string $default): string
95104
{
96-
$value = Env\get_var($key);
105+
$value = get_var($key);
97106

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

121130
public function githubEventPath(): string
122131
{
123-
// @TODO test me
124132
return $this->githubEventPath;
125133
}
126134

test/unit/Environment/EnvironmentVariablesTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,40 @@ public function testFailsOnMissingEnvironmentVariables(): void
112112

113113
EnvironmentVariables::fromEnvironment($importKey);
114114
}
115+
116+
public function testDebugModeOffEnvironmentVariables(): void
117+
{
118+
// commented to signify a missing env variable.
119+
// Env\set_var('ACTIONS_RUNNER_DEBUG', '');
120+
Env\set_var('GITHUB_TOKEN', 'token');
121+
Env\set_var('SIGNING_SECRET_KEY', 'aaa');
122+
Env\set_var('GITHUB_ORGANISATION', 'bbb');
123+
Env\set_var('GIT_AUTHOR_NAME', 'ccc');
124+
Env\set_var('GIT_AUTHOR_EMAIL', '[email protected]');
125+
Env\set_var('GITHUB_EVENT_PATH', '/tmp/event');
126+
Env\set_var('GITHUB_WORKSPACE', '/tmp');
127+
128+
$importKey = $this->createMock(ImportGpgKeyFromString::class);
129+
$importKey->method('__invoke')->willReturn(SecretKeyId::fromBase16String('aabbccdd'));
130+
$variables = EnvironmentVariables::fromEnvironment($importKey);
131+
self::assertEquals('INFO', $variables->logLevel());
132+
}
133+
134+
public function testDebugModeOnEnvironmentVariables(): void
135+
{
136+
Env\set_var('ACTIONS_RUNNER_DEBUG', 'TRUE');
137+
Env\set_var('GITHUB_TOKEN', 'token');
138+
Env\set_var('SIGNING_SECRET_KEY', 'aaa');
139+
Env\set_var('GITHUB_ORGANISATION', 'bbb');
140+
Env\set_var('GIT_AUTHOR_NAME', 'ccc');
141+
Env\set_var('GIT_AUTHOR_EMAIL', '[email protected]');
142+
Env\set_var('GITHUB_EVENT_PATH', '/tmp/event');
143+
Env\set_var('GITHUB_WORKSPACE', '/tmp');
144+
145+
$importKey = $this->createMock(ImportGpgKeyFromString::class);
146+
$importKey->method('__invoke')->willReturn(SecretKeyId::fromBase16String('aabbccdd'));
147+
$variables = EnvironmentVariables::fromEnvironment($importKey);
148+
149+
self::assertEquals('DEBUG', $variables->logLevel());
150+
}
115151
}

0 commit comments

Comments
 (0)