#Requires -Version 7.0 <# .SYNOPSIS Decide whether the daily alpha pipeline should build, and which tag to use. .DESCRIPTION Finds the newest commit reachable from -Ref whose subject is a Conventional Commits fix (`fix: ...`, `fix(scope): ...`, `fix!: ...`), then: - skips when there is no fix commit, or the commit is already part of the latest stable tag (vX.Y.Z); - reuses an existing vX.Y.Z-alpha[.N] tag on that commit, skipping when its GitHub Release already exists and rebuilding when an earlier run failed before publishing; - otherwise allocates the next vX.Y.Z-alpha.N, where vX.Y.Z is the next stable version (the patch after the latest stable tag, or the version in internal/version/version.go when that is higher). The decision is printed and, under GitHub Actions, written to $GITHUB_OUTPUT as `build`, `tag`, `commit`, `new_tag`, and `reason`. The script does not create or push tags. #> [CmdletBinding()] param( [string]$Ref = 'HEAD', [string]$Repo = 'Nwflower/Reims-Go', [string]$VersionFile = 'internal/version/version.go' ) $ErrorActionPreference = 'Stop' $fixSubject = '^fix(\([^)]*\))?!?:\s' $alphaTagPattern = '^v([0-9]+)\.([0-9]+)\.([0-9]+)-alpha(?:\.([0-9]+))?$' function Invoke-Git { $out = @(& git @args) if ($LASTEXITCODE -ne 0) { throw "git $($args -join ' ') failed ($LASTEXITCODE)" } return $out } function ConvertTo-StableVersion([string]$v) { if ($v -match '^v([0-9]+)\.([0-9]+)\.([0-9]+)$') { return [PSCustomObject]@{ Tag = $v; Major = [int]$Matches[1]; Minor = [int]$Matches[2]; Patch = [int]$Matches[3] } } return $null } function Compare-StableVersion($a, $b) { if ($a.Major -ne $b.Major) { return [Math]::Sign($a.Major - $b.Major) } if ($a.Minor -ne $b.Minor) { return [Math]::Sign($a.Minor - $b.Minor) } return [Math]::Sign($a.Patch - $b.Patch) } function Test-ReleaseExists([string]$tag) { $null = gh release view $tag -R $Repo --json tagName 2>$null return $LASTEXITCODE -eq 0 } function Write-Decision([bool]$build, [string]$tag, [string]$commit, [bool]$newTag, [string]$reason) { $decision = [ordered]@{ build = $build.ToString().ToLowerInvariant() tag = $tag commit = $commit new_tag = $newTag.ToString().ToLowerInvariant() reason = $reason } foreach ($k in $decision.Keys) { Write-Host ("{0,-8} {1}" -f $k, $decision[$k]) } if ($env:GITHUB_OUTPUT) { $decision.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 } if ($env:GITHUB_STEP_SUMMARY) { $summary = if ($build) { "Alpha build: ``$tag`` from ``$commit`` ($reason)" } else { "No alpha build: $reason" } $summary | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append -Encoding utf8 } } # 1. Newest fix commit. Merge commits are skipped; the fix commits they bring in are still visited. $fixCommit = '' $fixLine = '' foreach ($line in (Invoke-Git -c i18n.logOutputEncoding=utf-8 log $Ref --no-merges '--format=%H%x09%s')) { $sha, $subject = $line -split "`t", 2 if ($subject -match $fixSubject) { $fixCommit = $sha $fixLine = $subject break } } if (-not $fixCommit) { Write-Decision $false '' '' $false "no fix commit reachable from $Ref" exit 0 } $short = $fixCommit.Substring(0, 7) Write-Host "Newest fix commit: $short $fixLine" # 2. Already shipped in a stable release? $stableTags = @(Invoke-Git tag --list 'v*' | ForEach-Object { ConvertTo-StableVersion $_.Trim() } | Where-Object { $_ } | Sort-Object Major, Minor, Patch -Descending) $latestStable = if ($stableTags.Count -gt 0) { $stableTags[0] } else { $null } if ($latestStable) { & git merge-base --is-ancestor $fixCommit $latestStable.Tag if ($LASTEXITCODE -eq 0) { Write-Decision $false '' $fixCommit $false "$short is already included in $($latestStable.Tag)" exit 0 } if ($LASTEXITCODE -ne 1) { throw "git merge-base --is-ancestor failed ($LASTEXITCODE)" } } # 3. Alpha tag already on this commit: skip if published, otherwise retry the same tag. $existing = @(Invoke-Git tag --points-at $fixCommit | ForEach-Object { $_.Trim() } | Where-Object { $_ -match $alphaTagPattern }) if ($existing.Count -gt 0) { $tag = $existing | Sort-Object { [int]($_ -replace $alphaTagPattern, '$4' -replace '^$', '0') } -Descending | Select-Object -First 1 if (Test-ReleaseExists $tag) { Write-Decision $false $tag $fixCommit $false "$short was already released as $tag" } else { Write-Decision $true $tag $fixCommit $false "$tag exists without a release; rebuilding" } exit 0 } # 4. Allocate the next alpha number for the upcoming stable version. $codeVer = $null if (Test-Path -LiteralPath $VersionFile) { $verText = Get-Content -Raw -LiteralPath $VersionFile if ($verText -match 'var\s+Version\s*=\s*"(v[0-9]+\.[0-9]+\.[0-9]+)"') { $codeVer = ConvertTo-StableVersion $Matches[1] } } if ($latestStable) { if ($codeVer -and (Compare-StableVersion $codeVer $latestStable) -gt 0) { $base = $codeVer.Tag } else { $base = "v$($latestStable.Major).$($latestStable.Minor).$($latestStable.Patch + 1)" } } elseif ($codeVer) { $base = $codeVer.Tag } else { $base = 'v0.0.1' } $maxN = 0 foreach ($t in (Invoke-Git tag --list "$base-alpha*")) { if ($t.Trim() -match $alphaTagPattern -and $Matches[4]) { $maxN = [Math]::Max($maxN, [int]$Matches[4]) } } $tag = "$base-alpha.$($maxN + 1)" Write-Decision $true $tag $fixCommit $true "new alpha for $short"