Skip to content

Commit

Permalink
Add the ability to check for open pull request to a different repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Jun 3, 2021
1 parent 9445bab commit e8e3911
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
param(
[Parameter(Mandatory = $true)]
$RepoOwner,
# Use this if a pull request might have been opened from one repo against another.
# E.g Pull request opened from azure-sdk/azure-sdk prBranch --> Azure/azure-sdk baseBranch
[Parameter(Mandatory = $true)]
$ForkRepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$BranchPrefix,
[Parameter(Mandatory = $true)]
$AuthToken
)

Expand All @@ -23,6 +31,11 @@ foreach ($branch in $branches)
$head = "${RepoOwner}/${RepoName}:${branchName}"
LogDebug "Operating on branch [ $branchName ]"
$pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken

if ($ForkRepoOwner)
{
$pullRequests += Get-GitHubPullRequests -RepoOwner $ForkRepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken
}
}
catch
{
Expand All @@ -31,16 +44,19 @@ foreach ($branch in $branches)
}

$openPullRequests = $pullRequests | ? { $_.State -eq "open" }

if ($openPullRequests.Count -eq 0)
if ($openPullRequests.Count -gt 0)
{
LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated open Pull Request. Deleting Branch"
try{
Remove-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken
}
catch {
LogError "Remove-GitHubSourceReferences failed with exception:`n$_"
exit 1
}
LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has open pull Requests. Skipping"
LogDebug $openPullRequests.url
continue
}

LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated open Pull Request. Deleting Branch"
try{
Remove-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken
}
catch {
LogError "Remove-GitHubSourceReferences failed with exception:`n$_"
exit 1
}
}

0 comments on commit e8e3911

Please sign in to comment.