#Requires -Version 7.0 [CmdletBinding()] param( [Parameter(Mandatory)][string]$ServerUrl, [Parameter(Mandatory)][string]$Repository, [Parameter(Mandatory)][string]$Tag, [string]$ArtifactDirectory = (Join-Path $PSScriptRoot '..\artifacts\release') ) $ErrorActionPreference = 'Stop' if ([string]::IsNullOrWhiteSpace($env:PCL_RELEASE_TOKEN)) { throw 'Provide the job token GITEA_TOKEN or repository secret RELEASE_TOKEN with permission to publish releases.' } if ($Tag -cnotmatch '^v\d+\.\d+\.\d+$') { throw 'Release tags must use vMAJOR.MINOR.PATCH, for example v0.1.2.' } if ($Repository -notmatch '^[^/]+/[^/]+$') { throw 'Repository must use owner/repository format.' } $exePath = Join-Path $ArtifactDirectory 'PCL-ISE.exe' $hashPath = Join-Path $ArtifactDirectory 'PCL-ISE.exe.sha256' foreach ($path in @($exePath, $hashPath)) { if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { throw 'A release asset is missing.' } } $version = [Reflection.AssemblyName]::GetAssemblyName($exePath).Version if ($version -ne [Version]($Tag.Substring(1) + '.0')) { throw 'Release tag does not match the executable assembly version.' } $expectedHash = (Get-FileHash -LiteralPath $exePath -Algorithm SHA256).Hash.ToLowerInvariant() $checksum = [IO.File]::ReadAllText($hashPath).Trim() if ($checksum -cne "$expectedHash PCL-ISE.exe") { throw 'The release checksum does not match the executable.' } $repositoryPath = ($Repository.Split('/') | ForEach-Object { [Uri]::EscapeDataString($_) }) -join '/' $apiUrl = $ServerUrl.TrimEnd('/') + '/api/v1/repos/' + $repositoryPath + '/releases' $headers = @{ Authorization = 'token ' + $env:PCL_RELEASE_TOKEN } try { $null = Invoke-RestMethod -Method Get -Uri "$apiUrl/tags/$Tag" -Headers $headers throw 'A release already exists for this tag. Existing releases are never overwritten.' } catch { if (-not $_.Exception.Response -or [int]$_.Exception.Response.StatusCode -ne 404) { throw } } $notesPath = Join-Path $PSScriptRoot ('..\docs\changelog\' + $Tag.Substring(1) + '.md') if (-not (Test-Path -LiteralPath $notesPath -PathType Leaf)) { throw 'Add docs/changelog/MAJOR.MINOR.PATCH.md before publishing a release.' } $payload = @{ tag_name = $Tag name = "PCL-ISE $Tag" body = [IO.File]::ReadAllText($notesPath) draft = $true prerelease = $false } | ConvertTo-Json $release = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -ContentType 'application/json; charset=utf-8' -Body ([Text.Encoding]::UTF8.GetBytes($payload)) # Keep the release as a draft if either asset upload fails. foreach ($path in @($exePath, $hashPath)) { $name = [Uri]::EscapeDataString([IO.Path]::GetFileName($path)) $null = Invoke-RestMethod -Method Post -Uri "$apiUrl/$($release.id)/assets?name=$name" -Headers $headers -Form @{ attachment = Get-Item -LiteralPath $path } } $published = Invoke-RestMethod -Method Patch -Uri "$apiUrl/$($release.id)" -Headers $headers -ContentType 'application/json' -Body '{"draft":false}' Write-Host ('Published release: ' + $published.html_url)