-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-audit-gh.ps1
More file actions
451 lines (358 loc) · 13.5 KB
/
Copy pathrun-audit-gh.ps1
File metadata and controls
451 lines (358 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<#
.SYNOPSIS
Run audit-lead.js via GitHub Actions with encrypted artifact download
.DESCRIPTION
Triggers the audit-lead workflow on GitHub Actions, waits for completion,
downloads the encrypted artifact, decrypts it locally, and extracts results.
IMPORTANT: By default, results are extracted to a review folder and NOT applied
to your local profile.md files. This prevents accidental overwrites.
Safe workflow:
1. Run: .\run-audit-gh.ps1 -LeadId "1234" -Domain "example.com"
2. Review files in: .\audit-review-1234\
3. When satisfied, apply: .\run-audit-gh.ps1 -LeadId "1234" -Domain "example.com" -ApplyChanges
.PARAMETER LeadId
The lead ID to audit
.PARAMETER Domain
The domain to audit (without https://)
.PARAMETER ProfileRange
Optional profile range (e.g., "3600-3699")
.PARAMETER KeepEncrypted
Keep the encrypted artifact file after extraction
.PARAMETER SelfDelete
Whether to delete the workflow run after completion (default: true)
.PARAMETER ReviewOnly
Just extract to review folder, don't prompt to apply (default behavior)
.PARAMETER ApplyChanges
Apply the reviewed changes to local profile.md files
.EXAMPLE
.\run-audit-gh.ps1 -LeadId "3648" -Domain "iqmotorsports.com"
.EXAMPLE
.\run-audit-gh.ps1 -LeadId "3648" -Domain "iqmotorsports.com" -ProfileRange "3600-3699"
.EXAMPLE
.\run-audit-gh.ps1 -LeadId "3648" -Domain "iqmotorsports.com" -ApplyChanges
#>
param(
[Parameter(Mandatory=$true)]
[string]$LeadId,
[Parameter(Mandatory=$true)]
[string]$Domain,
[string]$ProfileRange = "",
[switch]$KeepEncrypted = $false,
[bool]$SelfDelete = $false,
[switch]$ReviewOnly = $false,
[switch]$ApplyChanges = $false,
[string]$Repo = "GalToast/website-audit-engine"
)
$ErrorActionPreference = "Stop"
# Config
$Workflow = "audit-lead.yml"
$KeyFile = "$env:USERPROFILE\.audit-encryption-key"
$OpenSSL = "C:\Program Files\Git\usr\bin\openssl.exe"
# Colors for output
function Write-Step { param($msg) Write-Host "`n==> $msg" -ForegroundColor Cyan }
function Write-Success { param($msg) Write-Host " $msg" -ForegroundColor Green }
function Write-Err { param($msg) Write-Host " ERROR: $msg" -ForegroundColor Red }
function Get-EncryptedArtifact {
param(
[string]$RepoName,
[string]$WorkflowRunId,
[string]$DestinationDir
)
$maxAttempts = 6
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
if (Test-Path $DestinationDir) {
Get-ChildItem -Path $DestinationDir -Recurse -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
}
& gh run download $WorkflowRunId --repo $RepoName -D $DestinationDir 2>&1 | Out-Null
$encrypted = Get-ChildItem -Path $DestinationDir -Recurse -Filter "*.enc" -File -ErrorAction SilentlyContinue | Select-Object -First 1
if ($encrypted) {
return $encrypted
}
if ($attempt -lt $maxAttempts) {
Start-Sleep -Seconds 3
}
}
return $null
}
function Get-RunArtifactMetadata {
param(
[string]$RepoName,
[string]$WorkflowRunId
)
$response = & gh api "repos/$RepoName/actions/runs/$WorkflowRunId/artifacts" 2>$null
if ($LASTEXITCODE -ne 0 -or -not $response) {
return $null
}
$payload = $response | ConvertFrom-Json
foreach ($artifact in @($payload.artifacts)) {
if (-not $artifact.name) { continue }
if ($artifact.name -match '^audit-(\d+)-(.*)-encrypted$') {
return @{
LeadId = $Matches[1]
BatchToken = $Matches[2]
ArtifactName = $artifact.name
}
}
if ($artifact.name -match '^audit-(\d+)-encrypted$') {
return @{
LeadId = $Matches[1]
BatchToken = ""
ArtifactName = $artifact.name
}
}
}
return $null
}
# Validate prerequisites
Write-Step "Validating prerequisites..."
if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
Write-Err "GitHub CLI (gh) not found. Install from: https://cli.github.com/"
exit 1
}
if (-not (Test-Path $KeyFile)) {
Write-Err "Encryption key not found at: $KeyFile"
Write-Host " Run this to generate one: openssl rand -base64 32 > $KeyFile"
exit 1
}
if (-not (Test-Path $OpenSSL)) {
Write-Err "OpenSSL not found at: $OpenSSL"
exit 1
}
# Check GitHub auth
$authStatus = gh auth status 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Err "Not authenticated with GitHub CLI. Run: gh auth login"
exit 1
}
Write-Success "GitHub CLI authenticated"
# Check if secret exists
$secrets = gh secret list --repo $Repo 2>&1
if ($secrets -notmatch "AUDIT_ENCRYPTION_KEY") {
Write-Err "AUDIT_ENCRYPTION_KEY secret not set in repository"
Write-Host " Run: gh secret set AUDIT_ENCRYPTION_KEY --repo $Repo < $KeyFile"
exit 1
}
Write-Success "Encryption key secret configured"
# Build workflow inputs
Write-Step "Triggering workflow for $Domain..."
$triggeredAtUtc = (Get-Date).ToUniversalTime()
$batchToken = "{0}-{1}-{2}" -f $LeadId, ((Get-Date).ToUniversalTime().ToString("yyyyMMddHHmmssfff")), ([guid]::NewGuid().ToString("N").Substring(0, 8))
$workflowArgs = @(
"workflow", "run", $Workflow,
"--repo", $Repo,
"-f", "lead_id=$LeadId",
"-f", "domain=$Domain",
"-f", "batch_token=$batchToken"
)
if ($ProfileRange) {
$workflowArgs += @("-f", "profile_range=$ProfileRange")
}
$triggerOutput = & gh $workflowArgs 2>&1
$triggerExitCode = $LASTEXITCODE
if ($triggerOutput) {
$triggerOutput | ForEach-Object { Write-Host $_ }
}
if ($triggerExitCode -ne 0) {
Write-Err "Failed to trigger workflow"
exit 1
}
Write-Success "Workflow triggered"
# Wait for workflow to start
Write-Step "Waiting for workflow to start..."
Start-Sleep -Seconds 3
$runId = $null
$triggerOutputText = ($triggerOutput | Out-String)
if ($triggerOutputText -match 'actions/runs/(\d+)') {
$runId = $Matches[1]
} elseif ($triggerOutputText -match 'gh run view (\d+)') {
$runId = $Matches[1]
}
if ($runId) {
Write-Success "Found run ID from trigger output: $runId"
}
$maxAttempts = 30
$attempt = 0
while (-not $runId -and $attempt -lt $maxAttempts) {
$attempt++
$runs = gh run list --repo $Repo --workflow $Workflow --limit 20 --json databaseId,status,displayTitle,createdAt 2>$null | ConvertFrom-Json
$candidateRuns = $runs |
Where-Object { ([datetime]$_.createdAt).ToUniversalTime() -ge $triggeredAtUtc.AddSeconds(-5) } |
Sort-Object { ([datetime]$_.createdAt).ToUniversalTime() }
foreach ($candidateRun in @($candidateRuns)) {
$artifactMeta = Get-RunArtifactMetadata -RepoName $Repo -WorkflowRunId $candidateRun.databaseId
if (-not $artifactMeta) { continue }
if ($artifactMeta.LeadId -ne $LeadId) { continue }
if ($artifactMeta.BatchToken -ne $batchToken) { continue }
$runId = $candidateRun.databaseId
break
}
if (-not $runId) {
Write-Host " Waiting... ($attempt/$maxAttempts)"
Start-Sleep -Seconds 2
}
}
if (-not $runId) {
Write-Err "Could not find workflow run for lead $LeadId"
exit 1
}
Write-Success "Found run ID: $runId"
# Watch workflow progress
Write-Step "Watching workflow progress..."
& gh run watch $runId --repo $Repo --exit-status
if ($LASTEXITCODE -ne 0) {
Write-Err "Workflow failed or was cancelled"
# Download logs for debugging
Write-Host " Downloading logs for debugging..."
& gh run view $runId --repo $Repo --log-failed
exit 1
}
Write-Success "Workflow completed successfully"
# Download artifact
Write-Step "Downloading encrypted artifact..."
$downloadDir = ".\audit-temp-$LeadId"
if (Test-Path $downloadDir) {
Remove-Item -Recurse -Force $downloadDir
}
New-Item -ItemType Directory -Path $downloadDir | Out-Null
$encryptedFile = Get-EncryptedArtifact -RepoName $Repo -WorkflowRunId $runId -DestinationDir $downloadDir
if (-not $encryptedFile) {
Write-Err "Failed to download artifact"
exit 1
}
Write-Success "Artifact downloaded"
Write-Success "Found encrypted file: $($encryptedFile.Name)"
# Decrypt
Write-Step "Decrypting results..."
$decryptedFile = Join-Path $downloadDir "audit-results.tar.gz"
& $OpenSSL enc -aes-256-cbc -d -in $encryptedFile.FullName -out $decryptedFile -pass file:"$KeyFile" -pbkdf2
if ($LASTEXITCODE -ne 0) {
Write-Err "Decryption failed. Check your encryption key."
exit 1
}
Write-Success "Decrypted successfully"
# Extract
Write-Step "Extracting results..."
# Always extract to review folder first (never overwrite directly)
$reviewDir = ".\audit-review-$LeadId"
if (Test-Path $reviewDir) {
Remove-Item -Recurse -Force $reviewDir
}
New-Item -ItemType Directory -Path $reviewDir | Out-Null
# Extract tarball to review folder
tar -xzf $decryptedFile -C $reviewDir
if ($LASTEXITCODE -ne 0) {
Write-Err "Extraction failed"
exit 1
}
Write-Success "Extracted to review folder: $reviewDir"
# Show what was extracted
Write-Host ""
Write-Host "=== EXTRACTED FILES ===" -ForegroundColor Yellow
Get-ChildItem -Path $reviewDir -Recurse -File | ForEach-Object {
$relativePath = $_.FullName.Replace("$reviewDir\", "")
Write-Host " $relativePath" -ForegroundColor Gray
}
# Check for profile.md changes
$extractedProfile = Get-ChildItem -Path $reviewDir -Recurse -Filter "profile.md" |
Where-Object { $_.FullName -match [regex]::Escape("\$LeadId-") } |
Select-Object -First 1
$existingProfile = $null
if ($extractedProfile) {
# Find existing profile to compare
$existingProfile = Get-ChildItem -Path "leads\profiles" -Recurse -Filter "profile.md" |
Where-Object { $_.FullName -match "\\$LeadId-" } | Select-Object -First 1
}
if ($ReviewOnly -and -not $ApplyChanges) {
Write-Host ""
Write-Host "=== REVIEW MODE ===" -ForegroundColor Yellow
Write-Host "Results extracted to: $reviewDir"
Write-Host ""
Write-Host "Files are NOT applied to your local profiles."
Write-Host "Review the files above, then re-run with -ApplyChanges to apply."
Write-Host ""
Write-Host "To apply changes:" -ForegroundColor Cyan
Write-Host " .\run-audit-gh.ps1 -LeadId $LeadId -Domain $Domain -ApplyChanges" -ForegroundColor White
Write-Host ""
Write-Host "To view profile diff:" -ForegroundColor Cyan
if ($existingProfile -and $extractedProfile) {
Write-Host " git diff --no-index `"$($existingProfile.FullName)`" `"$($extractedProfile.FullName)`"" -ForegroundColor White
}
Write-Host ""
# Skip to cleanup without self-delete so user can re-run
$SelfDelete = $false
} elseif ($ApplyChanges) {
Write-Host ""
Write-Host "=== APPLYING CHANGES ===" -ForegroundColor Yellow
# Copy results to actual locations
if (Test-Path "$reviewDir\leads\profiles") {
$leadDirs = Get-ChildItem -Path "$reviewDir\leads\profiles" -Directory -Recurse |
Where-Object { $_.Name -match "^$LeadId-" }
foreach ($leadDir in $leadDirs) {
$rangeName = Split-Path $leadDir.Parent.FullName -Leaf
$destRangeDir = Join-Path "leads\profiles" $rangeName
$destLeadDir = Join-Path $destRangeDir $leadDir.Name
if (-not (Test-Path $destRangeDir)) {
New-Item -ItemType Directory -Path $destRangeDir -Force | Out-Null
}
if (-not (Test-Path $destLeadDir)) {
New-Item -ItemType Directory -Path $destLeadDir -Force | Out-Null
}
Copy-Item -Recurse -Force (Join-Path $leadDir.FullName "*") $destLeadDir
Write-Success "Updated $rangeName\$($leadDir.Name)"
}
}
if (Test-Path "$reviewDir\ops\screenshots") {
Get-ChildItem -Path "$reviewDir\ops\screenshots" -File -Filter "$LeadId-*" | ForEach-Object {
Copy-Item -Force $_.FullName "ops\screenshots\"
Write-Success "Copied screenshot $($_.Name)"
}
}
# Copy any JSON files
Get-ChildItem -Path $reviewDir -Filter "audit-$LeadId*.json" | ForEach-Object {
Copy-Item -Force $_.FullName "."
Write-Success "Copied $($_.Name)"
}
# Clean up review folder after applying
Remove-Item -Recurse -Force $reviewDir
Write-Success "Cleaned up review folder"
} else {
# Default behavior: just show review folder location
Write-Host ""
Write-Host "Results extracted to: $reviewDir"
Write-Host ""
Write-Host "To apply these changes to your local profiles:" -ForegroundColor Cyan
Write-Host " .\run-audit-gh.ps1 -LeadId $LeadId -Domain $Domain -ApplyChanges" -ForegroundColor White
}
# Cleanup
Write-Step "Cleaning up..."
if (-not $KeepEncrypted) {
Remove-Item -Recurse -Force $downloadDir -ErrorAction SilentlyContinue
Write-Success "Removed temp files"
}
# Keep review folder unless ApplyChanges was used
if ($ApplyChanges) {
$reviewDir = ".\audit-review-$LeadId"
if (Test-Path $reviewDir) {
Remove-Item -Recurse -Force $reviewDir
}
}
# Delete workflow run (clears public logs)
if ($SelfDelete) {
Write-Step "Deleting workflow run..."
& gh run delete $runId --repo $Repo
if ($LASTEXITCODE -eq 0) {
Write-Success "Workflow run deleted (no public trace)"
} else {
Write-Host " Warning: Could not delete workflow run (may still be visible)"
}
}
# Summary
Write-Step "Done!"
Write-Host ""
Write-Host " Lead ID: $LeadId" -ForegroundColor White
Write-Host " Domain: $Domain" -ForegroundColor White
if ($ProfileRange) {
Write-Host " Profile: leads\profiles\$ProfileRange\$LeadId-*" -ForegroundColor White
}
Write-Host ""
Write-Host "Results ready for review." -ForegroundColor Green