azure-sdk-for-cpp/eng/common/scripts/Delete-RemoteBranches.ps1
Azure SDK Bot e2bfa85d8b
Sync eng/common directory with azure-sdk-tools for PR 1203 (#999)
* Refactoring artifact-metadata-parsing.ps1, update-docs-metadata.ps1, and create-tags-and-git-release.ps1

* Clean up common imports

* Refactor Update-docs-ci.ps1

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>
2020-11-18 21:50:29 -08:00

46 lines
1.3 KiB
PowerShell

param(
$RepoOwner,
$RepoName,
$BranchPrefix,
$AuthToken
)
. (Join-Path $PSScriptRoot common.ps1)
LogDebug "Operating on Repo [ $RepoName ]"
try{
$branches = (Get-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix" -AuthToken $AuthToken).ref
}
catch {
LogError "Get-GitHubSourceReferences failed with exception:`n$_"
exit 1
}
foreach ($branch in $branches)
{
try {
$branchName = $branch.Replace("refs/heads/","")
$head = "${RepoOwner}/${RepoName}:${branchName}"
LogDebug "Operating on branch [ $branchName ]"
$pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken
}
catch
{
LogError "Get-GitHubPullRequests failed with exception:`n$_"
exit 1
}
$openPullRequests = $pullRequests | ? { $_.State -eq "open" }
if ($openPullRequests.Count -eq 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
}
}
}