|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +param ( |
| 4 | + [switch] $SkipConfirm = $false, |
| 5 | + [string] $KeyId |
| 6 | +) |
| 7 | + |
| 8 | +$CurrentPath = Get-Location |
| 9 | +$SourceRootPath = Resolve-Path (Join-Path -Path $PSScriptRoot -ChildPath "..") |
| 10 | +$TestPath = Resolve-Path (Join-Path -Path $PSScriptRoot -ChildPath "..\..\fork-version.tests\gpg") |
| 11 | +$TempKeyContentPath = (Join-Path -Path $PSScriptRoot -ChildPath "TEST_GPG_KEY_CONTENT.tmp") |
| 12 | + |
| 13 | +$KeyContent = @" |
| 14 | +Key-Type: DSA |
| 15 | +Key-Length: 1024 |
| 16 | +Subkey-Type: ECDSA |
| 17 | +Subkey-Curve: nistp256 |
| 18 | +Expire-Date: 0 |
| 19 | +Name-Real: Fork Version |
| 20 | + |
| 21 | +Passphrase: FORK_VERSION_PASSPHRASE |
| 22 | +"@.Trim() |
| 23 | + |
| 24 | +# Functions |
| 25 | +# |
| 26 | + |
| 27 | +function ConfirmAction { |
| 28 | + param ( |
| 29 | + [string] $Title |
| 30 | + ) |
| 31 | + |
| 32 | + if ($SkipConfirm) { |
| 33 | + return $true |
| 34 | + } |
| 35 | + |
| 36 | + $Choices = '&Yes', '&No' |
| 37 | + $Decision = $Host.UI.PromptForChoice($Title, "", $Choices, 1) |
| 38 | + return $Decision -eq 0 |
| 39 | +} |
| 40 | + |
| 41 | +function CreateTestFolder { |
| 42 | + # Create test directory if it doesn't exist |
| 43 | + if (!(Test-Path $TestPath)) { |
| 44 | + New-Item -Path $TestPath -ItemType Directory -Force | Out-Null |
| 45 | + } |
| 46 | + |
| 47 | + if ((Get-ChildItem -Path $TestPath -Force).Count -ne 0) { |
| 48 | + # Confirm if the test directory should be cleared |
| 49 | + if (ConfirmAction -Title "Clear test directory?") { |
| 50 | + Remove-Item -Path $TestPath\* -Recurse -Force | Out-Null |
| 51 | + } |
| 52 | + else { |
| 53 | + Write-Host "Aborting test" |
| 54 | + exit 1 |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function GenerateGPGKey { |
| 60 | + Write-Host "Generating GPG key" |
| 61 | + |
| 62 | + # Create temporary file with key content |
| 63 | + New-Item -Path $TempKeyContentPath -ItemType File -Value $KeyContent -Force | Out-Null |
| 64 | + |
| 65 | + gpg --batch --generate-key $TempKeyContentPath |
| 66 | + |
| 67 | + if ($LastExitCode -ne 0) { |
| 68 | + Write-Error "Failed to generate GPG key" |
| 69 | + exit 1 |
| 70 | + } |
| 71 | + else { |
| 72 | + Remove-Item -Path $TempKeyContentPath -Force | Out-Null |
| 73 | + } |
| 74 | + |
| 75 | + # Search for the fingerprint |
| 76 | + $SearchPattern = "fpr:+(.*?):" |
| 77 | + |
| 78 | + $Fingerprints = gpg --list-secret-keys --with-colons | |
| 79 | + Select-String -Pattern "fpr" | |
| 80 | + ForEach-Object { |
| 81 | + [regex]::Match($_.Line, $SearchPattern).Groups[1].Value |
| 82 | + } |
| 83 | + |
| 84 | + return $Fingerprints[0] |
| 85 | +} |
| 86 | + |
| 87 | +# Main |
| 88 | +# |
| 89 | + |
| 90 | +function Main() { |
| 91 | + CreateTestFolder |
| 92 | + |
| 93 | + Set-Location -Path $TestPath |
| 94 | + |
| 95 | + $GPGKey = $KeyId |
| 96 | + if (([string]::IsNullOrEmpty($GPGKey)) -and (ConfirmAction -Title "Generate a new GPG key?")) { |
| 97 | + $GPGKey = GenerateGPGKey |
| 98 | + } |
| 99 | + |
| 100 | + if ([string]::IsNullOrEmpty($GPGKey)) { |
| 101 | + Write-Error "GPG key is not set" |
| 102 | + exit 1 |
| 103 | + } |
| 104 | + Write-Host "GPG Key: $GPGKey" |
| 105 | + |
| 106 | + # Initialize a new git repository with GPG signing + create initial test file and commit |
| 107 | + git init --initial-branch=main |
| 108 | + git config user.signingKey $GPGKey |
| 109 | + git config commit.gpgSign true |
| 110 | + git config tag.gpgSign true |
| 111 | + |
| 112 | + New-Item -Path "package.json" -ItemType File -Value @" |
| 113 | +{ |
| 114 | + "version": "1.2.3" |
| 115 | +} |
| 116 | +"@.Trim() | Out-Null |
| 117 | + |
| 118 | + git add . |
| 119 | + git commit -S -m "chore: init commit" |
| 120 | + |
| 121 | + Set-Location -Path $SourceRootPath |
| 122 | + |
| 123 | + # Run fork-version script with GPG signing |
| 124 | + pnpm run fork-version --path $TestPath --sign |
| 125 | + pnpm run fork-version --path $TestPath |
| 126 | + pnpm run fork-version --path $TestPath --sign |
| 127 | + |
| 128 | + Set-Location -Path $TestPath |
| 129 | + |
| 130 | + # Manually verify the commits and tags |
| 131 | + git --no-pager log --show-signature |
| 132 | + |
| 133 | + git tag --list | ForEach-Object { |
| 134 | + Write-Host "Verifying tag: $_" |
| 135 | + git verify-tag $_ |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +Main |
| 140 | + |
| 141 | +Set-Location -Path $CurrentPath |
0 commit comments