Skip to content

Commit 98ce7ab

Browse files
🚀 [Feature]: Add target branch detection to prevent releases from non-default branch merges (#7)
The action now detects whether a pull request targets the repository's default branch and uses this to determine the correct release type. Releases and site deployments are only triggered when PRs are merged into the default branch, preventing accidental releases from feature branch merges. ## Target branch detection Added detection logic to identify the PR's target branch and compare it against the repository's default branch: - `targetBranch` - Extracted from the PR's base ref (`pullRequest.Base.Ref`) - `defaultBranch` - Retrieved from repository info (`eventData.Repository.default_branch`) - `isTargetDefaultBranch` - Boolean flag indicating whether the PR targets the default branch ## ReleaseType logic update The `ReleaseType` calculation now factors in the target branch: | Condition | ReleaseType | |-----------|-------------| | PR merged to **default** branch | `Release` | | PR merged to **non-default** branch | `None` | | PR closed without merge | `Cleanup` | | Open PR with prerelease label | `Prerelease` | | Everything else | `None` | This ensures that PRs merged into feature branches or other non-default branches do not trigger releases. ## Site publishing update The `PublishSite` condition now also requires the PR to be merged into the default branch (`$isMergedPR -and $isTargetDefaultBranch`), ensuring sites are only deployed when merging to the main branch. ## Code simplification Simplified the event data extraction by removing defensive null-checking in favor of relying on `Get-GitHubEventData` to provide valid data. The action now uses direct property access for cleaner, more readable code. ## Logging improvements The GitHub event inputs logging now includes branch information for debugging: ``` TargetBranch : main DefaultBranch : main IsTargetDefaultBranch : True ```
1 parent 3b93ae6 commit 98ce7ab

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

‎scripts/main.ps1‎

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -195,47 +195,27 @@ $settings | Add-Member -MemberType NoteProperty -Name WorkingDirectory -Value $w
195195

196196
# Calculate job run conditions
197197
LogGroup 'Calculate Job Run Conditions:' {
198-
# Common conditions
199-
$eventData = $null
200-
try {
201-
$eventData = Get-GitHubEventData -ErrorAction Stop
202-
} catch {
203-
if (-not [string]::IsNullOrEmpty($env:GITHUB_EVENT_PATH) -and (Test-Path -Path $env:GITHUB_EVENT_PATH)) {
204-
$eventData = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
205-
}
206-
}
198+
$eventData = Get-GitHubEventData -ErrorAction Stop
207199

208200
LogGroup 'GitHub Event Data' {
209-
if ($null -ne $eventData) {
210-
Write-Host ($eventData | ConvertTo-Json -Depth 10 | Out-String)
211-
} else {
212-
Write-Host 'No event data available.'
213-
}
214-
}
215-
216-
$pullRequestAction = if ($null -ne $eventData.Action) {
217-
$eventData.Action
218-
} else {
219-
$env:GITHUB_EVENT_ACTION
220-
}
221-
222-
$pullRequest = if ($null -ne $eventData.PullRequest) {
223-
$eventData.PullRequest
224-
} else {
225-
$null
201+
$eventData | ConvertTo-Json -Depth 10 | Out-String
226202
}
227203

228-
$pullRequestIsMerged = if ($null -ne $pullRequest -and $null -ne $pullRequest.Merged) {
229-
[bool]$pullRequest.Merged
230-
} else {
231-
$false
232-
}
204+
$pullRequestAction = $eventData.Action
205+
$pullRequest = $eventData.PullRequest
206+
$pullRequestIsMerged = $pullRequest.Merged
207+
$targetBranch = $pullRequest.Base.Ref
208+
$defaultBranch = $eventData.Repository.default_branch
209+
$isTargetDefaultBranch = $targetBranch -eq $defaultBranch
233210

234211
Write-Host 'GitHub event inputs:'
235212
[pscustomobject]@{
236213
GITHUB_EVENT_NAME = $env:GITHUB_EVENT_NAME
237214
GITHUB_EVENT_ACTION = $pullRequestAction
238215
GITHUB_EVENT_PULL_REQUEST_MERGED = $pullRequestIsMerged
216+
TargetBranch = $targetBranch
217+
DefaultBranch = $defaultBranch
218+
IsTargetDefaultBranch = $isTargetDefaultBranch
239219
} | Format-List | Out-String
240220

241221
$isPR = $env:GITHUB_EVENT_NAME -eq 'pull_request'
@@ -254,10 +234,13 @@ LogGroup 'Calculate Job Run Conditions:' {
254234

255235
# Determine ReleaseType - single source of truth for what Publish-PSModule should do
256236
# Values: 'Release', 'Prerelease', 'Cleanup', 'None'
237+
# Release only happens when PR is merged into the default branch
257238
$releaseType = if (-not $isPR) {
258239
'None'
259-
} elseif ($isMergedPR) {
240+
} elseif ($isMergedPR -and $isTargetDefaultBranch) {
260241
'Release'
242+
} elseif ($isMergedPR -and -not $isTargetDefaultBranch) {
243+
'None' # Merged to non-default branch - no release
261244
} elseif ($isAbandonedPR) {
262245
'Cleanup'
263246
} elseif ($shouldPrerelease) {
@@ -267,13 +250,14 @@ LogGroup 'Calculate Job Run Conditions:' {
267250
}
268251

269252
[pscustomobject]@{
270-
isPR = $isPR
271-
isOpenOrUpdatedPR = $isOpenOrUpdatedPR
272-
isAbandonedPR = $isAbandonedPR
273-
isMergedPR = $isMergedPR
274-
isNotAbandonedPR = $isNotAbandonedPR
275-
shouldPrerelease = $shouldPrerelease
276-
ReleaseType = $releaseType
253+
isPR = $isPR
254+
isOpenOrUpdatedPR = $isOpenOrUpdatedPR
255+
isAbandonedPR = $isAbandonedPR
256+
isMergedPR = $isMergedPR
257+
isNotAbandonedPR = $isNotAbandonedPR
258+
isTargetDefaultBranch = $isTargetDefaultBranch
259+
shouldPrerelease = $shouldPrerelease
260+
ReleaseType = $releaseType
277261
} | Format-List | Out-String
278262
}
279263

@@ -467,7 +451,7 @@ LogGroup 'Calculate Job Run Conditions:' {
467451
ReleaseType = $releaseType # 'Release', 'Prerelease', 'Cleanup', or 'None'
468452
BuildDocs = $isNotAbandonedPR -and (-not $settings.Build.Docs.Skip)
469453
BuildSite = $isNotAbandonedPR -and (-not $settings.Build.Site.Skip)
470-
PublishSite = $isMergedPR
454+
PublishSite = $isMergedPR -and $isTargetDefaultBranch
471455
}
472456
$settings | Add-Member -MemberType NoteProperty -Name Run -Value $run
473457

0 commit comments

Comments
 (0)