Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to check for open pull request to a different repo. #1658

Merged
1 commit merged into from
Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
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
$ForkRepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$BranchPrefix,
[Parameter(Mandatory = $true)]
$AuthToken
)

Expand All @@ -23,6 +30,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 +43,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"
chidozieononiwu marked this conversation as resolved.
Show resolved Hide resolved
LogDebug $openPullRequests.url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correctly output if the pull requests list is more then one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that works

continue
}

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