Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions scripts/Resolve-PSModuleVersion.Helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@ function Get-GitHubPullRequest {
Reads and validates the GitHub pull request from the event payload.

.DESCRIPTION
Loads the GitHub event from the input override or from the event path file,
then validates and extracts pull request data.
Loads the GitHub event from the input override or from the event path file. On a
pull_request event it returns the pull request head ref and labels. On any other
event (for example workflow_dispatch or schedule) there is no pull request, so it
returns $null and the caller resolves the current version without a version bump.

.OUTPUTS
PSCustomObject with HeadRef and Labels properties.
PSCustomObject with HeadRef and Labels properties for a pull_request event, or
$null when the event has no pull request (non-PR events).

.EXAMPLE
$pullRequest = Get-GitHubPullRequest
Expand All @@ -149,7 +152,9 @@ function Get-GitHubPullRequest {

$pr = $githubEvent.pull_request
if (-not $pr) {
throw 'GitHub event does not contain pull_request data. This action must be run from a pull_request event.'
Write-Host 'GitHub event does not contain pull_request data (non-PR event, e.g. workflow_dispatch or schedule).'
Write-Host 'No pull request context is available; the caller keeps the current version without a bump.'
return $null
}
Comment thread
MariusStorhaug marked this conversation as resolved.

$labels = @()
Expand Down
19 changes: 18 additions & 1 deletion scripts/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ Import-Module -Name "$PSScriptRoot/Resolve-PSModuleVersion.Helpers.psm1" -Force
$actionInput = Read-ActionInput
$config = Get-PublishConfiguration -SettingsJson $actionInput.SettingsJson
$pullRequest = Get-GitHubPullRequest
$decision = Resolve-ReleaseDecision -Configuration $config -PullRequest $pullRequest

$decision = if ($null -eq $pullRequest) {
# Non-PR event (for example workflow_dispatch or schedule): there are no pull request
# labels to determine a version bump, so keep the current published version and publish
# nothing. For a module that has never been released this floors at 0.0.0.
[PSCustomObject]@{
ShouldPublish = $false
CreateRelease = $false
CreatePrerelease = $false
MajorRelease = $false
MinorRelease = $false
PatchRelease = $false
HasVersionBump = $false
PrereleaseName = ''
}
} else {
Resolve-ReleaseDecision -Configuration $config -PullRequest $pullRequest
}

$releases = Get-GitHubRelease
$ghVersion = Get-LatestGitHubVersion -Releases $releases
Expand Down
70 changes: 70 additions & 0 deletions tests/Resolve-PSModuleVersion.Helpers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,73 @@ Describe 'End-to-end: Resolve-ReleaseDecision + Get-NextModuleVersion' {
}
}
}

Describe 'Non-PR event keeps the current version' {
# main.ps1 builds this decision when the event has no pull_request (for example
# workflow_dispatch or schedule): no bump, no prerelease, no publish. Get-NextModuleVersion
# must then return the current published version unchanged, floored at 0.0.0 when nothing
# has ever been released.
BeforeAll {
Mock -CommandName Find-PSResource -ModuleName 'Resolve-PSModuleVersion.Helpers' -MockWith { @() }
$emptyReleases = @([PSCustomObject]@{ tagName = 'v0.0.0'; isLatest = $false; isPrerelease = $false })
$config = [PSCustomObject]@{
AutoPatching = $false
IncrementalPrerelease = $true
DatePrereleaseFormat = ''
VersionPrefix = 'v'
ReleaseType = 'None'
IgnoreLabels = @()
MajorLabels = @('major')
MinorLabels = @('minor')
PatchLabels = @('patch')
}
$noReleaseDecision = [PSCustomObject]@{
ShouldPublish = $false
CreateRelease = $false
CreatePrerelease = $false
MajorRelease = $false
MinorRelease = $false
PatchRelease = $false
HasVersionBump = $false
PrereleaseName = ''
}
}

It 'keeps the current version <LatestVersion> with no bump and no prerelease' -ForEach @(
@{ LatestVersion = '1.2.3' }
@{ LatestVersion = '0.0.0' }
) {
$latestVer = New-PSSemVer -Version $LatestVersion
$result = Get-NextModuleVersion -LatestVersion $latestVer -Decision $noReleaseDecision `
-Configuration $config -ModuleName 'TestModule' -Releases $emptyReleases
"$($result.Major).$($result.Minor).$($result.Patch)" | Should -Be $LatestVersion
$result.Prerelease | Should -BeNullOrEmpty
}
}

Describe 'Get-GitHubPullRequest' {
# Covers the non-PR branch: on an event without a pull_request (workflow_dispatch/schedule)
# the function must return $null so the caller keeps the current version, and it must still
# return the head ref + labels on a pull_request event.
AfterEach {
Remove-Item Env:PSMODULE_RESOLVE_PSMODULEVERSION_INPUT_EventJson -ErrorAction SilentlyContinue
}

It 'returns $null when the event has no pull_request (non-PR event)' {
$env:PSMODULE_RESOLVE_PSMODULEVERSION_INPUT_EventJson = '{ "action": "workflow_dispatch" }'
Get-GitHubPullRequest | Should -BeNullOrEmpty
}

It 'returns the head ref and labels for a pull_request event' {
$eventJson = @{
pull_request = @{
head = @{ ref = 'feat/example' }
labels = @(@{ name = 'patch' }, @{ name = 'prerelease' })
}
} | ConvertTo-Json -Depth 5
$env:PSMODULE_RESOLVE_PSMODULEVERSION_INPUT_EventJson = $eventJson
$result = Get-GitHubPullRequest
$result.HeadRef | Should -Be 'feat/example'
($result.Labels -join ',') | Should -Be 'patch,prerelease'
}
}
Loading