kiwix-js-pwa/scripts/Rewrite-DraftReleaseTag.ps1
Jaifroid 5f23e17a4e Update Rewrite-DraftReleaseTag.ps1
Former-commit-id: 861e56e895abfc710ea63cf61ee0b29e7a740b95 [formerly f00a03344f0ab4f7df3aa26c5b70d69dbad2540f [formerly 059425ad3ba51b8ee3c276a3f9d9885201db396a]]
Former-commit-id: 54e2860e37834961371b0429dc7209077c3b7990 [formerly 1e2922efc2001c557704ef832527a3fa1405f567]
Former-commit-id: 9ee01de68192091b5c19254e18b110019a77a9a8
2022-06-06 01:32:34 +01:00

90 lines
3.0 KiB
PowerShell

# Rewrites the draft release on the server to enable Electron cloud building
# Automatic if $INPUT_VERSION is provided, or else explicitly set $from and $to
[CmdletBinding()]
param (
[switch]$dryrun = $false,
[string]$from = '',
[string]$to = ''
)
if ($INPUT_TARGET -eq "nightly") {
$CRON_LAUNCHED = "1"
}
if ($INPUT_VERSION) {
$from = $INPUT_VERSION
if ($from -match '^v[0-9.]+(-WikiMed|-Wikivoyage)?$') {
$to = $from -replace '^(v[0-9.]+)(.*)$', '$1-E'
} elseif ($from -cmatch '^v[0-9.]+-E') {
$to = $INPUT_VERSION
}
}
if (-not $CRON_LAUNCHED) {
if (-not ($from -and $to)) {
"`nFrom and To inputs were not provided or could not be determeined automatically!"
exit 0
} else {
"Rewriting draft version from $from to $to..."
}
"`nChecking for a draft publishing target on GitHub..."
if (-not $GITHUB_TOKEN) {
$GITHUB_TOKEN = Get-Content -Raw "$PSScriptRoot/github_token"
}
$draft_release_params = @{
Uri = "https://api.github.com/repos/kiwix/kiwix-js-windows/releases"
Method = 'GET'
Headers = @{
'Authorization' = "token $GITHUB_TOKEN"
'Accept' = 'application/vnd.github.v3+json'
}
ContentType = "application/json"
}
$releases = Invoke-RestMethod @draft_release_params
$release_found = $false
$release = $null
$base_input = $INPUT_VERSION -replace '^(v[0-9.]+).*', '$1'
$releases | Where-Object { $release_found -eq $False } | % {
$release = $_
if (($release.draft -eq $true) -and ($release.tag_name -match $base_input)) {
$release_found = $true
}
}
if ($release_found) {
if ($dryrun) {
$release_json = $release | ConvertTo-Json
"[DRYRUN:] Draft release found: `n$release_json"
}
$release_body_json = @{
'tag_name' = "$to"
'draft' = $true
} | ConvertTo-Json
# Explicitly encode as UTF8 (or else it will fail with UTF8 characters)
# $release_body_json = ([System.Text.Encoding]::UTF8.GetBytes($release_body_json))
$release_params = @{
Uri = $release.url
Method = 'POST'
Headers = @{
'Authorization' = "token $GITHUB_TOKEN"
'Accept' = 'application/vnd.github.v3+json'
}
Body = $release_body_json
ContentType = "application/json"
}
# Post to the release server
if (-Not $dryrun) {
$release = Invoke-RestMethod @release_params
# Change the INPUT_VERSION
Set-Variable 'INPUT_VERSION' $to -Scope Global
"Posted rewrite request to server and setting INPUT_VERSION to $INPUT_VERSION...`n"
} else {
"[DRYRUN] Release Body:`n" + ($release_params | ConvertTo-Json)
}
} else {
"No draft release matching the tag $INPUT_VERSION was found."
}
}