From 9fc895c1c7d375810e0b7608d2625237684ec64c Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 12 Oct 2020 10:17:07 -0700 Subject: [PATCH 1/4] Add pipeline configuration for cleaning up upstream branches --- eng/common/scripts/Delete-Remote-Branches.ps1 | 43 +++++++++++++++++++ .../templates/jobs/tools-cleanup.yml | 32 ++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 eng/common/scripts/Delete-Remote-Branches.ps1 create mode 100644 eng/pipelines/templates/jobs/tools-cleanup.yml diff --git a/eng/common/scripts/Delete-Remote-Branches.ps1 b/eng/common/scripts/Delete-Remote-Branches.ps1 new file mode 100644 index 00000000000..71dcf3982ec --- /dev/null +++ b/eng/common/scripts/Delete-Remote-Branches.ps1 @@ -0,0 +1,43 @@ +param( + $RepoOwner, + $RepoName, + $BranchPrefix, + $WorkingDirectory, + $AuthToken +) + +. "${PSScriptRoot}\common.ps1" + +pushd $WorkingDirectory +git clone https://github.com/$RepoOwner/$RepoName.git +pushd $RepoName +$syncBranches = git branch -r --list origin/$BranchPrefix* | % { $_ -replace "origin/", "" } + +LogDebug "Operating on Repo [ $RepoName ]" +foreach ($branch in $syncBranches) +{ + try { + $branchName = $branch.Trim() + $head = "${RepoOwner}/${RepoName}:${branchName}" + LogDebug "Operating on branch [ $branchName ]" + $response = ListPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head + } + catch + { + LogError "ListPullRequests failed with exception:`n$_" + exit 1 + } + + if ($response.Count -eq 0) + { + LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated Pull Request. Deleting Branch" + git push origin --delete $branchName + if ($lastExitCode -ne 0) { + Write-Host "Failed to delete branch [ $branchName ] in repo [ $RepoName ]" + exit 1 + } + } +} + +popd +popd diff --git a/eng/pipelines/templates/jobs/tools-cleanup.yml b/eng/pipelines/templates/jobs/tools-cleanup.yml new file mode 100644 index 00000000000..8ba4d2e9407 --- /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-Remote-Branches.ps1 + arguments: > + -RepoName ${{ repo }} + -BranchPrefix "sync-eng/common-" + -WorkingDirectory $(System.DefaultWorkingDirectory) + -AuthToken $(azuresdk-github-pat) \ No newline at end of file From 78355812cae9d4a1e82d4ab63c14ddc4228db92f Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 12 Oct 2020 11:32:31 -0700 Subject: [PATCH 2/4] Fix powershell naming --- .../{Delete-Remote-Branches.ps1 => Delete-RemoteBranches.ps1} | 4 ++-- eng/pipelines/templates/jobs/tools-cleanup.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename eng/common/scripts/{Delete-Remote-Branches.ps1 => Delete-RemoteBranches.ps1} (84%) diff --git a/eng/common/scripts/Delete-Remote-Branches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 similarity index 84% rename from eng/common/scripts/Delete-Remote-Branches.ps1 rename to eng/common/scripts/Delete-RemoteBranches.ps1 index 71dcf3982ec..66913163bf4 100644 --- a/eng/common/scripts/Delete-Remote-Branches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -20,11 +20,11 @@ foreach ($branch in $syncBranches) $branchName = $branch.Trim() $head = "${RepoOwner}/${RepoName}:${branchName}" LogDebug "Operating on branch [ $branchName ]" - $response = ListPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head + $response = List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head -AuthToken $AuthToken } catch { - LogError "ListPullRequests failed with exception:`n$_" + LogError "List-PullRequests failed with exception:`n$_" exit 1 } diff --git a/eng/pipelines/templates/jobs/tools-cleanup.yml b/eng/pipelines/templates/jobs/tools-cleanup.yml index 8ba4d2e9407..a7bb15e956c 100644 --- a/eng/pipelines/templates/jobs/tools-cleanup.yml +++ b/eng/pipelines/templates/jobs/tools-cleanup.yml @@ -24,7 +24,7 @@ jobs: inputs: pwsh: true workingDirectory: $(System.DefaultWorkingDirectory) - filePath: $(System.DefaultWorkingDirectory)/eng/common/scripts/Delete-Remote-Branches.ps1 + filePath: $(System.DefaultWorkingDirectory)/eng/common/scripts/Delete-RemoteBranches.ps1 arguments: > -RepoName ${{ repo }} -BranchPrefix "sync-eng/common-" From e7f45607802f036e25e3c551393d734b50cec80d Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 12 Oct 2020 18:35:52 -0700 Subject: [PATCH 3/4] Switch to use github API --- eng/common/scripts/Delete-RemoteBranches.ps1 | 39 ++++++------ eng/common/scripts/Invoke-GitHubAPI.ps1 | 62 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 66913163bf4..0dd20150589 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -2,25 +2,27 @@ param( $RepoOwner, $RepoName, $BranchPrefix, - $WorkingDirectory, $AuthToken ) . "${PSScriptRoot}\common.ps1" -pushd $WorkingDirectory -git clone https://github.com/$RepoOwner/$RepoName.git -pushd $RepoName -$syncBranches = git branch -r --list origin/$BranchPrefix* | % { $_ -replace "origin/", "" } - LogDebug "Operating on Repo [ $RepoName ]" -foreach ($branch in $syncBranches) +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.Trim() + $branchName = $branch.Replace("refs/heads/","") $head = "${RepoOwner}/${RepoName}:${branchName}" LogDebug "Operating on branch [ $branchName ]" - $response = List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head -AuthToken $AuthToken + $pullRequests = List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head } catch { @@ -28,16 +30,19 @@ foreach ($branch in $syncBranches) exit 1 } - if ($response.Count -eq 0) + "bvranch $branch" + "PR COunt $($pullRequests.Count)" + + + if ($pullRequests.Count -eq 0) { LogDebug "Branch [ $branchName ] in repo [ $RepoName ] has no associated Pull Request. Deleting Branch" - git push origin --delete $branchName - if ($lastExitCode -ne 0) { - Write-Host "Failed to delete branch [ $branchName ] in repo [ $RepoName ]" + try{ + Delete-References -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) + } + catch { + LogError "Delete-References failed with exception:`n$_" exit 1 } } -} - -popd -popd +} \ 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 From 66a2e8b5b8222f0cc3f11fec1b448a49cbd49882 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 12 Oct 2020 18:40:24 -0700 Subject: [PATCH 4/4] Use github API to delete branches --- eng/common/scripts/Delete-RemoteBranches.ps1 | 6 +----- eng/pipelines/templates/jobs/tools-cleanup.yml | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 0dd20150589..da55471ef25 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -30,15 +30,11 @@ foreach ($branch in $branches) exit 1 } - "bvranch $branch" - "PR COunt $($pullRequests.Count)" - - 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)) + Delete-References -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken } catch { LogError "Delete-References failed with exception:`n$_" diff --git a/eng/pipelines/templates/jobs/tools-cleanup.yml b/eng/pipelines/templates/jobs/tools-cleanup.yml index a7bb15e956c..88be953e580 100644 --- a/eng/pipelines/templates/jobs/tools-cleanup.yml +++ b/eng/pipelines/templates/jobs/tools-cleanup.yml @@ -26,7 +26,7 @@ jobs: workingDirectory: $(System.DefaultWorkingDirectory) filePath: $(System.DefaultWorkingDirectory)/eng/common/scripts/Delete-RemoteBranches.ps1 arguments: > + -RepoOwner "Azure" -RepoName ${{ repo }} -BranchPrefix "sync-eng/common-" - -WorkingDirectory $(System.DefaultWorkingDirectory) -AuthToken $(azuresdk-github-pat) \ No newline at end of file