1 param( 2 $RepoOwner, 3 $RepoName, 4 $BranchPrefix, 5 $AuthToken 6 ) 7 8 . (Join-Path $PSScriptRoot common.ps1) 9 10 LogDebug "Operating on Repo [ $RepoName ]" 11 try{ 12 $branches = (Get-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix" -AuthToken $AuthToken).ref 13 } 14 catch { 15 LogError "Get-GitHubSourceReferences failed with exception:`n$_" 16 exit 1 17 } 18 19 foreach ($branch in $branches) 20 { 21 try { 22 $branchName = $branch.Replace("refs/heads/","") 23 $head = "${RepoOwner}/${RepoName}:${branchName}" 24 LogDebug "Operating on branch [ $branchName ]" 25 $pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken 26 } 27 catch 28 { 29 LogError "Get-GitHubPullRequests failed with exception:`n$_" 30 exit 1 31 } 32 33 $openPullRequests = $pullRequests | ? { $_.State -eq "open" } 34 35 if ($openPullRequests.Count -eq 0) 36 { 37 LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated open Pull Request. Deleting Branch" 38 try{ 39 Remove-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken 40 } 41 catch { 42 LogError "Remove-GitHubSourceReferences failed with exception:`n$_" 43 exit 1 44 } 45 } 46 }