glp-companion/scripts/test.ps1

52 lines
2.0 KiB
PowerShell

$Server = "https://git.kellsupport.com"
$Owner = "mtkell"
$Repo = "glp-companion"
$Tag = "v0.0.1-test"
# Token is read from the environment only — never hardcode credentials here.
$Token = $env:FORGEJO_TOKEN
if (-not $Token) {
Write-Error "FORGEJO_TOKEN environment variable is not set. Export it before running this script."
}
$DummyFile = "dummy-asset-test.txt"
"Forgejo API asset upload verification payload - $(Get-Date)" | Set-Content -Path $DummyFile -Encoding utf8
$Headers = @{
"Authorization" = "token $Token"
"Accept" = "application/json"
}
Write-Host "Checking for release for tag: $Tag..." -ForegroundColor Cyan
$ReleaseUrl = "$Server/api/v1/repos/$Owner/$Repo/releases"
try {
$Release = Invoke-RestMethod -Uri "$ReleaseUrl/tags/$Tag" -Headers $Headers -Method Get
$ReleaseId =$Release.id
Write-Host "Found existing release ID: $ReleaseId" -ForegroundColor Green
} catch {
Write-Host "Creating new draft test release for tag $Tag..." -ForegroundColor Yellow
$Release = Invoke-RestMethod -Uri $ReleaseUrl -Headers $Headers -Method Post -ContentType "application/json" -Body (@{
tag_name = $Tag
name = "Test Asset Upload Release"
body = "Testing Forgejo attachment upload endpoint."
draft = $true
prerelease = $true
} | ConvertTo-Json)
$ReleaseId =$Release.id
Write-Host "Created test release ID: $ReleaseId" -ForegroundColor Green
}
Write-Host "Uploading $DummyFile to release ID$ReleaseId via curl..." -ForegroundColor Cyan
$UploadUrl = "$Server/api/v1/repos/$Owner/$Repo/releases/$ReleaseId/assets?name=$DummyFile"
& curl.exe -s -X POST $UploadUrl `
-H "Authorization: token $Token" `
-F "attachment=@$DummyFile" `
-o "curl_response.json"
Write-Host "`nServer Response:" -ForegroundColor Gray
Get-Content -Path "curl_response.json"
Remove-Item -Path $DummyFile -Force -ErrorAction SilentlyContinue
Remove-Item -Path "curl_response.json" -Force -ErrorAction SilentlyContinue