Skip to content

Commit 093fe55

Browse files
azure-sdksima-zhuweshaggard
authored
Sync eng/common directory with azure-sdk-tools for PR 3342 (#18211)
* Delete PR and branch which central PR is closed * more logging changes * resume the delete operations. * Change the pr link directly * fix the regex * Refactor on regex name * change the function to inline logic * change typo * delete on branch * make changes on comments * add commnets * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> Co-authored-by: sima-zhu <sizhu@microsoft.com> Co-authored-by: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com>
1 parent a027916 commit 093fe55

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

eng/common/scripts/Delete-RemoteBranches.ps1

+54-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
[CmdletBinding(SupportsShouldProcess)]
12
param(
23
# The repo owner: e.g. Azure
34
$RepoOwner,
45
# The repo name. E.g. azure-sdk-for-java
56
$RepoName,
67
# Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
78
$RepoId="$RepoOwner/$RepoName",
8-
[Parameter(Mandatory = $true)]
9-
$BranchPrefix,
9+
# CentralRepoId the original PR to generate sync PR. E.g Azure/azure-sdk-tools for eng/common
10+
$CentralRepoId,
11+
# 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.
12+
# For sync-eng/common work, we use regex as "^sync-eng/common.*-(?<PrNumber>\d+).*$".
13+
$BranchRegex,
1014
# Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format.
1115
[AllowNull()]
1216
[DateTime]$LastCommitOlderThan,
@@ -19,7 +23,8 @@ param(
1923
LogDebug "Operating on Repo [ $RepoId ]"
2024

2125
try{
22-
$responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads/$BranchPrefix" -AuthToken $AuthToken
26+
# pull all branches.
27+
$responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads" -AuthToken $AuthToken
2328
}
2429
catch {
2530
LogError "Get-GitHubSourceReferences failed with exception:`n$_"
@@ -29,11 +34,16 @@ catch {
2934
foreach ($res in $responses)
3035
{
3136
if (!$res -or !$res.ref) {
32-
LogDebug "No branch returned from the branch prefix $BranchPrefix on $Repo. Skipping..."
37+
LogDebug "No branch returned from the branch prefix $BranchRegex on $Repo. Skipping..."
3338
continue
3439
}
3540
$branch = $res.ref
3641
$branchName = $branch.Replace("refs/heads/","")
42+
if (!($branchName -match $BranchRegex)) {
43+
continue
44+
}
45+
46+
# Get all open sync PRs associate with branch.
3747
try {
3848
$head = "${RepoId}:${branchName}"
3949
LogDebug "Operating on branch [ $branchName ]"
@@ -44,25 +54,52 @@ foreach ($res in $responses)
4454
LogError "Get-GitHubPullRequests failed with exception:`n$_"
4555
exit 1
4656
}
47-
4857
$openPullRequests = $pullRequests | ? { $_.State -eq "open" }
49-
if ($openPullRequests.Count -gt 0)
50-
{
51-
LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has open pull Requests. Skipping"
52-
LogDebug $openPullRequests.url
58+
59+
if (!$CentralRepoId -and $openPullRequests.Count -gt 0) {
60+
LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
5361
continue
5462
}
5563

56-
LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. "
64+
# check central PR
65+
if ($CentralRepoId) {
66+
$pullRequestNumber = $Matches["PrNumber"]
67+
# If central PR number found, then skip
68+
if (!$pullRequestNumber) {
69+
LogError "No PR number found in the branch name. Please check the branch name [ $branchName ]. Skipping..."
70+
continue
71+
}
72+
73+
try {
74+
$centralPR = Get-GitHubPullRequest -RepoId $CentralRepoId -PullRequestNumber $pullRequestNumber -AuthToken $AuthToken
75+
LogDebug "Found central PR pull request: $($centralPR.html_url)"
76+
if ($centralPR.state -ne "closed") {
77+
# Skipping if there open central PR number for the branch.
78+
continue
79+
}
80+
}
81+
catch
82+
{
83+
# If there is no central PR for the PR number, log error and skip.
84+
LogError "Get-GitHubPullRequests failed with exception:`n$_"
85+
LogError "Not found PR number [ $pullRequestNumber ] from [ $CentralRepoId ]. Skipping..."
86+
continue
87+
}
88+
}
89+
90+
foreach ($openPullRequest in $openPullRequests) {
91+
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
92+
}
93+
94+
# If there is date filter, then check if branch last commit older than the date.
5795
if ($LastCommitOlderThan) {
5896
if (!$res.object -or !$res.object.url) {
5997
LogWarning "No commit url returned from response. Skipping... "
6098
continue
6199
}
62100
try {
63101
$commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken
64-
if (!$commitDate)
65-
{
102+
if (!$commitDate) {
66103
LogDebug "No last commit date found. Skipping."
67104
continue
68105
}
@@ -78,9 +115,12 @@ foreach ($res in $responses)
78115
exit 1
79116
}
80117
}
118+
81119
try {
82-
Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
83-
LogDebug "The branch [ $branchName ] in [ $RepoId ] has been deleted."
120+
if ($PSCmdlet.ShouldProcess("[ $branchName ] in [ $RepoId ]", "Deleting branches on cleanup script")) {
121+
Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
122+
Write-Host "The branch [ $branchName ] with sha [$($res.object.sha)] in [ $RepoId ] has been deleted."
123+
}
84124
}
85125
catch {
86126
LogError "Remove-GitHubSourceReferences failed with exception:`n$_"

eng/common/scripts/Invoke-GitHubAPI.ps1

+23-3
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,17 @@ function Get-GitHubSourceReferences {
9595

9696
function Get-GitHubPullRequest {
9797
param (
98-
[Parameter(Mandatory = $true)]
9998
$RepoOwner,
100-
[Parameter(Mandatory = $true)]
10199
$RepoName,
100+
$RepoId = "$RepoOwner/$RepoName",
102101
[Parameter(Mandatory = $true)]
103102
$PullRequestNumber,
104103
[ValidateNotNullOrEmpty()]
105104
[Parameter(Mandatory = $true)]
106105
$AuthToken
107106
)
108107

109-
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls/$PullRequestNumber"
108+
$uri = "$GithubAPIBaseURI/$RepoId/pulls/$PullRequestNumber"
110109

111110
return Invoke-RestMethod `
112111
-Method GET `
@@ -152,6 +151,27 @@ function New-GitHubPullRequest {
152151
-MaximumRetryCount 3
153152
}
154153

154+
function Close-GitHubPullRequest {
155+
param (
156+
[Parameter(Mandatory = $true)]
157+
$apiurl,
158+
[ValidateNotNullOrEmpty()]
159+
[Parameter(Mandatory = $true)]
160+
$AuthToken
161+
)
162+
163+
$parameters = @{
164+
state = "closed"
165+
}
166+
167+
return Invoke-RestMethod `
168+
-Method PATCH `
169+
-Uri $apiurl `
170+
-Body ($parameters | ConvertTo-Json) `
171+
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
172+
-MaximumRetryCount 3
173+
}
174+
155175
function New-GitHubIssue {
156176
param (
157177
[Parameter(Mandatory = $true)]

0 commit comments

Comments
 (0)