|
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [string]$Repository, |
| 4 | + |
| 5 | + [ValidateSet('false_positive', 'used_in_tests', 'wont_fix', 'revoked', 'pattern_deleted')] |
| 6 | + [string]$Resolution = 'used_in_tests', |
| 7 | + |
| 8 | + [string]$ResolutionComment = 'Scanner vendor artifacts were removed from repository; resolving historical false positives in fix-forward mode.', |
| 9 | + |
| 10 | + [switch]$Apply |
| 11 | +) |
| 12 | + |
| 13 | +Set-StrictMode -Version Latest |
| 14 | +$ErrorActionPreference = 'Stop' |
| 15 | + |
| 16 | +function Invoke-GhApiJson { |
| 17 | + param( |
| 18 | + [Parameter(Mandatory = $true)] |
| 19 | + [string]$Endpoint, |
| 20 | + |
| 21 | + [string]$Method = 'GET', |
| 22 | + |
| 23 | + [hashtable]$Fields |
| 24 | + ) |
| 25 | + |
| 26 | + $args = @('api', '-X', $Method, '-H', 'Accept: application/vnd.github+json', $Endpoint) |
| 27 | + if ($Fields) { |
| 28 | + foreach ($entry in $Fields.GetEnumerator()) { |
| 29 | + $args += '-f' |
| 30 | + $args += ('{0}={1}' -f $entry.Key, $entry.Value) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + $raw = (& gh @args 2>&1 | Out-String).Trim() |
| 35 | + if ($LASTEXITCODE -ne 0) { |
| 36 | + throw "gh api call failed for '$Endpoint'. Output: $raw" |
| 37 | + } |
| 38 | + |
| 39 | + if ([string]::IsNullOrWhiteSpace($raw)) { |
| 40 | + return $null |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + return $raw | ConvertFrom-Json |
| 45 | + } |
| 46 | + catch { |
| 47 | + throw "Unable to parse gh api JSON response for '$Endpoint'. Raw output: $raw" |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +if (-not (Get-Command gh -ErrorAction SilentlyContinue)) { |
| 52 | + throw 'GitHub CLI (gh) is required. Install gh and authenticate with a token that has repo + security_events scope.' |
| 53 | +} |
| 54 | + |
| 55 | +$repoPattern = '^[^/]+/[^/]+$' |
| 56 | +if ($Repository -notmatch $repoPattern) { |
| 57 | + throw "Repository must be in 'owner/name' format. Received: $Repository" |
| 58 | +} |
| 59 | + |
| 60 | +Write-Host "Repository: $Repository" |
| 61 | +Write-Host "Resolution: $Resolution" |
| 62 | +Write-Host "Mode: $([bool]$Apply ? 'APPLY' : 'DRY-RUN')" |
| 63 | + |
| 64 | +$openAlerts = @() |
| 65 | +$page = 1 |
| 66 | +$perPage = 100 |
| 67 | + |
| 68 | +while ($true) { |
| 69 | + $endpoint = "/repos/$Repository/secret-scanning/alerts?state=open&per_page=$perPage&page=$page" |
| 70 | + $batch = Invoke-GhApiJson -Endpoint $endpoint |
| 71 | + |
| 72 | + if (-not $batch -or $batch.Count -eq 0) { |
| 73 | + break |
| 74 | + } |
| 75 | + |
| 76 | + $openAlerts += $batch |
| 77 | + if ($batch.Count -lt $perPage) { |
| 78 | + break |
| 79 | + } |
| 80 | + |
| 81 | + $page += 1 |
| 82 | +} |
| 83 | + |
| 84 | +if ($openAlerts.Count -eq 0) { |
| 85 | + Write-Host 'No open secret scanning alerts found.' |
| 86 | + exit 0 |
| 87 | +} |
| 88 | + |
| 89 | +Write-Host "Open alerts found: $($openAlerts.Count)" |
| 90 | + |
| 91 | +$summary = $openAlerts | |
| 92 | + Select-Object number, secret_type, state, created_at, html_url | |
| 93 | + Sort-Object number |
| 94 | + |
| 95 | +$summary | Format-Table -AutoSize |
| 96 | + |
| 97 | +if (-not $Apply) { |
| 98 | + Write-Host '' |
| 99 | + Write-Host 'Dry-run only. Re-run with -Apply to resolve all open alerts listed above.' |
| 100 | + exit 0 |
| 101 | +} |
| 102 | + |
| 103 | +$resolved = 0 |
| 104 | +foreach ($alert in $openAlerts) { |
| 105 | + $alertNumber = $alert.number |
| 106 | + $patchEndpoint = "/repos/$Repository/secret-scanning/alerts/$alertNumber" |
| 107 | + |
| 108 | + Invoke-GhApiJson -Endpoint $patchEndpoint -Method 'PATCH' -Fields @{ |
| 109 | + state = 'resolved' |
| 110 | + resolution = $Resolution |
| 111 | + resolution_comment = $ResolutionComment |
| 112 | + } | Out-Null |
| 113 | + |
| 114 | + $resolved += 1 |
| 115 | + Write-Host "Resolved alert #$alertNumber" |
| 116 | +} |
| 117 | + |
| 118 | +Write-Host '' |
| 119 | +Write-Host "Completed. Resolved alerts: $resolved" |
0 commit comments