From dac10a52bc07ed482ba6f9a961322a4fcfd0432c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Sun, 29 Nov 2026 04:00:37 -0700 Subject: [PATCH] ci(release): build release notes from CHANGELOG.md - scripts/build-release-notes.ps1 extracts the `## ` section and wraps it with download, stable runtime, license and compare links; it fails if the section is missing or empty. - CI checks that CHANGELOG.md has notes for the version in version.go. - release.yml uses those notes instead of --generate-notes, checks out the dispatched tag, fails on gh errors, and backfills notes on an existing release whose body is empty or only the auto "Full Changelog" link. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 13 +++ .github/workflows/release.yml | 58 ++++++++++- scripts/build-release-notes.ps1 | 173 ++++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 scripts/build-release-notes.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ba1daa..86bfd2a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,19 @@ jobs: cache: pnpm cache-dependency-path: frontend/pnpm-lock.yaml + - name: Verify CHANGELOG.md matches current version + shell: pwsh + run: | + $ErrorActionPreference = 'Stop' + $verFile = Get-Content -Raw -LiteralPath 'internal/version/version.go' + if ($verFile -notmatch 'var\s+Version\s*=\s*"(v[^"]+)"') { + throw 'Could not parse Version from internal/version/version.go' + } + $ver = $Matches[1] + $tmp = Join-Path $env:RUNNER_TEMP "release-notes-$ver.md" + & ./scripts/build-release-notes.ps1 -Tag $ver -OutFile $tmp + Write-Host "PASS CHANGELOG.md contains valid release notes for $ver ($((Get-Item -LiteralPath $tmp).Length) bytes)" + - name: Frontend build shell: pwsh run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 553e1ea..3afe743 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,6 +18,8 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || '' }} - uses: actions/setup-go@v5 with: @@ -40,6 +42,39 @@ jobs: if (-not $tag.StartsWith('v')) { throw "Release tag must start with 'v', got: $tag" } "RELEASE_TAG=$tag" | Out-File -FilePath $env:GITHUB_ENV -Append + - name: Generate release notes from CHANGELOG.md + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: pwsh + run: | + $ErrorActionPreference = 'Stop' + $tag = $env:RELEASE_TAG + $runtimeTag = '' + $runtimeTitle = '' + try { + $rels = gh release list -R $env:GITHUB_REPOSITORY --limit 100 --json tagName,name,isPrerelease,isDraft,createdAt | ConvertFrom-Json + $stable = $rels | + Where-Object { $_.tagName -like 'runtime-*' -and -not $_.isPrerelease -and -not $_.isDraft } | + Sort-Object createdAt -Descending | + Select-Object -First 1 + if ($stable) { + $runtimeTag = $stable.tagName + $runtimeTitle = if ($stable.name) { $stable.name } else { $stable.tagName } + } + } catch { + Write-Warning "Could not query latest stable runtime release: $_" + } + $notesFile = Join-Path $env:RUNNER_TEMP "release-notes-$tag.md" + & ./scripts/build-release-notes.ps1 ` + -Tag $tag ` + -ChangelogPath 'CHANGELOG.md' ` + -Repo $env:GITHUB_REPOSITORY ` + -StableRuntimeTag $runtimeTag ` + -StableRuntimeTitle $runtimeTitle ` + -OutFile $notesFile + "RELEASE_NOTES_FILE=$notesFile" | Out-File -FilePath $env:GITHUB_ENV -Append + Write-Host "Generated release notes ($((Get-Item -LiteralPath $notesFile).Length) bytes) for $tag" + - name: Build frontend shell: pwsh run: | @@ -82,18 +117,35 @@ jobs: run: | $ErrorActionPreference = 'Stop' $tag = $env:RELEASE_TAG + $notesFile = $env:RELEASE_NOTES_FILE + if (-not (Test-Path -LiteralPath $notesFile) -or (Get-Item -LiteralPath $notesFile).Length -eq 0) { + throw "Release notes file is missing or empty: $notesFile" + } $files = @(Get-ChildItem -LiteralPath 'dist-release' -File | Select-Object -ExpandProperty FullName) $exists = $true + $existingBody = '' try { - gh release view $tag *> $null - if ($LASTEXITCODE -ne 0) { $exists = $false } + $relJson = gh release view $tag --json body 2>$null + if ($LASTEXITCODE -ne 0) { + $exists = $false + } else { + $existingBody = (($relJson | ConvertFrom-Json).body | Out-String).Trim() + } } catch { $exists = $false } if ($exists) { gh release upload $tag @files --clobber + if ($LASTEXITCODE -ne 0) { throw "gh release upload failed ($LASTEXITCODE)" } + # Backfill notes if the existing release body is empty or only GitHub's auto-generated compare link. + $isAutoOnly = [string]::IsNullOrWhiteSpace($existingBody) -or ($existingBody -match '^\*\*Full Changelog\*\*:\s*https://\S+$') + if ($isAutoOnly) { + gh release edit $tag --notes-file $notesFile + if ($LASTEXITCODE -ne 0) { throw "gh release edit failed ($LASTEXITCODE)" } + } } else { gh release create $tag @files ` --title "Reims-Go $tag" ` - --generate-notes + --notes-file $notesFile + if ($LASTEXITCODE -ne 0) { throw "gh release create failed ($LASTEXITCODE)" } } diff --git a/scripts/build-release-notes.ps1 b/scripts/build-release-notes.ps1 new file mode 100644 index 0000000..108f4da --- /dev/null +++ b/scripts/build-release-notes.ps1 @@ -0,0 +1,173 @@ +#Requires -Version 7.0 +<# +.SYNOPSIS + Build GitHub Release notes for a Reims-Go app tag from CHANGELOG.md. + +.DESCRIPTION + Extracts the matching `## ` section from CHANGELOG.md and wraps it with + download, stable runtime, known limitation, license, and compare links. + Fails fast if CHANGELOG.md has no entry for -Tag or the section is empty, + preventing releases with empty/auto-only notes. +#> +[CmdletBinding()] +param( + [Parameter(Mandatory = $true)] + [string]$Tag, + [string]$ChangelogPath = 'CHANGELOG.md', + [string]$OutFile = '', + [string]$Repo = 'Nwflower/Reims-Go', + [string]$PrevTag = '', + [string]$StableRuntimeTag = '', + [string]$StableRuntimeTitle = '', + [string]$Summary = '' +) + +$ErrorActionPreference = 'Stop' + +if ($Tag -notmatch '^v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$') { + throw "Invalid release tag '$Tag' (expected semantic version starting with 'v', e.g. v0.2.5)." +} + +if (-not (Test-Path -LiteralPath $ChangelogPath)) { + throw "Changelog file not found: $ChangelogPath" +} + +$utf8NoBom = [Text.UTF8Encoding]::new($false) +$raw = [IO.File]::ReadAllText((Resolve-Path -LiteralPath $ChangelogPath).Path, $utf8NoBom) +$lines = $raw -split "\r?\n" + +$headingPattern = '^##\s+(?v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)(?:\s|$|[—–\-\(])' + +$inSection = $false +$found = $false +$sectionLines = [Collections.Generic.List[string]]::new() +$detectedPrevTag = '' + +foreach ($line in $lines) { + if ($line -match '^##\s+') { + if ($inSection) { + if ($line -match $headingPattern) { + $detectedPrevTag = $Matches['tag'] + } + break + } + if ($line -match $headingPattern -and $Matches['tag'] -eq $Tag) { + $inSection = $true + $found = $true + continue + } + } elseif ($inSection) { + $sectionLines.Add($line) + } +} + +if (-not $found) { + throw "CHANGELOG.md does not contain a '## $Tag' section. Add release notes for $Tag to $ChangelogPath before releasing." +} + +# Trim leading and trailing blank lines. +while ($sectionLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($sectionLines[0])) { + $sectionLines.RemoveAt(0) +} +while ($sectionLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($sectionLines[$sectionLines.Count - 1])) { + $sectionLines.RemoveAt($sectionLines.Count - 1) +} + +$hasContent = $false +foreach ($l in $sectionLines) { + $t = $l.Trim() + if ($t -ne '' -and $t -notmatch '^#+\s+') { + $hasContent = $true + break + } +} +if (-not $hasContent) { + throw "CHANGELOG.md section '## $Tag' has no release note entries." +} + +# If CHANGELOG.md has an introductory paragraph before the first ### subheading, +# place it at the top of the release notes (above ### 下载). +$leadLines = [Collections.Generic.List[string]]::new() +$bodyLines = [Collections.Generic.List[string]]::new() +$seenSubheading = $false +foreach ($l in $sectionLines) { + if (-not $seenSubheading -and $l -match '^###\s+') { + $seenSubheading = $true + } + if ($seenSubheading) { + $bodyLines.Add($l) + } else { + $leadLines.Add($l) + } +} + +if (-not $seenSubheading) { + # Section has prose/bullets without ### subheadings: keep them under ### 变更说明. + $bodyLines.Add('### 变更说明') + $bodyLines.Add('') + foreach ($l in $leadLines) { $bodyLines.Add($l) } + $leadLines.Clear() +} else { + while ($leadLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($leadLines[$leadLines.Count - 1])) { + $leadLines.RemoveAt($leadLines.Count - 1) + } +} + +if ([string]::IsNullOrWhiteSpace($PrevTag)) { + $PrevTag = $detectedPrevTag +} + +$blocks = [Collections.Generic.List[string]]::new() + +if (-not [string]::IsNullOrWhiteSpace($Summary)) { + $blocks.Add("Reims-Go ${Tag}:$($Summary.Trim())") +} elseif ($leadLines.Count -gt 0) { + $blocks.Add(($leadLines -join "`n").Trim()) +} + +$downloadLines = @( + '### 下载' + '' + "- ``reims-go-$Tag-windows-amd64.exe``:单文件,免安装。双击打开界面,带子命令即在终端运行(如 ``reims-go vm list``);" + '- `SHA256SUMS.txt`:上面文件的 SHA256。' +) +if (-not [string]::IsNullOrWhiteSpace($StableRuntimeTag)) { + $rtLabel = if (-not [string]::IsNullOrWhiteSpace($StableRuntimeTitle)) { $StableRuntimeTitle } else { $StableRuntimeTag } + $downloadLines += '' + $downloadLines += "当前稳定版运行时为 [$rtLabel](https://github.com/$Repo/releases/tag/$StableRuntimeTag),已经装过的不用重装。" +} +$blocks.Add(($downloadLines -join "`n")) + +$blocks.Add(($bodyLines -join "`n").Trim()) + +$blocks.Add(@" +### 已知限制 + +- 没有代码签名,第一次运行时 Windows SmartScreen 会拦截,需要点「更多信息」→「仍要运行」。 +"@.Trim()) + +$blocks.Add(@" +### 许可证 + +Reims-Go 自身代码采用 MIT 许可证。运行时组件按各自的 GPL/LGPL 许可证分发,对应源码与许可证文本随运行时 Release 一起提供。 + +本应用不包含、也不分发任何 Apple 系统文件;macOS 安装包由你的电脑直接从 Apple 服务器下载。macOS 的软件许可只允许在 Apple 硬件上运行 macOS 虚拟机,使用前请自行确认合规。 +"@.Trim()) + +if (-not [string]::IsNullOrWhiteSpace($PrevTag)) { + $blocks.Add("完整更新日志见 [CHANGELOG.md](https://github.com/$Repo/blob/main/CHANGELOG.md) · 完整提交对比:[$PrevTag...$Tag](https://github.com/$Repo/compare/$PrevTag...$Tag)。") +} else { + $blocks.Add("完整更新日志见 [CHANGELOG.md](https://github.com/$Repo/blob/main/CHANGELOG.md)。") +} + +$output = ($blocks -join "`n`n") + "`n" + +if (-not [string]::IsNullOrWhiteSpace($OutFile)) { + $parent = Split-Path -Parent $OutFile + if (-not [string]::IsNullOrWhiteSpace($parent)) { + New-Item -ItemType Directory -Force -Path $parent | Out-Null + } + [IO.File]::WriteAllText($OutFile, $output, $utf8NoBom) +} else { + [Console]::Out.Write($output) +}