Skip to content

Commit

Permalink
Refactor GitHub API Calls
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Oct 8, 2020
1 parent 0425fdc commit 6f7eb91
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 28 deletions.
5 changes: 3 additions & 2 deletions eng/common/scripts/Add-Issue-Comment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ param(
)

. "${PSScriptRoot}\logging.ps1"
. "${PSScriptRoot}\Invoke-GitHub-API.ps1"
. "${PSScriptRoot}\Invoke-GitHub-API.ps1" -AuthToken $AuthToken

$commentPrefixValue = [System.Environment]::GetEnvironmentVariable($CommentPrefix)
$commentValue = [System.Environment]::GetEnvironmentVariable($Comment)
Expand All @@ -34,7 +34,8 @@ if (!$commentValue) { $commentValue = $Comment }
if (!$commentPostFixValue) { $commentPostFixValue = $CommentPostFix }

try {
$resp = AddIssueComment -IssueNumber $IssueNumber -CommentPrefix $commentPrefixValue `
$resp = AddIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
-IssueNumber $IssueNumber -CommentPrefix $commentPrefixValue `
-Comment $commentValue -CommentSuffix $commentPostFixValue
}
catch {
Expand Down
36 changes: 21 additions & 15 deletions eng/common/scripts/Delete-Remote-Branches.ps1
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
param(
$RepoOwner = "Azure",
$RepoOwner,
$RepoName,
$BranchPrefix = "sync-eng/common",
$BranchPrefix,
$WorkingDirectory,
$AuthToken
)

. "${PSScriptRoot}\logging.ps1"
. "${PSScriptRoot}\Invoke-GitHub-API.ps1"
. "${PSScriptRoot}\Invoke-GitHub-API.ps1" -AuthToken $AuthToken

git clone "https://github.com/$RepoOwner/$RepoName"
pushd $WorkingDirectory
git clone https://github.com/$RepoOwner/$RepoName.git
pushd $RepoName
$syncBranches = (git branch --remote).where{$_ -like "*origin/$BranchPrefix*"}
$syncBranches = (git branch --remote).where{ $_ -like "*origin/$BranchPrefix*" }

Write-Host "Repo Name $RepoName"
LogDebug "Operating on Repo [ $RepoName ]"
foreach ($branch in $syncBranches)
{
try {
$branchName = $branch.Trim()
$branchName = ($branch.Trim()).Replace("origin/","")
$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
}
Write-Host "Response Count $($Response.Count)"
if ($Response.Count -eq 0)

if ($response.Count -eq 0)
{
# Delete branch here
#git push origin --delete "sync-${{ parameters.DirectoryToSync }}-$(System.PullRequest.SourceBranch)-$(System.PullRequest.PullRequestNumber)"
#if ($lastExitCode -ne 0) {
# Write-Host "Failed to delete [sync-${{ parameters.DirectoryToSync }}-$(System.PullRequest.SourceBranch)-$(System.PullRequest.PullRequestNumber)] branch in ${{ repo }}"
# exit 1
#}
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
127 changes: 117 additions & 10 deletions eng/common/scripts/Invoke-GitHub-API.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@ param(
$AuthToken
)

$Token = ConvertTo-SecureString -String $AuthToken -AsPlainText -Force
$GithubAPIBaseURI = "https://api.github.com/repos"
function InvokePost($apiURI, $body) {
$resp = Invoke-RestMethod `
-Method POST `
-Body ($body | ConvertTo-Json) `
-Uri $apiURI `
-Authentication Bearer `
-Token $AuthToken
-Token $Token `
-MaximumRetryCount 3
($body | Format-List | Write-Output)
$resp | Write-Verbose

return $resp
}

function InvokePatch($apiURI, $body) {
$resp = Invoke-RestMethod `
-Method PATCH `
-Body ($body | ConvertTo-Json) `
-Uri $apiURI `
-Authentication Bearer `
-Token $Token `
-MaximumRetryCount 3

return $resp
}

function InvokeGet($apiURI) {
$resp = Invoke-RestMethod `
-Method GET `
-Uri $apiURI `
-Authentication Bearer `
-Token $AuthToken
-Token $Token `
-MaximumRetryCount 3
($body | Format-List | Write-Output)
$resp | Write-Verbose

return $resp
}

function SplitMembers ($membersString)
{
if (!$membersString) { return $null }
return @($membersString.Split(",") | % { $_.Trim() } | ? { return $_ })
}

function ListPullRequests {
Expand All @@ -38,7 +57,7 @@ function ListPullRequests {
$base,
[ValidateSet("created","updated","popularity","long-running")]
$sort,
[ValidateSet("asc","desc",)]
[ValidateSet("asc","desc")]
$direction
)

Expand All @@ -55,18 +74,106 @@ function ListPullRequests {

function AddIssueComment {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$IssueNumber,
$CommentPrefix,
[Parameter(Mandatory = $true)]
$Comment,
$CommentSuffix,
$CommentSuffix
)
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/comments"
$PRComment = "$CommentPrefix $Comment $CommentSuffix"

$data = @{
$parameters = @{
body = $PRComment
}
return InvokePost -apiURI $uri -body $data
return InvokePost -apiURI $uri -body $parameters
}

# Will add labels to existing labels on the issue
function AddIssueLabels {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$IssueNumber,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$labels
)

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/labels"
$labelAdditions = SplitMembers -membersString $labels
$parameters = @{
labels = @($labelAdditions)
}

return InvokePost -apiURI $uri -body $parameters
}

# Will add assignees to existing assignees on the issue
function AddIssueAssignees {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$IssueNumber,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$assignees
)

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/assignees"
$assigneesAdditions = SplitMembers -membersString $assignees
$parameters = @{
assignees = @($assigneesAdditions)
}

return InvokePost -apiURI $uri -body $parameters
}

# For labels and assignee pass comma delimited string, to replace existing labels or assignees.
# Or pass white space " " to remove all labels or assignees
function UpdateIssue {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$IssueNumber,
[string]$title,
[string]$body,
[string]$state,
[int]$milestome,
[ValidateNotNullOrEmpty()]
[string]$labels,
[ValidateNotNullOrEmpty()]
[string]$assignees
)

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber"
$parameters = @{}
if ($title) { $parameters["title"] = $title }
if ($body) { $parameters["body"] = $body }
if ($state) { $parameters["state"] = $state }
if ($milestone) { $parameters["milestone"] = $milestone }
if ($labels) {
$labelAdditions = SplitMembers -membersString $labels
$parameters["labels"] = @($labelAdditions)
}
if ($assignees) {
$assigneesAdditions = SplitMembers -membersString $assignees
$parameters["assignees"] = @($assigneesAdditions)
}

return InvokePatch -apiURI $uri -body $parameters
}
3 changes: 2 additions & 1 deletion eng/pipelines/templates/jobs/tools-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ jobs:
filePath: $(System.DefaultWorkingDirectory)/eng/common/scripts/Delete-Remote-Branches.ps1
arguments: >
-RepoName ${{ repo }}
-BranchPrefix "feature"
-BranchPrefix "sync-eng/common-"
-WorkingDirectory $(System.DefaultWorkingDirectory)
-AuthToken "$(azuresdk-github-pat)"

0 comments on commit 6f7eb91

Please sign in to comment.