From 1978205617e1c4d55e70bc5ab99897a0b44b62d7 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Wed, 27 Apr 2022 22:34:42 -0700 Subject: [PATCH] Remove the daily branch before date --- eng/common/scripts/Delete-RemoteBranches.ps1 | 62 +++++++++++++------- eng/common/scripts/Invoke-GitHubAPI.ps1 | 46 ++++++++++----- 2 files changed, 71 insertions(+), 37 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index f5ad981bd7..6b5e170309 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -1,40 +1,43 @@ param( - [Parameter(Mandatory = $true)] + # The repo owner: e.g. Azure $RepoOwner, - # Use this if a pull request might have been opened from one repo against another. - # E.g Pull request opened from azure-sdk/azure-sdk prBranch --> Azure/azure-sdk baseBranch - $ForkRepoOwner, - [Parameter(Mandatory = $true)] + # 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", [Parameter(Mandatory = $true)] - $BranchPrefix, + $BranchPrefix, + # Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format. + [AllowNull()] + [DateTime]$LastCommitOlderThan, [Parameter(Mandatory = $true)] $AuthToken ) . (Join-Path $PSScriptRoot common.ps1) -LogDebug "Operating on Repo [ $RepoName ]" +LogDebug "Operating on Repo [ $RepoId ]" + try{ - $branches = (Get-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix" -AuthToken $AuthToken).ref + $responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads/$BranchPrefix" -AuthToken $AuthToken } catch { LogError "Get-GitHubSourceReferences failed with exception:`n$_" exit 1 } -foreach ($branch in $branches) +foreach ($res in $responses) { + if (!$res -or !$res.ref) { + LogDebug "No branch returned from the branch prefix $BranchPrefix on $Repo. Skipping..." + continue + } + $branch = $res.ref try { $branchName = $branch.Replace("refs/heads/","") - $head = "${RepoOwner}/${RepoName}:${branchName}" + $head = "${RepoId}:${branchName}" LogDebug "Operating on branch [ $branchName ]" - $pullRequests = Get-GitHubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken - - if ($ForkRepoOwner) - { - $pullRequests += Get-GitHubPullRequests -RepoOwner $ForkRepoOwner -RepoName $RepoName -State "all" -Head $head -AuthToken $AuthToken - } + $pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken } catch { @@ -45,17 +48,34 @@ foreach ($branch in $branches) $openPullRequests = $pullRequests | ? { $_.State -eq "open" } if ($openPullRequests.Count -gt 0) { - LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has open pull Requests. Skipping" + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has open pull Requests. Skipping" LogDebug $openPullRequests.url continue } - LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated open Pull Request. Deleting Branch" - try{ - Remove-GitHubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken + if ($LastCommitOlderThan) { + if (!$res.object -or !$res.object.url) { + LogWarning "No commit url returned from response. Skipping... " + continue + } + try { + $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken + if ($commitDate -and ($commitDate -gt $LastCommitOlderThan)) { + LogDebug "The branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." + continue + } + } + catch { + LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" + exit 1 + } + } + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. " + try { + Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken } catch { LogError "Remove-GitHubSourceReferences failed with exception:`n$_" exit 1 } -} \ No newline at end of file +} diff --git a/eng/common/scripts/Invoke-GitHubAPI.ps1 b/eng/common/scripts/Invoke-GitHubAPI.ps1 index 9e5e6b3a74..6f02d2edc6 100644 --- a/eng/common/scripts/Invoke-GitHubAPI.ps1 +++ b/eng/common/scripts/Invoke-GitHubAPI.ps1 @@ -37,10 +37,9 @@ function Set-GitHubAPIParameters ($members, $parameterName, $parameters, $allow function Get-GitHubPullRequests { param ( - [Parameter(Mandatory = $true)] $RepoOwner, - [Parameter(Mandatory = $true)] $RepoName, + $RepoId = "$RepoOwner/$RepoName", [ValidateSet("open","closed","all")] $State = "open", $Head, @@ -53,8 +52,7 @@ function Get-GitHubPullRequests { [ValidateNotNullOrEmpty()] $AuthToken ) - - $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls" + $uri = "$GithubAPIBaseURI/$RepoId/pulls" if ($State -or $Head -or $Base -or $Sort -or $Direction) { $uri += '?' } if ($State) { $uri += "state=$State&" } if ($Head) { $uri += "head=$Head&" } @@ -77,17 +75,15 @@ Pass 'heads/ ,tags/, or nothing #> function Get-GitHubSourceReferences { param ( - [Parameter(Mandatory = $true)] $RepoOwner, - [Parameter(Mandatory = $true)] $RepoName, + $RepoId = "$RepoOwner/$RepoName", $Ref, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] $AuthToken ) - - $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/git/matching-refs/" + $uri = "$GithubAPIBaseURI/$RepoId/git/matching-refs/" if ($Ref) { $uri += "$Ref" } return Invoke-RestMethod ` @@ -121,7 +117,6 @@ function Get-GitHubPullRequest { function New-GitHubPullRequest { param ( - [Parameter(Mandatory = $true)] $RepoOwner, [Parameter(Mandatory = $true)] $RepoName, @@ -392,28 +387,47 @@ function Update-GitHubIssue { function Remove-GitHubSourceReferences { param ( - [Parameter(Mandatory = $true)] $RepoOwner, - [Parameter(Mandatory = $true)] $RepoName, + $RepoId = "$RepoOwner/$RepoName", [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] - $Ref, + $Ref, # Using the format of "refs/heads/" or "heads/" for branch, and "tags/" for tag [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] $AuthToken ) - if ($Ref.Trim().Length -eq 0) { throw "You must supply a valid 'Ref' Parameter to 'Delete-GithubSourceReferences'." } - - $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/git/refs/$Ref" + # Github is using branch in format of "heads/{branch_name}". Trim the "refs/heads/..." to "heads/..." + $Ref = $Ref -replace "refs/" + $uri = "$GithubAPIBaseURI/$RepoId/git/refs/$Ref" return Invoke-RestMethod ` -Method DELETE ` -Uri $uri ` -Headers (Get-GitHubApiHeaders -token $AuthToken) ` -MaximumRetryCount 3 -} \ No newline at end of file +} + + +function Get-GithubReferenceCommitDate($commitUrl, $AuthToken) { + $commitResponse = "" + if ($AuthToken) + { + $commitResponse = Invoke-RestMethod $commitUrl ` + -Headers (Get-GitHubApiHeaders -token $AuthToken) ` + -MaximumRetryCount 3 + } + else + { + $commitResponse = Invoke-RestMethod $commitUrl -MaximumRetryCount 3 + } + if (!$commitResponse.committer -or !$commitResponse.committer.date) { + LogDebug "No date returned from the commit sha. " + return $null + } + return $commitData.committer.date +}