Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools for PR 3061 #3593

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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
}
}
}
46 changes: 30 additions & 16 deletions eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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&" }
Expand All @@ -77,17 +75,15 @@ Pass 'heads/<branchame> ,tags/<tag name>, 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 `
Expand Down Expand Up @@ -121,7 +117,6 @@ function Get-GitHubPullRequest {

function New-GitHubPullRequest {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
Expand Down Expand Up @@ -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/<branch>" or "heads/<branch>" for branch, and "tags/<tag>" 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
}
}


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
}