Skip to content

Commit 6aac86d

Browse files
Guard closed pull request label events
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 5f316b8 commit 6aac86d

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function Resolve-WorkflowEventRouting {
1515
[Parameter()]
1616
[bool] $PullRequestIsMerged,
1717

18+
[Parameter()]
19+
[bool] $PullRequestIsClosed,
20+
1821
[Parameter()]
1922
[bool] $IsTargetDefaultBranch,
2023

@@ -34,8 +37,9 @@ function Resolve-WorkflowEventRouting {
3437
$isPR = $EventName -eq 'pull_request'
3538
$isPush = $EventName -eq 'push'
3639
$isManualDispatch = $EventName -eq 'workflow_dispatch'
37-
$isOpenOrUpdatedPR = $isPR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled', 'unlabeled')
38-
$isOpenOrLabeledPR = $isPR -and $EventAction -in @('opened', 'reopened', 'synchronize', 'labeled')
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')
3943
$isClosedPR = $isPR -and $EventAction -eq 'closed'
4044
$isAbandonedPR = $isClosedPR -and -not $PullRequestIsMerged
4145
$isMergedPR = $isClosedPR -and $PullRequestIsMerged
@@ -65,7 +69,11 @@ function Resolve-WorkflowEventRouting {
6569
} else {
6670
'None'
6771
}
68-
ShouldRunBuildTest = (-not $isClosedPR) -and $HasImportantChanges
72+
ShouldRunBuildTest = (
73+
-not $isClosedPR -and
74+
((-not $isPR) -or (-not $PullRequestIsClosed)) -and
75+
$HasImportantChanges
76+
)
6977
ShouldCleanupEvent = $isClosedPR
7078
}
7179
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ LogGroup 'Calculate Job Run Conditions:' {
267267
} else {
268268
-not [string]::IsNullOrWhiteSpace($pullRequest.merged_at)
269269
}
270+
$pullRequestIsClosed = $null -ne $pullRequest -and $pullRequest.State -eq 'closed'
270271
$targetBranch = if ($pullRequest) { $pullRequest.Base.Ref } elseif ($isPush) { $pushBranch } else { $workflowRef }
271272
$isTargetDefaultBranch = $targetBranch -eq $defaultBranch
272273
$pullRequestContext = if ($pullRequest) {
@@ -278,6 +279,7 @@ LogGroup 'Calculate Job Run Conditions:' {
278279
BaseRef = $pullRequest.Base.Ref
279280
Labels = @($pullRequest.Labels.Name)
280281
Merged = $pullRequestIsMerged
282+
Closed = $pullRequestIsClosed
281283
MergeCommitSha = $pullRequest.merge_commit_sha
282284
HtmlUrl = $pullRequest.html_url
283285
}
@@ -301,6 +303,7 @@ LogGroup 'Calculate Job Run Conditions:' {
301303
GITHUB_EVENT_NAME = $eventName
302304
GITHUB_EVENT_ACTION = $pullRequestAction
303305
GITHUB_EVENT_PULL_REQUEST_MERGED = $pullRequestIsMerged
306+
GITHUB_EVENT_PULL_REQUEST_CLOSED = $pullRequestIsClosed
304307
CommitSha = $commitSha
305308
PushBranch = $pushBranch
306309
TargetBranch = $targetBranch
@@ -439,6 +442,7 @@ If you believe this is incorrect, please verify that your changes are in the cor
439442
$routing = Resolve-WorkflowEventRouting -EventName $eventName `
440443
-EventAction $pullRequestAction `
441444
-PullRequestIsMerged $pullRequestIsMerged `
445+
-PullRequestIsClosed $pullRequestIsClosed `
442446
-IsTargetDefaultBranch $isTargetDefaultBranch `
443447
-IsPushToDefaultBranch $isPushToDefaultBranch `
444448
-IsManualDispatchToDefaultBranch $isManualDispatchToDefaultBranch `

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ Describe 'Resolve-WorkflowEventRouting' {
6363
$result.ShouldRunBuildTest | Should -BeFalse
6464
$result.ShouldCleanupEvent | Should -BeTrue
6565
}
66+
67+
It 'does not run a label event for a closed PR' {
68+
$result = Resolve-WorkflowEventRouting -EventName pull_request `
69+
-EventAction labeled `
70+
-PullRequestIsClosed $true `
71+
-IsTargetDefaultBranch $true `
72+
-HasImportantChanges $true `
73+
-HasPrereleaseLabel $true
74+
75+
$result.ReleaseType | Should -Be 'None'
76+
$result.IsOpenOrUpdatedPR | Should -BeFalse
77+
$result.ShouldRunBuildTest | Should -BeFalse
78+
$result.ShouldCleanupEvent | Should -BeFalse
79+
}
6680
}
6781

6882
Describe 'Select-PullRequestForPush' {

0 commit comments

Comments
 (0)