Remove the daily branch before date (#3593)
Co-authored-by: sima-zhu <sizhu@microsoft.com>
This commit is contained in:
parent
637def0979
commit
d1be7c8bfd
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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/<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 `
|
||||
@ -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/<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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user