Skip to content

Commit 8a09377

Browse files
azure-sdkweshaggardbenbp
authored
Sync eng/common directory with azure-sdk-tools for PR 7181 (Azure#27547)
Sync eng/common directory with azure-sdk-tools for PR Azure/azure-sdk-tools#7181 See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/README.md#workflow) --------- Co-authored-by: Wes Haggard <[email protected]> Co-authored-by: Wes Haggard <[email protected]> Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent 8c2c48a commit 8a09377

File tree

1 file changed

+81
-88
lines changed

1 file changed

+81
-88
lines changed
Lines changed: 81 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[CmdletBinding(SupportsShouldProcess)]
22
param(
33
# Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
4-
$RepoId = "$RepoOwner/$RepoName",
5-
# Upstream repo to check and see if there are existing open PRs from before deleting branch
6-
$UpstreamRepoId,
4+
$RepoId,
75
# CentralRepoId the original PR to generate sync PR. E.g Azure/azure-sdk-tools for eng/common
86
$CentralRepoId,
97
# 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.
@@ -14,126 +12,121 @@ param(
1412
[AllowNull()]
1513
[DateTime]$LastCommitOlderThan,
1614
[Switch]$DeleteBranchesEvenIfThereIsOpenPR = $false,
17-
[Parameter(Mandatory = $true)]
1815
$AuthToken
1916
)
2017
Set-StrictMode -version 3
2118

2219
. (Join-Path $PSScriptRoot common.ps1)
2320

24-
LogDebug "Operating on Repo [ $RepoId ]"
21+
function Get-AllBranchesAndPullRequestInfo($owner, $repo) {
22+
$query = @'
23+
query($owner: String!, $repo: String!, $refPrefix: String!$endCursor: String) {
24+
repository(owner: $owner, name: $repo) {
25+
refs(first: 100, refPrefix: $refPrefix, after: $endCursor) {
26+
nodes {
27+
name
28+
target {
29+
commitUrl
30+
... on Commit {
31+
committedDate
32+
}
33+
}
34+
associatedPullRequests(first: 100) {
35+
nodes {
36+
url
37+
closed
38+
}
39+
}
40+
}
41+
pageInfo {
42+
hasNextPage
43+
endCursor
44+
}
45+
}
46+
}
47+
}
48+
'@
2549

26-
try{
27-
# pull all branches.
28-
$responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads" -AuthToken $AuthToken
50+
$all_branches = gh api graphql --paginate -F owner=$owner -F repo=$repo -F refPrefix='refs/heads/' -f query=$query `
51+
--jq '.data.repository.refs.nodes[] | { name, commitUrl: .target.commitUrl, committedDate: .target.committedDate, pullRequests: .associatedPullRequests.nodes }' | ConvertFrom-Json
52+
53+
if ($LASTEXITCODE) {
54+
LogError "Failed to retrieve branches for '$owner' and '$repo' running query '$query'"
55+
exit $LASTEXITCODE
56+
}
57+
58+
return $all_branches
2959
}
30-
catch {
31-
LogError "Get-GitHubSourceReferences failed with exception:`n$_"
32-
exit 1
60+
61+
LogDebug "Operating on Repo '$RepoId'"
62+
63+
# Setup GH_TOKEN for the gh cli commands
64+
if ($AuthToken) {
65+
$env:GH_TOKEN = $AuthToken
3366
}
3467

35-
foreach ($res in $responses)
68+
$owner, $repo = $RepoId -split "/"
69+
$branches = Get-AllBranchesAndPullRequestInfo $owner $repo
70+
71+
foreach ($branch in $branches)
3672
{
37-
if (!$res -or !$res.ref) {
38-
LogDebug "No branch returned from the branch prefix $BranchRegex on $Repo. Skipping..."
39-
continue
40-
}
41-
$branch = $res.ref
42-
$branchName = $branch.Replace("refs/heads/","")
43-
if (!($branchName -match $BranchRegex)) {
73+
$branchName = $branch.Name
74+
if ($branchName -notmatch $BranchRegex) {
4475
continue
4576
}
77+
$openPullRequests = @($branch.pullRequests | Where-Object { !$_.Closed })
4678

47-
# If we have a central PR that created this branch still open still don't delete the branch
79+
# If we have a central PR that created this branch still open don't delete the branch
4880
if ($CentralRepoId)
4981
{
50-
$pullRequestNumber = $Matches["PrNumber"]
51-
# If central PR number found, then skip
82+
$pullRequestNumber = $matches["PrNumber"]
83+
# If central PR number is not found, then skip
5284
if (!$pullRequestNumber) {
53-
LogError "No PR number found in the branch name. Please check the branch name [ $branchName ]. Skipping..."
85+
LogError "No PR number found in the branch name. Please check the branch name '$branchName'. Skipping..."
5486
continue
5587
}
5688

57-
try {
58-
$centralPR = Get-GitHubPullRequest -RepoId $CentralRepoId -PullRequestNumber $pullRequestNumber -AuthToken $AuthToken
59-
LogDebug "Found central PR pull request: $($centralPR.html_url)"
60-
if ($centralPR.state -ne "closed") {
61-
# Skipping if there open central PR number for the branch.
62-
continue
89+
$centralPR = gh pr view --json 'url,closed' --repo $CentralRepoId $pullRequestNumber | ConvertFrom-Json
90+
if ($LASTEXITCODE) {
91+
LogError "PR '$pullRequestNumber' not found in repo '$CentralRepoId'. Skipping..."
92+
continue;
93+
} else {
94+
LogDebug "Found central PR $($centralPR.url) and Closed=$($centralPR.closed)"
95+
if (!$centralPR.Closed) {
96+
# Skipping if there is an open central PR open for the branch.
97+
LogDebug "Central PR is still open so skipping the deletion of branch '$branchName'. Skipping..."
98+
continue;
6399
}
64100
}
65-
catch
66-
{
67-
# If there is no central PR for the PR number, log error and skip.
68-
LogError "Get-GitHubPullRequests failed with exception:`n$_"
69-
LogError "Not found PR number [ $pullRequestNumber ] from [ $CentralRepoId ]. Skipping..."
70-
continue
71-
}
72101
}
73-
74-
# If this branch has an open PR in the repo or the upstream repo then don't delete
75-
try
76-
{
77-
$head = "${RepoId}:${branchName}"
78-
LogDebug "Operating on branch [ $branchName ]"
79-
$pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken
80-
81-
# check to see if there are any PR's open in the main central repo as well.
82-
if ($UpstreamRepoId) {
83-
$pullRequests += Get-GitHubPullRequests -RepoId $UpstreamRepoId -State "all" -Head $head -AuthToken $AuthToken
102+
else {
103+
# Not CentralRepoId - not associated with a central repo PR
104+
if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) {
105+
LogDebug "Found open PRs associate with branch '$branchName'. Skipping..."
106+
continue
84107
}
85108
}
86-
catch
87-
{
88-
LogError "Get-GitHubPullRequests failed with exception:`n$_"
89-
exit 1
90-
}
91-
$openPullRequests = @($pullRequests | Where-Object { $_.State -eq "open" })
92-
93-
if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) {
94-
LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
95-
continue
96-
}
97109

98-
# If there is date filter, then check if branch last commit older than the date.
110+
# If there is date filter, then check if branch last commit is older than the date.
99111
if ($LastCommitOlderThan)
100112
{
101-
if (!$res.object -or !$res.object.url) {
102-
LogWarning "No commit url returned from response. Skipping... "
113+
$commitDate = $branch.committedDate
114+
if ($commitDate -gt $LastCommitOlderThan) {
115+
LogDebug "The branch $branch last commit date '$commitDate' is newer than the date '$LastCommitOlderThan'. Skipping..."
103116
continue
104117
}
105-
try {
106-
$commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken
107-
if (!$commitDate) {
108-
LogDebug "No last commit date found. Skipping."
109-
continue
110-
}
111-
if ($commitDate -gt $LastCommitOlderThan) {
112-
LogDebug "The branch $branch last commit date [ $commitDate ] is newer than the date $LastCommitOlderThan. Skipping."
113-
continue
114-
}
115-
116-
LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has a last commit date [ $commitDate ] that is older than $LastCommitOlderThan. "
117-
}
118-
catch {
119-
LogError "Get-GithubReferenceCommitDate failed with exception:`n$_"
120-
exit 1
121-
}
122118
}
123119

124120
foreach ($openPullRequest in $openPullRequests) {
125-
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
121+
Write-Host "Note: Open pull Request '$($openPullRequest.url)' will be closed after branch deletion, given the central PR is closed."
126122
}
127123

128-
try
129-
{
130-
if ($PSCmdlet.ShouldProcess("[ $branchName ] in [ $RepoId ]", "Deleting branches on cleanup script")) {
131-
Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
132-
Write-Host "The branch [ $branchName ] with sha [ $($res.object.sha) ] in [ $RepoId ] has been deleted."
124+
$commitUrl = $branch.commitUrl
125+
if ($PSCmdlet.ShouldProcess("'$branchName' in '$RepoId'", "Deleting branch on cleanup script")) {
126+
gh api "repos/${RepoId}/git/refs/heads/${branchName}" -X DELETE
127+
if ($LASTEXITCODE) {
128+
LogError "Deletion of branch '$branchName` failed"
133129
}
134-
}
135-
catch {
136-
LogError "Remove-GitHubSourceReferences failed with exception:`n$_"
137-
exit 1
130+
Write-Host "The branch '$branchName' at commit '$commitUrl' in '$RepoId' has been deleted."
138131
}
139132
}

0 commit comments

Comments
 (0)