Skip to content

Commit

Permalink
Switch to use github API
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Oct 13, 2020
1 parent 7835581 commit e7f4560
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
39 changes: 22 additions & 17 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,47 @@ 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
{
LogError "List-PullRequests failed with exception:`n$_"
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
}
62 changes: 62 additions & 0 deletions eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -105,6 +123,27 @@ function List-PullRequests {
return Invoke-GitHubAPIGet -apiURI $uri
}

#
<#
.PARAMETER Ref
Ref to search for
Pass 'heads/<branchame> ,tags/<tag name>, 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)]
Expand Down Expand Up @@ -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
}

0 comments on commit e7f4560

Please sign in to comment.