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
5 changes: 5 additions & 0 deletions .github/actions/Cleanup-PSModulePrereleases/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
description: GitHub release tag to retain during cleanup.
required: false
default: ''
PullRequest:
description: Normalized pull request context JSON from Get-PSModuleSettings.
required: false
default: ''
WhatIf:
description: If specified, the action will only log the changes it would make.
required: false
Expand All @@ -33,5 +37,6 @@ runs:
env:
GH_TOKEN: ${{ env.GH_TOKEN }}
PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_WhatIf: ${{ inputs.WhatIf }}
PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_PullRequest: ${{ inputs.PullRequest }}
PSMODULE_CLEANUP_PSMODULEPRERELEASES_CONTEXT_ReleaseTag: ${{ inputs.ReleaseTag }}
run: ${{ github.action_path }}/src/cleanup.ps1
18 changes: 12 additions & 6 deletions .github/actions/Cleanup-PSModulePrereleases/src/cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ Import-Module -Name 'PSModule' -Force
LogGroup 'Load inputs' {
$whatIf = $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_WhatIf -eq 'true'

$githubEventJson = Get-Content -Raw $env:GITHUB_EVENT_PATH
$githubEvent = $githubEventJson | ConvertFrom-Json
$pull_request = $githubEvent.pull_request
if (-not $pull_request) {
throw 'GitHub event does not contain pull_request data. This script must be run from a pull_request event.'
$pullRequestJson = $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_PullRequest
$prHeadRef = if (-not [string]::IsNullOrWhiteSpace($pullRequestJson) -and $pullRequestJson -ne 'null') {
($pullRequestJson | ConvertFrom-Json).HeadRef
} else {
$githubEventJson = Get-Content -Raw $env:GITHUB_EVENT_PATH
$githubEvent = $githubEventJson | ConvertFrom-Json
$githubEvent.pull_request.head.ref
}

if ([string]::IsNullOrWhiteSpace($prHeadRef)) {
Write-Host '::notice::No pull request head ref is available. Nothing to cleanup.'
exit 0
}
$prHeadRef = $pull_request.head.ref
$prereleaseName = $prHeadRef -replace '[^a-zA-Z0-9]'

if ([string]::IsNullOrWhiteSpace($prereleaseName)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
function Resolve-WorkflowEventRouting {
<#
.SYNOPSIS
Resolves release and execution routing from normalized GitHub event state.
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory)]
[string] $EventName,

[Parameter()]
[string] $EventAction,

[Parameter()]
[bool] $PullRequestIsMerged,

[Parameter()]
[bool] $PullRequestIsClosed,

[Parameter()]
[bool] $IsTargetDefaultBranch,

[Parameter()]
[bool] $IsPushToDefaultBranch,

[Parameter()]
[bool] $IsManualDispatchToDefaultBranch,

[Parameter()]
[bool] $HasImportantChanges,

[Parameter()]
[bool] $HasPrereleaseLabel
)

$isPR = $EventName -eq 'pull_request'
$isPush = $EventName -eq 'push'
$isManualDispatch = $EventName -eq 'workflow_dispatch'
$isActivePR = $isPR -and -not $PullRequestIsClosed
$isOpenOrUpdatedPR = $isActivePR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled', 'unlabeled')
$isOpenOrLabeledPR = $isActivePR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled')
$isClosedPR = $isPR -and $EventAction -eq 'closed'
$isAbandonedPR = $isClosedPR -and -not $PullRequestIsMerged
$isMergedPR = $isClosedPR -and $PullRequestIsMerged
$shouldPrerelease = $isOpenOrLabeledPR -and $HasPrereleaseLabel -and $HasImportantChanges
$shouldRelease = (
($IsPushToDefaultBranch -or $IsManualDispatchToDefaultBranch) -and
$HasImportantChanges
)

[pscustomobject]@{
IsPR = $isPR
IsPush = $isPush
IsManualDispatch = $isManualDispatch
IsOpenOrUpdatedPR = $isOpenOrUpdatedPR
IsOpenOrLabeledPR = $isOpenOrLabeledPR
IsClosedPR = $isClosedPR
IsAbandonedPR = $isAbandonedPR
IsMergedPR = $isMergedPR
IsTargetDefaultBranch = $IsTargetDefaultBranch
IsPushToDefaultBranch = $IsPushToDefaultBranch
IsManualDispatchToDefaultBranch = $IsManualDispatchToDefaultBranch
ShouldPrerelease = $shouldPrerelease
ReleaseType = if ($shouldRelease) {
'Release'
} elseif ($shouldPrerelease) {
'Prerelease'
} else {
'None'
}
ShouldRunBuildTest = (
-not $isClosedPR -and
((-not $isPR) -or (-not $PullRequestIsClosed)) -and
$HasImportantChanges
)
ShouldCleanupEvent = $isClosedPR
ShouldRunCleanup = $isClosedPR -or $shouldRelease
}
}

function Select-PullRequestForPush {
<#
.SYNOPSIS
Selects the merged default-branch pull request associated with a pushed commit.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '',
Justification = 'Parameter is used inside a Sort-Object script block.')]
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter()]
[object[]] $PullRequest,

[Parameter(Mandatory)]
[string] $DefaultBranch,

[Parameter(Mandatory)]
[string] $CommitSha
)

$PullRequest |
Where-Object {
$_.Base.Ref -eq $DefaultBranch -and
-not [string]::IsNullOrWhiteSpace($_.merged_at) -and
$_.merge_commit_sha -eq $CommitSha
} |
Sort-Object -Property @{ Expression = { $_.merged_at }; Descending = $true } |
Select-Object -First 1
}

function Get-FilesFromGitTree {
<#
.SYNOPSIS
Returns the files contained in a complete Git tree response.
#>
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter(Mandatory)]
[PSCustomObject] $Tree
)

if ($Tree.truncated) {
throw 'Cannot determine changed files because the Git tree response was truncated.'
}

$Tree.tree |
Where-Object { $_.type -eq 'blob' } |
Select-Object -ExpandProperty path
}

function Get-FilesFromGitHubComparison {
<#
.SYNOPSIS
Returns files from a complete GitHub compare response.
#>
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter(Mandatory)]
[PSCustomObject] $Comparison
)

$files = @($Comparison.files | Where-Object { $null -ne $_ })
if ($files.Count -ge 300) {
throw 'Cannot determine changed files because the GitHub compare response reached its 300-file limit.'
}

$files | Select-Object -ExpandProperty filename
}
10 changes: 10 additions & 0 deletions .github/actions/Get-PSModuleSettings/src/Settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@
"description": "The type of release to create: Release (stable), Prerelease, or None."
}
}
},
"Site": {
"type": "object",
"description": "Documentation site publish configuration",
"properties": {
"Skip": {
"type": "boolean",
"description": "Skip publishing the generated documentation site"
}
}
}
}
},
Expand Down
Loading
Loading