azure-sdk-for-cpp/eng/common/scripts/Delete-RemoteBranches.ps1
Azure SDK Bot 71416d73b1
Sync eng/common directory with azure-sdk-tools for PR 7181 (#5057)
* Update branch deletion code

- Update to use the gh cli for github operations
- Switch to using graphql for pulling branches as the rest api
  starts to fail after a certain number of branches and we can
  also pull back all the associated data like pull request info
  and commit date all in one query.
- switc to running on ubuntu 1ES pool

Improve query error handling

Fix condition

Fix slash escaping on the command line

clean-up pipeline

* Update eng/common/scripts/Delete-RemoteBranches.ps1

Co-authored-by: Ben Broderick Phillips <ben@benbp.net>

* Update eng/common/scripts/Delete-RemoteBranches.ps1

Co-authored-by: Ben Broderick Phillips <ben@benbp.net>

---------

Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com>
Co-authored-by: Ben Broderick Phillips <ben@benbp.net>
2023-10-27 13:48:04 -07:00

133 lines
4.6 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess)]
param(
# Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
$RepoId,
# CentralRepoId the original PR to generate sync PR. E.g Azure/azure-sdk-tools for eng/common
$CentralRepoId,
# We start from the sync PRs, use the branch name to get the PR number of central repo. E.g. sync-eng/common-(<branchName>)-(<PrNumber>). Have group name on PR number.
# For sync-eng/common work, we use regex as "^sync-eng/common.*-(?<PrNumber>\d+).*$".
# For sync-.github/workflows work, we use regex as "^sync-.github/workflows.*-(?<PrNumber>\d+).*$".
$BranchRegex,
# Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format.
[AllowNull()]
[DateTime]$LastCommitOlderThan,
[Switch]$DeleteBranchesEvenIfThereIsOpenPR = $false,
$AuthToken
)
Set-StrictMode -version 3
. (Join-Path $PSScriptRoot common.ps1)
function Get-AllBranchesAndPullRequestInfo($owner, $repo) {
$query = @'
query($owner: String!, $repo: String!, $refPrefix: String!$endCursor: String) {
repository(owner: $owner, name: $repo) {
refs(first: 100, refPrefix: $refPrefix, after: $endCursor) {
nodes {
name
target {
commitUrl
... on Commit {
committedDate
}
}
associatedPullRequests(first: 100) {
nodes {
url
closed
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
'@
$all_branches = gh api graphql --paginate -F owner=$owner -F repo=$repo -F refPrefix='refs/heads/' -f query=$query `
--jq '.data.repository.refs.nodes[] | { name, commitUrl: .target.commitUrl, committedDate: .target.committedDate, pullRequests: .associatedPullRequests.nodes }' | ConvertFrom-Json
if ($LASTEXITCODE) {
LogError "Failed to retrieve branches for '$owner' and '$repo' running query '$query'"
exit $LASTEXITCODE
}
return $all_branches
}
LogDebug "Operating on Repo '$RepoId'"
# Setup GH_TOKEN for the gh cli commands
if ($AuthToken) {
$env:GH_TOKEN = $AuthToken
}
$owner, $repo = $RepoId -split "/"
$branches = Get-AllBranchesAndPullRequestInfo $owner $repo
foreach ($branch in $branches)
{
$branchName = $branch.Name
if ($branchName -notmatch $BranchRegex) {
continue
}
$openPullRequests = @($branch.pullRequests | Where-Object { !$_.Closed })
# If we have a central PR that created this branch still open don't delete the branch
if ($CentralRepoId)
{
$pullRequestNumber = $matches["PrNumber"]
# If central PR number is not found, then skip
if (!$pullRequestNumber) {
LogError "No PR number found in the branch name. Please check the branch name '$branchName'. Skipping..."
continue
}
$centralPR = gh pr view --json 'url,closed' --repo $CentralRepoId $pullRequestNumber | ConvertFrom-Json
if ($LASTEXITCODE) {
LogError "PR '$pullRequestNumber' not found in repo '$CentralRepoId'. Skipping..."
continue;
} else {
LogDebug "Found central PR $($centralPR.url) and Closed=$($centralPR.closed)"
if (!$centralPR.Closed) {
# Skipping if there is an open central PR open for the branch.
LogDebug "Central PR is still open so skipping the deletion of branch '$branchName'. Skipping..."
continue;
}
}
}
else {
# Not CentralRepoId - not associated with a central repo PR
if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) {
LogDebug "Found open PRs associate with branch '$branchName'. Skipping..."
continue
}
}
# If there is date filter, then check if branch last commit is older than the date.
if ($LastCommitOlderThan)
{
$commitDate = $branch.committedDate
if ($commitDate -gt $LastCommitOlderThan) {
LogDebug "The branch $branch last commit date '$commitDate' is newer than the date '$LastCommitOlderThan'. Skipping..."
continue
}
}
foreach ($openPullRequest in $openPullRequests) {
Write-Host "Note: Open pull Request '$($openPullRequest.url)' will be closed after branch deletion, given the central PR is closed."
}
$commitUrl = $branch.commitUrl
if ($PSCmdlet.ShouldProcess("'$branchName' in '$RepoId'", "Deleting branch on cleanup script")) {
gh api "repos/${RepoId}/git/refs/heads/${branchName}" -X DELETE
if ($LASTEXITCODE) {
LogError "Deletion of branch '$branchName` failed"
}
Write-Host "The branch '$branchName' at commit '$commitUrl' in '$RepoId' has been deleted."
}
}