Skip to content

Commit 5ba3c16

Browse files
Sync common script changes from azure-sdk repo (#20983)
Co-authored-by: Wes Haggard <[email protected]>
1 parent fab98b0 commit 5ba3c16

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

eng/common/scripts/Delete-RemoteBranches.ps1

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[CmdletBinding(SupportsShouldProcess)]
22
param(
3-
# The repo owner: e.g. Azure
4-
$RepoOwner,
5-
# The repo name. E.g. azure-sdk-for-java
6-
$RepoName,
73
# Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
8-
$RepoId="$RepoOwner/$RepoName",
4+
$RepoId = "$RepoOwner/$RepoName",
5+
# Upstream repo to check and see if there are existing open PRs from before deleting branch
6+
$UpstreamRepoId,
97
# CentralRepoId the original PR to generate sync PR. E.g Azure/azure-sdk-tools for eng/common
108
$CentralRepoId,
119
# 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.
@@ -15,9 +13,11 @@ param(
1513
# Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format.
1614
[AllowNull()]
1715
[DateTime]$LastCommitOlderThan,
16+
[Switch]$DeleteBranchesEvenIfThereIsOpenPR = $false,
1817
[Parameter(Mandatory = $true)]
1918
$AuthToken
2019
)
20+
Set-StrictMode -version 3
2121

2222
. (Join-Path $PSScriptRoot common.ps1)
2323

@@ -44,26 +44,9 @@ foreach ($res in $responses)
4444
continue
4545
}
4646

47-
# Get all open sync PRs associate with branch.
48-
try {
49-
$head = "${RepoId}:${branchName}"
50-
LogDebug "Operating on branch [ $branchName ]"
51-
$pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken
52-
}
53-
catch
47+
# If we have a central PR that created this branch still open still don't delete the branch
48+
if ($CentralRepoId)
5449
{
55-
LogError "Get-GitHubPullRequests failed with exception:`n$_"
56-
exit 1
57-
}
58-
$openPullRequests = $pullRequests | ? { $_.State -eq "open" }
59-
60-
if (!$CentralRepoId -and $openPullRequests.Count -gt 0) {
61-
LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
62-
continue
63-
}
64-
65-
# check central PR
66-
if ($CentralRepoId) {
6750
$pullRequestNumber = $Matches["PrNumber"]
6851
# If central PR number found, then skip
6952
if (!$pullRequestNumber) {
@@ -88,12 +71,33 @@ foreach ($res in $responses)
8871
}
8972
}
9073

91-
foreach ($openPullRequest in $openPullRequests) {
92-
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
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
84+
}
85+
}
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
9396
}
9497

9598
# If there is date filter, then check if branch last commit older than the date.
96-
if ($LastCommitOlderThan) {
99+
if ($LastCommitOlderThan)
100+
{
97101
if (!$res.object -or !$res.object.url) {
98102
LogWarning "No commit url returned from response. Skipping... "
99103
continue
@@ -117,10 +121,15 @@ foreach ($res in $responses)
117121
}
118122
}
119123

120-
try {
124+
foreach ($openPullRequest in $openPullRequests) {
125+
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
126+
}
127+
128+
try
129+
{
121130
if ($PSCmdlet.ShouldProcess("[ $branchName ] in [ $RepoId ]", "Deleting branches on cleanup script")) {
122131
Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
123-
Write-Host "The branch [ $branchName ] with sha [$($res.object.sha)] in [ $RepoId ] has been deleted."
132+
Write-Host "The branch [ $branchName ] with sha [ $($res.object.sha) ] in [ $RepoId ] has been deleted."
124133
}
125134
}
126135
catch {

eng/common/scripts/Helpers/DevOps-WorkItem-Helpers.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,15 @@ function UpdatePackageVersions($pkgWorkItem, $plannedVersions, $shippedVersions)
836836
$shippedVersionSet[$version.Version] = $version
837837
$updateShipped = $true
838838
}
839+
else
840+
{
841+
# Check for any date update, general case would from be previous Unknown to date
842+
if ($shippedVersionSet[$version.Version].Date -ne $version.Date)
843+
{
844+
$shippedVersionSet[$version.Version] = $version
845+
$updateShipped = $true
846+
}
847+
}
839848
}
840849

841850
$versionSet = @{}
@@ -889,11 +898,11 @@ function UpdatePackageVersions($pkgWorkItem, $plannedVersions, $shippedVersions)
889898
# If we shipped a version after we set "In Release" state then reset the state to "Next Release Unknown"
890899
if ($pkgWorkItem.fields["System.State"] -eq "In Release")
891900
{
892-
$lastShippedDate = [DateTime]$newShippedVersions[0].Date
901+
$lastShippedDate = $newShippedVersions[0].Date -as [DateTime]
893902
$markedInReleaseDate = ([DateTime]$pkgWorkItem.fields["Microsoft.VSTS.Common.StateChangeDate"])
894903

895904
# We just shipped so lets set the state to "Next Release Unknown"
896-
if ($markedInReleaseDate -le $lastShippedDate)
905+
if ($lastShippedDate -and $markedInReleaseDate -le $lastShippedDate)
897906
{
898907
$fieldUpdates += @'
899908
{

0 commit comments

Comments
 (0)