Skip to content

Commit 70fcb5a

Browse files
Harden stable push routing
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent a758ef4 commit 70fcb5a

4 files changed

Lines changed: 56 additions & 7 deletions

File tree

.github/actions/Get-PSModuleSettings/src/Get-PSModuleSettings.Helpers.psm1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function Resolve-WorkflowEventRouting {
7575
$HasImportantChanges
7676
)
7777
ShouldCleanupEvent = $isClosedPR
78+
ShouldRunCleanup = $isClosedPR -or $shouldRelease
7879
}
7980
}
8081

@@ -128,3 +129,23 @@ function Get-FilesFromGitTree {
128129
Where-Object { $_.type -eq 'blob' } |
129130
Select-Object -ExpandProperty path
130131
}
132+
133+
function Get-FilesFromGitHubComparison {
134+
<#
135+
.SYNOPSIS
136+
Returns files from a complete GitHub compare response.
137+
#>
138+
[CmdletBinding()]
139+
[OutputType([string])]
140+
param(
141+
[Parameter(Mandatory)]
142+
[PSCustomObject] $Comparison
143+
)
144+
145+
$files = @($Comparison.files | Where-Object { $null -ne $_ })
146+
if ($files.Count -ge 300) {
147+
throw 'Cannot determine changed files because the GitHub compare response reached its 300-file limit.'
148+
}
149+
150+
$files | Select-Object -ExpandProperty filename
151+
}

.github/actions/Get-PSModuleSettings/src/main.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ LogGroup 'Calculate Job Run Conditions:' {
325325
# Check if important files have changed in the PR
326326
# Important files are determined by the configured ImportantFilePatterns setting
327327
$hasImportantChanges = $false
328-
if ($pullRequestContext.Number) {
328+
if ($eventName -eq 'pull_request' -and $pullRequestContext.Number) {
329329
LogGroup 'Check for Important File Changes' {
330330
$owner = $env:GITHUB_REPOSITORY_OWNER
331331
$repo = $env:GITHUB_REPOSITORY_NAME
@@ -416,10 +416,8 @@ If you believe this is incorrect, please verify that your changes are in the cor
416416
$changedFiles = Get-FilesFromGitTree -Tree $tree
417417
} else {
418418
Write-Host "Fetching changed files between [$beforeCommitSha] and [$commitSha]..."
419-
$changedFiles = Invoke-GitHubAPI -ApiEndpoint "/repos/$owner/$repo/compare/$beforeCommitSha...$commitSha" -Method GET |
420-
Select-Object -ExpandProperty Response |
421-
Select-Object -ExpandProperty files |
422-
Select-Object -ExpandProperty filename
419+
$comparison = (Invoke-GitHubAPI -ApiEndpoint "/repos/$owner/$repo/compare/$beforeCommitSha...$commitSha" -Method GET).Response
420+
$changedFiles = Get-FilesFromGitHubComparison -Comparison $comparison
423421
}
424422

425423
Write-Host "Changed files ($($changedFiles.Count)):"
@@ -633,7 +631,7 @@ $settings.Test.Module | Add-Member -MemberType NoteProperty -Name Suites -Value
633631

634632
# Calculate job-specific conditions and add to settings
635633
LogGroup 'Calculate Job Run Conditions:' {
636-
$shouldAutoCleanup = $routing.ShouldCleanupEvent -and ($settings.Publish.Module.AutoCleanup -eq $true)
634+
$shouldAutoCleanup = $routing.ShouldRunCleanup -and ($settings.Publish.Module.AutoCleanup -eq $true)
637635

638636
# Update Publish.Module with computed release values
639637
$settings.Publish.Module | Add-Member -MemberType NoteProperty -Name ReleaseType -Value $releaseType -Force

.github/actions/Get-PSModuleSettings/tests/Get-PSModuleSettings.Helpers.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Describe 'Resolve-WorkflowEventRouting' {
1212
$result.ReleaseType | Should -Be 'Release'
1313
$result.ShouldRunBuildTest | Should -BeTrue
1414
$result.ShouldCleanupEvent | Should -BeFalse
15+
$result.ShouldRunCleanup | Should -BeTrue
1516
}
1617

1718
It 'routes a direct push to the default branch to a stable release' {
@@ -62,6 +63,7 @@ Describe 'Resolve-WorkflowEventRouting' {
6263
$result.ReleaseType | Should -Be 'None'
6364
$result.ShouldRunBuildTest | Should -BeFalse
6465
$result.ShouldCleanupEvent | Should -BeTrue
66+
$result.ShouldRunCleanup | Should -BeTrue
6567
}
6668

6769
It 'does not run a label event for a closed PR' {
@@ -76,6 +78,7 @@ Describe 'Resolve-WorkflowEventRouting' {
7678
$result.IsOpenOrUpdatedPR | Should -BeFalse
7779
$result.ShouldRunBuildTest | Should -BeFalse
7880
$result.ShouldCleanupEvent | Should -BeFalse
81+
$result.ShouldRunCleanup | Should -BeFalse
7982
}
8083
}
8184

@@ -148,3 +151,29 @@ Describe 'Get-FilesFromGitTree' {
148151
{ Get-FilesFromGitTree -Tree $tree } | Should -Throw '*tree response was truncated*'
149152
}
150153
}
154+
155+
Describe 'Get-FilesFromGitHubComparison' {
156+
It 'returns filenames from a compare response below the file limit' {
157+
$comparison = [pscustomobject]@{
158+
files = @(
159+
[pscustomobject]@{ filename = 'src/Module.psm1' }
160+
[pscustomobject]@{ filename = 'README.md' }
161+
)
162+
}
163+
164+
$result = Get-FilesFromGitHubComparison -Comparison $comparison
165+
166+
$result | Should -Be @('src/Module.psm1', 'README.md')
167+
}
168+
169+
It 'rejects a compare response that reaches the file limit' {
170+
$comparison = [pscustomobject]@{
171+
files = @(1..300 | ForEach-Object {
172+
[pscustomobject]@{ filename = "src/File$_.ps1" }
173+
})
174+
}
175+
176+
{ Get-FilesFromGitHubComparison -Comparison $comparison } |
177+
Should -Throw '*compare response reached its 300-file limit*'
178+
}
179+
}

docs/content/reference/scenario-matrix.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ execution; other pages link here rather than repeating it.
2424
| **Get-TestResults** | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes |
2525
| **Get-CodeCoverage** | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes |
2626
| **Publish-Site** | ❌ No | ✅ Yes* | ❌ No | ✅ Yes* |
27-
| **Publish-Module** | ✅ Prerelease† | ✅ Stable† | ✅ Cleanup‡ | ✅ Stable† |
27+
| **Publish-Module** | ✅ Prerelease† | ✅ Stable†§ | ✅ Cleanup‡ | ✅ Stable†§ |
2828

2929
- \* Only when `Publish.Site.Skip` is `false`.
3030
- † Requires an important change and all required build, test, and coverage gates to succeed. An open PR also requires
@@ -33,6 +33,7 @@ execution; other pages link here rather than repeating it.
3333
release with commit-based notes.
3434
- ‡ Cleans up prerelease versions and tags for the closed pull request when `Publish.Module.AutoCleanup` is enabled;
3535
it does not publish a stable release.
36+
- § A successful stable release also retries prerelease cleanup when `Publish.Module.AutoCleanup` is enabled.
3637

3738
A job that is enabled by this matrix can still be skipped by a setting (for example `Test.Skip`) or because an open PR
3839
or default-branch push changed no

0 commit comments

Comments
 (0)