Skip to content

Commit a09aaee

Browse files
authored
Merge pull request #326 from asgrim/pre-check-gh-attestation-command
Check gh attestation command exists before using it
2 parents 9365c6e + 1e1799a commit a09aaee

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/SelfManage/Verify/GithubCliAttestationVerification.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
use function implode;
1616
use function sprintf;
17+
use function str_starts_with;
1718

1819
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
1920
final class GithubCliAttestationVerification implements VerifyPiePhar
2021
{
2122
private const GH_CLI_NAME = 'gh';
23+
private const GH_ATTESTATION_COMMAND = 'attestation';
2224
private const GH_VERIFICATION_TIMEOUT = 30;
2325

2426
public function __construct(private readonly ExecutableFinder $executableFinder)
@@ -33,9 +35,20 @@ public function verify(ReleaseMetadata $releaseMetadata, BinaryFile $pharFilenam
3335
throw GithubCliNotAvailable::fromExpectedGhToolName(self::GH_CLI_NAME);
3436
}
3537

38+
// Try to use `gh attestation --help` to ensure it is not an old `gh` cli version
39+
try {
40+
Process::run([$gh, self::GH_ATTESTATION_COMMAND, '--help'], null, self::GH_VERIFICATION_TIMEOUT);
41+
} catch (ProcessFailedException $attestationCommandCheck) {
42+
if (str_starts_with($attestationCommandCheck->getProcess()->getErrorOutput(), sprintf('unknown command "%s" for "%s"', self::GH_ATTESTATION_COMMAND, self::GH_CLI_NAME))) {
43+
throw GithubCliNotAvailable::withMissingAttestationCommand(self::GH_CLI_NAME);
44+
}
45+
46+
throw $attestationCommandCheck;
47+
}
48+
3649
$verificationCommand = [
3750
$gh,
38-
'attestation',
51+
self::GH_ATTESTATION_COMMAND,
3952
'verify',
4053
'--owner=php',
4154
$pharFilename->filePath,

src/SelfManage/Verify/GithubCliNotAvailable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public static function fromExpectedGhToolName(string $expectedGhToolName): self
1414
{
1515
return new self(sprintf('The GitHub "%s" CLI tool was not available.', $expectedGhToolName));
1616
}
17+
18+
public static function withMissingAttestationCommand(string $expectedGhToolName): self
19+
{
20+
return new self(sprintf('The GitHub "%s" CLI tool was available, but the `gh attestation` command failed; perhaps this version is out of date.', $expectedGhToolName));
21+
}
1722
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
@echo off
2+
if "%*" == "" goto main
3+
echo %* | findstr /C:"--help" >nul
4+
if %errorlevel% == 0 exit /b 0
5+
6+
:main
17
echo "Pretending to be gh cli - unhappy path"
28
exit /b 1

test/assets/fake-gh-cli/unhappy.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/usr/bin/env bash
22

3+
if [[ "$*" == *"--help"* ]]; then
4+
exit 0
5+
fi
6+
37
echo "Pretending to be gh cli - unhappy path"
48
exit 1

0 commit comments

Comments
 (0)