diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 0ca7c55be..77cf7e506 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -9,7 +9,7 @@ param( LogDebug "Operating on Repo [ $RepoName ]" try{ - $branches = (Get-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix").ref + $branches = (Get-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix" -AuthToken $AuthToken).ref } catch { LogError "Get-GitHubSourceReferences failed with exception:`n$_" @@ -22,7 +22,7 @@ foreach ($branch in $branches) $branchName = $branch.Replace("refs/heads/","") $head = "${RepoOwner}/${RepoName}:${branchName}" LogDebug "Operating on branch [ $branchName ]" - $pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head + $pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken } catch { @@ -30,9 +30,9 @@ foreach ($branch in $branches) exit 1 } - if ($pullRequests.Count -eq 0) + if ($pullRequests -and (($pullRequests | ? { $_.State -eq "open" }).Count -eq 0)) { - LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated Pull Request. Deleting Branch" + 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 }