From edf1b48c3dbd69df70d04997529bd8fc3e0f61e1 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu <31145988+chidozieononiwu@users.noreply.github.com> Date: Tue, 13 Oct 2020 21:51:33 -0700 Subject: [PATCH] Add pipeline configuration for cleaning up upstream branches (#1088) - Add Cleanup pipeline. - The intention is to run this on a schedule. The first step here is for cleaning up `eng sommon sync` branches. --- eng/common/scripts/Delete-RemoteBranches.ps1 | 44 +++++++++++++ eng/common/scripts/Invoke-GitHubAPI.ps1 | 62 +++++++++++++++++++ .../templates/jobs/tools-cleanup.yml | 32 ++++++++++ 3 files changed, 138 insertions(+) create mode 100644 eng/common/scripts/Delete-RemoteBranches.ps1 create mode 100644 eng/pipelines/templates/jobs/tools-cleanup.yml diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 new file mode 100644 index 00000000000..da55471ef25 --- /dev/null +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -0,0 +1,44 @@ +param( + $RepoOwner, + $RepoName, + $BranchPrefix, + $AuthToken +) + +. "${PSScriptRoot}\common.ps1" + +LogDebug "Operating on Repo [ $RepoName ]" +try{ + $branches = (List-References -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix").ref +} +catch { + LogError "List-References failed with exception:`n$_" + exit 1 +} + +foreach ($branch in $branches) +{ + try { + $branchName = $branch.Replace("refs/heads/","") + $head = "${RepoOwner}/${RepoName}:${branchName}" + LogDebug "Operating on branch [ $branchName ]" + $pullRequests = List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head + } + catch + { + LogError "List-PullRequests failed with exception:`n$_" + exit 1 + } + + if ($pullRequests.Count -eq 0) + { + LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated Pull Request. Deleting Branch" + try{ + Delete-References -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken + } + catch { + LogError "Delete-References 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 e2836856dc9..22f370f3202 100644 --- a/eng/common/scripts/Invoke-GitHubAPI.ps1 +++ b/eng/common/scripts/Invoke-GitHubAPI.ps1 @@ -47,6 +47,24 @@ function Invoke-GitHubAPIPatch { return $resp } +function Invoke-GitHubAPIDelete { + param ( + [Parameter(Mandatory = $true)] + $apiURI, + [Parameter(Mandatory = $true)] + $token + ) + + $resp = Invoke-RestMethod ` + -Method DELETE ` + -Uri $apiURI ` + -Headers (Get-GitHubHeaders -token $token) ` + -MaximumRetryCount 3 + + return $resp +} + + function Invoke-GitHubAPIGet { param ( [Parameter(Mandatory = $true)] @@ -105,6 +123,27 @@ function List-PullRequests { return Invoke-GitHubAPIGet -apiURI $uri } +# +<# +.PARAMETER Ref +Ref to search for +Pass 'heads/ ,tags/, or nothing +#> +function List-References { + param ( + [Parameter(Mandatory = $true)] + $RepoOwner, + [Parameter(Mandatory = $true)] + $RepoName, + $Ref + ) + + $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/git/matching-refs/" + if ($Ref) { $uri += "$Ref" } + + return Invoke-GitHubAPIGet -apiURI $uri +} + function Add-IssueComment { param ( [Parameter(Mandatory = $true)] @@ -229,4 +268,27 @@ function Update-Issue { } return Invoke-GitHubAPIPatch -apiURI $uri -body $parameters -token $AuthToken +} + +function Delete-References { + param ( + [Parameter(Mandatory = $true)] + $RepoOwner, + [Parameter(Mandatory = $true)] + $RepoName, + [ValidateNotNullOrEmpty()] + [Parameter(Mandatory = $true)] + $Ref, + [Parameter(Mandatory = $true)] + $AuthToken + ) + + if ($Ref.Trim().Length -eq 0) + { + throw "You must supply a valid 'Ref' Parameter to 'Delete-Reference'." + } + + $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/git/refs/$Ref" + + return Invoke-GitHubAPIDelete -apiURI $uri -token $AuthToken } \ No newline at end of file diff --git a/eng/pipelines/templates/jobs/tools-cleanup.yml b/eng/pipelines/templates/jobs/tools-cleanup.yml new file mode 100644 index 00000000000..88be953e580 --- /dev/null +++ b/eng/pipelines/templates/jobs/tools-cleanup.yml @@ -0,0 +1,32 @@ +parameters: +- name: Repos + type: object + default: + - azure-sdk-for-android + - azure-sdk-for-c + - azure-sdk-for-cpp + - azure-sdk-for-go + - azure-sdk-for-ios + - azure-sdk-for-java + - azure-sdk-for-js + - azure-sdk-for-net + - azure-sdk-for-python + +jobs: + - job: CleanUp + pool: + vmImage: windows-2019 + steps: + - ${{ each repo in parameters.Repos }}: + - task: PowerShell@2 + displayName: Clean Up Sync Common Branches + condition: succeeded() + inputs: + pwsh: true + workingDirectory: $(System.DefaultWorkingDirectory) + filePath: $(System.DefaultWorkingDirectory)/eng/common/scripts/Delete-RemoteBranches.ps1 + arguments: > + -RepoOwner "Azure" + -RepoName ${{ repo }} + -BranchPrefix "sync-eng/common-" + -AuthToken $(azuresdk-github-pat) \ No newline at end of file