Skip to content

Commit 2ebe863

Browse files
committed
fix(release): 通过已验证 Tag 发布以保留最小令牌权限
GitHub API 在显式 target_commitish 与默认分支 workflow 不一致时要求额外 workflow 写权限。Tag 已在远端核实,无需传该字段;用不可移动的 Tag、源码链接及包身份核验恢复/重复发布,新增不同源码拒绝测试。
1 parent 8ad9edc commit 2ebe863

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎tests/run-release-tests.ps1‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ $releaseTestOldToken = $env:GH_TOKEN
2727
$env:GH_TOKEN = 'synthetic-test-token'
2828
$releaseTestResults = @()
2929
try {
30-
foreach ($releaseTestScenario in @('new','annotated-tag','existing-exact','existing-different','draft-resume','draft-page-two','duplicate-draft','draft-starter','upload-failure','wrong-remote-tag','forbidden','incomplete-assets','unexpected-draft-asset','checksum-mismatch','tag-version-mismatch')) {
30+
foreach ($releaseTestScenario in @('new','annotated-tag','existing-exact','existing-different','existing-wrong-source','draft-resume','draft-page-two','duplicate-draft','draft-starter','upload-failure','wrong-remote-tag','forbidden','incomplete-assets','unexpected-draft-asset','checksum-mismatch','tag-version-mismatch')) {
3131
Reset-TestChecksum
3232
& {
3333
$releaseTestState = @{calls=[Collections.Generic.List[object]]::new();release=$null}
34-
if ($releaseTestScenario -in @('existing-exact','existing-different','draft-resume','draft-page-two','duplicate-draft','draft-starter','unexpected-draft-asset')) {
34+
if ($releaseTestScenario -in @('existing-exact','existing-different','existing-wrong-source','draft-resume','draft-page-two','duplicate-draft','draft-starter','unexpected-draft-asset')) {
3535
$releaseTestState.release = [pscustomobject]@{
3636
id=1;tag_name=$releaseTestVersion.Tag;target_commitish=$releaseTestSource
3737
draft=($releaseTestScenario -in @('draft-resume','draft-page-two','duplicate-draft','draft-starter','unexpected-draft-asset'))
3838
assets=@((New-TestAsset $releaseTestArchive),(New-TestAsset $releaseTestChecksum))
3939
html_url='https://github.com/fixture/repository/releases/example'
40+
body="Source: https://github.com/fixture/repository/commit/$releaseTestSource"
4041
}
4142
if ($releaseTestScenario -eq 'existing-different') { $releaseTestState.release.assets[0].digest='sha256:wrong' }
43+
if ($releaseTestScenario -eq 'existing-wrong-source') { $releaseTestState.release.body='Source: different' }
4244
if ($releaseTestScenario -in @('draft-resume','draft-page-two')) { $releaseTestState.release.assets=@($releaseTestState.release.assets[0]) }
4345
if ($releaseTestScenario -eq 'draft-starter') {
4446
$releaseTestState.release.assets[0]=[pscustomobject]@{id=123;name=([IO.Path]::GetFileName($releaseTestArchive));state='starter';size=0;digest=$null}
@@ -83,8 +85,9 @@ try {
8385
if ($Uri -eq 'https://api.github.com/repos/fixture/repository/releases' -and $Method -eq 'POST') {
8486
Assert-Test $payload.draft 'Creation must remain private until assets pass'
8587
Assert-Test $payload.generate_release_notes 'Automatic notes were not requested'
88+
Assert-Test ($payload.PSObject.Properties.Name -notcontains 'target_commitish') 'An existing tag needs no target_commitish/workflow-write permission'
8689
$releaseTestState.release = [pscustomobject]@{
87-
id=1;tag_name=$payload.tag_name;target_commitish=$payload.target_commitish;draft=$true;assets=@()
90+
id=1;tag_name=$payload.tag_name;target_commitish='main';body=$payload.body;draft=$true;assets=@()
8891
html_url='https://github.com/fixture/repository/releases/example'
8992
}
9093
return $releaseTestState.release
@@ -120,7 +123,7 @@ try {
120123
Assert-Test ($failed -ne $success) "Unexpected outcome: $releaseTestScenario"
121124
$published=@($releaseTestState.calls | Where-Object method -EQ 'PATCH')
122125
Assert-Test ($published.Count -eq $(if ($success -and $releaseTestScenario -ne 'existing-exact') { 1 } else { 0 })) "Publication boundary failed: $releaseTestScenario"
123-
if ($releaseTestScenario -in @('existing-exact','existing-different','duplicate-draft','wrong-remote-tag','forbidden','checksum-mismatch','tag-version-mismatch')) {
126+
if ($releaseTestScenario -in @('existing-exact','existing-different','existing-wrong-source','duplicate-draft','wrong-remote-tag','forbidden','checksum-mismatch','tag-version-mismatch')) {
124127
Assert-Test (@($releaseTestState.calls | Where-Object method -NE 'GET').Count -eq 0) "Unexpected mutation: $releaseTestScenario"
125128
}
126129
if ($published.Count) {

‎tools/publish-release.ps1‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if (-not $env:GH_TOKEN) { throw 'GH_TOKEN with contents:write is required.' }
1212
$taskVersion = & (Join-Path $PSScriptRoot 'check-version.ps1') -ExpectedTag $Tag -VerifyTag
1313
$taskSource = (& git -C (Split-Path $PSScriptRoot -Parent) rev-parse HEAD).Trim()
1414
if ($LASTEXITCODE -ne 0) { throw 'Cannot identify the release source.' }
15+
$taskSourceLine = "Source: https://github.com/$Repository/commit/$taskSource"
1516
$taskArchive = (Resolve-Path -LiteralPath $ArchivePath).Path
1617
$taskName = [IO.Path]::GetFileName($taskArchive)
1718
if ($taskName -cne ($taskVersion.ArchiveStem + '-windows-x64.zip')) { throw 'Archive name does not match the source version and commit.' }
@@ -80,16 +81,19 @@ $taskPrerelease = $taskVersion.Version.Split('+')[0].Contains('-')
8081
if ($null -eq $taskRelease) {
8182
$taskBody = @{
8283
tag_name = $Tag
83-
target_commitish = $taskSource
84+
# The tag exists and was checked above. Omitting target_commitish
85+
# avoids asking GITHUB_TOKEN for workflow-write permission on a dev
86+
# commit whose workflows differ from the default branch.
8487
name = "NR Nexus $Tag"
8588
draft = $true
8689
prerelease = $taskPrerelease
8790
generate_release_notes = $true
88-
body = "Windows x64 add-on / Windows x64 插件。Installation and prerequisites / 安装与前置条件:see the README inside the ZIP. NVIDIA runtimes/models and the ReShade loader are obtained separately under their own licenses.`n`nSource: https://github.com/$Repository/commit/$taskSource"
91+
body = "Windows x64 add-on / Windows x64 插件。Installation and prerequisites / 安装与前置条件:see the README inside the ZIP. NVIDIA runtimes/models and the ReShade loader are obtained separately under their own licenses.`n`n$taskSourceLine"
8992
}
9093
$taskRelease = Invoke-ReleaseApi 'releases' 'POST' $taskBody
9194
}
92-
if ($taskRelease.target_commitish -cne $taskSource -or $taskRelease.tag_name -cne $Tag) {
95+
if ($taskRelease.tag_name -cne $Tag -or
96+
-not [regex]::IsMatch($taskRelease.body, '(?m)^' + [regex]::Escape($taskSourceLine) + '\r?$')) {
9397
throw 'An existing release targets different source; no changes were made to it.'
9498
}
9599
foreach ($taskFile in $taskFiles) {

0 commit comments

Comments
 (0)