Skip to content

Commit

Permalink
Sync common script changes from azure-sdk repo (#20983)
Browse files Browse the repository at this point in the history
Co-authored-by: Wes Haggard <[email protected]>
  • Loading branch information
azure-sdk and weshaggard authored Jun 8, 2023
1 parent fab98b0 commit 5ba3c16
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
67 changes: 38 additions & 29 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[CmdletBinding(SupportsShouldProcess)]
param(
# The repo owner: e.g. Azure
$RepoOwner,
# The repo name. E.g. azure-sdk-for-java
$RepoName,
# Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
$RepoId="$RepoOwner/$RepoName",
$RepoId = "$RepoOwner/$RepoName",
# Upstream repo to check and see if there are existing open PRs from before deleting branch
$UpstreamRepoId,
# 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.
Expand All @@ -15,9 +13,11 @@ param(
# Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format.
[AllowNull()]
[DateTime]$LastCommitOlderThan,
[Switch]$DeleteBranchesEvenIfThereIsOpenPR = $false,
[Parameter(Mandatory = $true)]
$AuthToken
)
Set-StrictMode -version 3

. (Join-Path $PSScriptRoot common.ps1)

Expand All @@ -44,26 +44,9 @@ foreach ($res in $responses)
continue
}

# Get all open sync PRs associate with branch.
try {
$head = "${RepoId}:${branchName}"
LogDebug "Operating on branch [ $branchName ]"
$pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken
}
catch
# If we have a central PR that created this branch still open still don't delete the branch
if ($CentralRepoId)
{
LogError "Get-GitHubPullRequests failed with exception:`n$_"
exit 1
}
$openPullRequests = $pullRequests | ? { $_.State -eq "open" }

if (!$CentralRepoId -and $openPullRequests.Count -gt 0) {
LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
continue
}

# check central PR
if ($CentralRepoId) {
$pullRequestNumber = $Matches["PrNumber"]
# If central PR number found, then skip
if (!$pullRequestNumber) {
Expand All @@ -88,12 +71,33 @@ foreach ($res in $responses)
}
}

foreach ($openPullRequest in $openPullRequests) {
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
# If this branch has an open PR in the repo or the upstream repo then don't delete
try
{
$head = "${RepoId}:${branchName}"
LogDebug "Operating on branch [ $branchName ]"
$pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken

# check to see if there are any PR's open in the main central repo as well.
if ($UpstreamRepoId) {
$pullRequests += Get-GitHubPullRequests -RepoId $UpstreamRepoId -State "all" -Head $head -AuthToken $AuthToken
}
}
catch
{
LogError "Get-GitHubPullRequests failed with exception:`n$_"
exit 1
}
$openPullRequests = @($pullRequests | Where-Object { $_.State -eq "open" })

if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) {
LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
continue
}

# If there is date filter, then check if branch last commit older than the date.
if ($LastCommitOlderThan) {
if ($LastCommitOlderThan)
{
if (!$res.object -or !$res.object.url) {
LogWarning "No commit url returned from response. Skipping... "
continue
Expand All @@ -117,10 +121,15 @@ foreach ($res in $responses)
}
}

try {
foreach ($openPullRequest in $openPullRequests) {
Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
}

try
{
if ($PSCmdlet.ShouldProcess("[ $branchName ] in [ $RepoId ]", "Deleting branches on cleanup script")) {
Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
Write-Host "The branch [ $branchName ] with sha [$($res.object.sha)] in [ $RepoId ] has been deleted."
Write-Host "The branch [ $branchName ] with sha [ $($res.object.sha) ] in [ $RepoId ] has been deleted."
}
}
catch {
Expand Down
13 changes: 11 additions & 2 deletions eng/common/scripts/Helpers/DevOps-WorkItem-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,15 @@ function UpdatePackageVersions($pkgWorkItem, $plannedVersions, $shippedVersions)
$shippedVersionSet[$version.Version] = $version
$updateShipped = $true
}
else
{
# Check for any date update, general case would from be previous Unknown to date
if ($shippedVersionSet[$version.Version].Date -ne $version.Date)
{
$shippedVersionSet[$version.Version] = $version
$updateShipped = $true
}
}
}

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

# We just shipped so lets set the state to "Next Release Unknown"
if ($markedInReleaseDate -le $lastShippedDate)
if ($lastShippedDate -and $markedInReleaseDate -le $lastShippedDate)
{
$fieldUpdates += @'
{
Expand Down

0 comments on commit 5ba3c16

Please sign in to comment.