From 644ab3e0b7430b38a55fd01971b5526acbcc7eab Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Mon, 23 May 2022 14:47:48 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 3342 (#3664) * 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 * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard * Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard Co-authored-by: sima-zhu Co-authored-by: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Co-authored-by: Wes Haggard --- eng/common/scripts/Delete-RemoteBranches.ps1 | 68 ++++++++++++++++---- eng/common/scripts/Invoke-GitHubAPI.ps1 | 26 +++++++- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 4ee3f1b05..2d1c3c303 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -1,3 +1,4 @@ +[CmdletBinding(SupportsShouldProcess)] param( # The repo owner: e.g. Azure $RepoOwner, @@ -5,8 +6,11 @@ param( $RepoName, # Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java $RepoId="$RepoOwner/$RepoName", - [Parameter(Mandatory = $true)] - $BranchPrefix, + # 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-()-(). Have group name on PR number. + # For sync-eng/common work, we use regex as "^sync-eng/common.*-(?\d+).*$". + $BranchRegex, # Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format. [AllowNull()] [DateTime]$LastCommitOlderThan, @@ -19,7 +23,8 @@ param( LogDebug "Operating on Repo [ $RepoId ]" try{ - $responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads/$BranchPrefix" -AuthToken $AuthToken + # pull all branches. + $responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads" -AuthToken $AuthToken } catch { LogError "Get-GitHubSourceReferences failed with exception:`n$_" @@ -29,11 +34,16 @@ catch { foreach ($res in $responses) { if (!$res -or !$res.ref) { - LogDebug "No branch returned from the branch prefix $BranchPrefix on $Repo. Skipping..." + LogDebug "No branch returned from the branch prefix $BranchRegex on $Repo. Skipping..." continue } $branch = $res.ref $branchName = $branch.Replace("refs/heads/","") + if (!($branchName -match $BranchRegex)) { + continue + } + + # Get all open sync PRs associate with branch. try { $head = "${RepoId}:${branchName}" LogDebug "Operating on branch [ $branchName ]" @@ -44,16 +54,44 @@ foreach ($res in $responses) LogError "Get-GitHubPullRequests failed with exception:`n$_" exit 1 } - $openPullRequests = $pullRequests | ? { $_.State -eq "open" } - if ($openPullRequests.Count -gt 0) - { - LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has open pull Requests. Skipping" - LogDebug $openPullRequests.url + + if (!$CentralRepoId -and $openPullRequests.Count -gt 0) { + LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..." continue } - LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. " + # check central PR + if ($CentralRepoId) { + $pullRequestNumber = $Matches["PrNumber"] + # If central PR number found, then skip + if (!$pullRequestNumber) { + LogError "No PR number found in the branch name. Please check the branch name [ $branchName ]. Skipping..." + continue + } + + try { + $centralPR = Get-GitHubPullRequest -RepoId $CentralRepoId -PullRequestNumber $pullRequestNumber -AuthToken $AuthToken + LogDebug "Found central PR pull request: $($centralPR.html_url)" + if ($centralPR.state -ne "closed") { + # Skipping if there open central PR number for the branch. + continue + } + } + catch + { + # If there is no central PR for the PR number, log error and skip. + LogError "Get-GitHubPullRequests failed with exception:`n$_" + LogError "Not found PR number [ $pullRequestNumber ] from [ $CentralRepoId ]. Skipping..." + continue + } + } + + foreach ($openPullRequest in $openPullRequests) { + Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion." + } + + # If there is date filter, then check if branch last commit older than the date. if ($LastCommitOlderThan) { if (!$res.object -or !$res.object.url) { LogWarning "No commit url returned from response. Skipping... " @@ -61,8 +99,7 @@ foreach ($res in $responses) } try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken - if (!$commitDate) - { + if (!$commitDate) { LogDebug "No last commit date found. Skipping." continue } @@ -78,9 +115,12 @@ foreach ($res in $responses) exit 1 } } + try { - Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken - LogDebug "The branch [ $branchName ] in [ $RepoId ] has been deleted." + 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." + } } catch { LogError "Remove-GitHubSourceReferences failed with exception:`n$_" diff --git a/eng/common/scripts/Invoke-GitHubAPI.ps1 b/eng/common/scripts/Invoke-GitHubAPI.ps1 index 0bc2e871c..504ac51ad 100644 --- a/eng/common/scripts/Invoke-GitHubAPI.ps1 +++ b/eng/common/scripts/Invoke-GitHubAPI.ps1 @@ -95,10 +95,9 @@ function Get-GitHubSourceReferences { function Get-GitHubPullRequest { param ( - [Parameter(Mandatory = $true)] $RepoOwner, - [Parameter(Mandatory = $true)] $RepoName, + $RepoId = "$RepoOwner/$RepoName", [Parameter(Mandatory = $true)] $PullRequestNumber, [ValidateNotNullOrEmpty()] @@ -106,7 +105,7 @@ function Get-GitHubPullRequest { $AuthToken ) - $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls/$PullRequestNumber" + $uri = "$GithubAPIBaseURI/$RepoId/pulls/$PullRequestNumber" return Invoke-RestMethod ` -Method GET ` @@ -152,6 +151,27 @@ function New-GitHubPullRequest { -MaximumRetryCount 3 } +function Close-GitHubPullRequest { + param ( + [Parameter(Mandatory = $true)] + $apiurl, + [ValidateNotNullOrEmpty()] + [Parameter(Mandatory = $true)] + $AuthToken + ) + + $parameters = @{ + state = "closed" + } + + return Invoke-RestMethod ` + -Method PATCH ` + -Uri $apiurl ` + -Body ($parameters | ConvertTo-Json) ` + -Headers (Get-GitHubApiHeaders -token $AuthToken) ` + -MaximumRetryCount 3 +} + function New-GitHubIssue { param ( [Parameter(Mandatory = $true)]