|
| 1 | +function Resolve-WorkflowEventRouting { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Resolves release and execution routing from normalized GitHub event state. |
| 5 | + #> |
| 6 | + [CmdletBinding()] |
| 7 | + [OutputType([PSCustomObject])] |
| 8 | + param( |
| 9 | + [Parameter(Mandatory)] |
| 10 | + [string] $EventName, |
| 11 | + |
| 12 | + [Parameter()] |
| 13 | + [string] $EventAction, |
| 14 | + |
| 15 | + [Parameter()] |
| 16 | + [bool] $PullRequestIsMerged, |
| 17 | + |
| 18 | + [Parameter()] |
| 19 | + [bool] $PullRequestIsClosed, |
| 20 | + |
| 21 | + [Parameter()] |
| 22 | + [bool] $IsTargetDefaultBranch, |
| 23 | + |
| 24 | + [Parameter()] |
| 25 | + [bool] $IsPushToDefaultBranch, |
| 26 | + |
| 27 | + [Parameter()] |
| 28 | + [bool] $IsManualDispatchToDefaultBranch, |
| 29 | + |
| 30 | + [Parameter()] |
| 31 | + [bool] $HasImportantChanges, |
| 32 | + |
| 33 | + [Parameter()] |
| 34 | + [bool] $HasPrereleaseLabel |
| 35 | + ) |
| 36 | + |
| 37 | + $isPR = $EventName -eq 'pull_request' |
| 38 | + $isPush = $EventName -eq 'push' |
| 39 | + $isManualDispatch = $EventName -eq 'workflow_dispatch' |
| 40 | + $isActivePR = $isPR -and -not $PullRequestIsClosed |
| 41 | + $isOpenOrUpdatedPR = $isActivePR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled', 'unlabeled') |
| 42 | + $isOpenOrLabeledPR = $isActivePR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled') |
| 43 | + $isClosedPR = $isPR -and $EventAction -eq 'closed' |
| 44 | + $isAbandonedPR = $isClosedPR -and -not $PullRequestIsMerged |
| 45 | + $isMergedPR = $isClosedPR -and $PullRequestIsMerged |
| 46 | + $shouldPrerelease = $isOpenOrLabeledPR -and $HasPrereleaseLabel -and $HasImportantChanges |
| 47 | + $shouldRelease = ( |
| 48 | + ($IsPushToDefaultBranch -or $IsManualDispatchToDefaultBranch) -and |
| 49 | + $HasImportantChanges |
| 50 | + ) |
| 51 | + |
| 52 | + [pscustomobject]@{ |
| 53 | + IsPR = $isPR |
| 54 | + IsPush = $isPush |
| 55 | + IsManualDispatch = $isManualDispatch |
| 56 | + IsOpenOrUpdatedPR = $isOpenOrUpdatedPR |
| 57 | + IsOpenOrLabeledPR = $isOpenOrLabeledPR |
| 58 | + IsClosedPR = $isClosedPR |
| 59 | + IsAbandonedPR = $isAbandonedPR |
| 60 | + IsMergedPR = $isMergedPR |
| 61 | + IsTargetDefaultBranch = $IsTargetDefaultBranch |
| 62 | + IsPushToDefaultBranch = $IsPushToDefaultBranch |
| 63 | + IsManualDispatchToDefaultBranch = $IsManualDispatchToDefaultBranch |
| 64 | + ShouldPrerelease = $shouldPrerelease |
| 65 | + ReleaseType = if ($shouldRelease) { |
| 66 | + 'Release' |
| 67 | + } elseif ($shouldPrerelease) { |
| 68 | + 'Prerelease' |
| 69 | + } else { |
| 70 | + 'None' |
| 71 | + } |
| 72 | + ShouldRunBuildTest = ( |
| 73 | + -not $isClosedPR -and |
| 74 | + ((-not $isPR) -or (-not $PullRequestIsClosed)) -and |
| 75 | + $HasImportantChanges |
| 76 | + ) |
| 77 | + ShouldCleanupEvent = $isClosedPR |
| 78 | + ShouldRunCleanup = $isClosedPR -or $shouldRelease |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function Select-PullRequestForPush { |
| 83 | + <# |
| 84 | + .SYNOPSIS |
| 85 | + Selects the merged default-branch pull request associated with a pushed commit. |
| 86 | + #> |
| 87 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', |
| 88 | + Justification = 'Parameter is used inside a Sort-Object script block.')] |
| 89 | + [CmdletBinding()] |
| 90 | + [OutputType([PSCustomObject])] |
| 91 | + param( |
| 92 | + [Parameter()] |
| 93 | + [object[]] $PullRequest, |
| 94 | + |
| 95 | + [Parameter(Mandatory)] |
| 96 | + [string] $DefaultBranch, |
| 97 | + |
| 98 | + [Parameter(Mandatory)] |
| 99 | + [string] $CommitSha |
| 100 | + ) |
| 101 | + |
| 102 | + $PullRequest | |
| 103 | + Where-Object { |
| 104 | + $_.Base.Ref -eq $DefaultBranch -and |
| 105 | + -not [string]::IsNullOrWhiteSpace($_.merged_at) -and |
| 106 | + $_.merge_commit_sha -eq $CommitSha |
| 107 | + } | |
| 108 | + Sort-Object -Property @{ Expression = { $_.merged_at }; Descending = $true } | |
| 109 | + Select-Object -First 1 |
| 110 | +} |
| 111 | + |
| 112 | +function Get-FilesFromGitTree { |
| 113 | + <# |
| 114 | + .SYNOPSIS |
| 115 | + Returns the files contained in a complete Git tree response. |
| 116 | + #> |
| 117 | + [CmdletBinding()] |
| 118 | + [OutputType([string])] |
| 119 | + param( |
| 120 | + [Parameter(Mandatory)] |
| 121 | + [PSCustomObject] $Tree |
| 122 | + ) |
| 123 | + |
| 124 | + if ($Tree.truncated) { |
| 125 | + throw 'Cannot determine changed files because the Git tree response was truncated.' |
| 126 | + } |
| 127 | + |
| 128 | + $Tree.tree | |
| 129 | + Where-Object { $_.type -eq 'blob' } | |
| 130 | + Select-Object -ExpandProperty path |
| 131 | +} |
| 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 | +} |
0 commit comments