Skip to content

Commit

Permalink
Rework API Lib
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu authored and azure-sdk committed Oct 15, 2020
1 parent f19361c commit ce4793b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ steps:
-AuthToken "$(azuresdk-github-pat)"
-PRTitle "${{ parameters.PRTitle }}"
-PRBody "${{ coalesce(parameters.PRBody, parameters.CommitMsg, parameters.PRTitle) }}"
-PRLabels "${{ parameters.PRLabels}}"
-PRLabels "${{ parameters.PRLabels }}"
-UserReviewers "${{ parameters.GHReviewersVariable }}"
-TeamReviewers "${{ parameters.GHTeamReviewersVariable }}"
-Assignees "${{ parameters.GHAssignessVariable }}"
4 changes: 2 additions & 2 deletions eng/common/scripts/Add-IssueComment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ param(
. "${PSScriptRoot}\common.ps1"

try {
Add-IssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
Add-GithubIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
-IssueNumber $IssueNumber -Comment $Comment -AuthToken $AuthToken
}
catch {
LogError "Add-IssueComment failed with exception:`n$_"
LogError "Add-GithubIssueComment failed with exception:`n$_"
exit 1
}
4 changes: 2 additions & 2 deletions eng/common/scripts/Add-IssueLabels.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ param(
. "${PSScriptRoot}\common.ps1"

try {
Add-IssueLabels -RepoOwner $RepoOwner -RepoName $RepoName `
Add-GithubIssueLabels -RepoOwner $RepoOwner -RepoName $RepoName `
-IssueNumber $IssueNumber -Labels $Labels -AuthToken $AuthToken
}
catch {
LogError "Add-IssueLabels failed with exception:`n$_"
LogError "Add-GithubIssueLabels failed with exception:`n$_"
exit 1
}
12 changes: 6 additions & 6 deletions eng/common/scripts/Delete-RemoteBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ param(

LogDebug "Operating on Repo [ $RepoName ]"
try{
$branches = (List-References -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix").ref
$branches = (List-GithubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref "heads/$BranchPrefix").ref
}
catch {
LogError "List-References failed with exception:`n$_"
LogError "List-GithubSourceReferences failed with exception:`n$_"
exit 1
}

Expand All @@ -22,22 +22,22 @@ foreach ($branch in $branches)
$branchName = $branch.Replace("refs/heads/","")
$head = "${RepoOwner}/${RepoName}:${branchName}"
LogDebug "Operating on branch [ $branchName ]"
$pullRequests = List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head
$pullRequests = List-GithubPullRequests -RepoOwner $RepoOwner -RepoName $RepoName -head $head
}
catch
{
LogError "List-PullRequests failed with exception:`n$_"
LogError "List-GithubPullRequests failed with exception:`n$_"
exit 1
}

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)) -AuthToken $AuthToken
Delete-GithubSourceReferences -RepoOwner $RepoOwner -RepoName $RepoName -Ref ($branch.Remove(0,5)) -AuthToken $AuthToken
}
catch {
LogError "Delete-References failed with exception:`n$_"
LogError "Delete-GithubSourceReferences failed with exception:`n$_"
exit 1
}
}
Expand Down
2 changes: 1 addition & 1 deletion eng/common/scripts/Get-PrCreator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ param (

try
{
$pullRequest = Get-PullRequest -RepoOwner $RepoOwner -RepoName $RepoName -PullRequestNumber $PullRequestNumber
$pullRequest = Get-GithubPullRequest -RepoOwner $RepoOwner -RepoName $RepoName -PullRequestNumber $PullRequestNumber
Write-Host "##vso[task.setvariable variable=System.PullRequest.Creator;]$($pullRequest.user.login)"
}
catch
Expand Down
128 changes: 57 additions & 71 deletions eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,29 @@ function Invoke-GitHubAPIGet {
return $resp
}

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

function List-PullRequests {
function Set-GitHubAPIParameters ($members, $parameterName, $parameters, $allowEmptyMembers=$false)
{
if ($members) {
if ($members -is [array])
{
$parameters[$parameterName] = @($members)
}
elseif (![System.String]::IsNullOrWhiteSpace($members) -or $allowEmptyMembers) {
$memberAdditions = Split-GitHubAPIParameterMembers -membersString $members
$parameters[$parameterName] = @($memberAdditions)
}
}

return $parameters
}

function List-GithubPullRequests {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -109,18 +125,11 @@ function List-PullRequests {
[ValidateSet("created","updated","popularity","long-running")]
$Sort,
[ValidateSet("asc","desc")]
$Direction,
[Parameter(DontShow)]
$PullRequestNumber
$Direction
)

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls"
if ($State -or $Head -or $Base -or $Sort -or $Direction) {
$uri += '?'
}
elseif ($PullRequestNumber) {
$uri += "/${PullRequestNumber}"
}
if ($State -or $Head -or $Base -or $Sort -or $Direction) { $uri += '?' }
if ($State) { $uri += "state=$State&" }
if ($Head) { $uri += "head=$Head&" }
if ($Base) { $uri += "base=$Base&" }
Expand All @@ -136,7 +145,7 @@ function List-PullRequests {
Ref to search for
Pass 'heads/<branchame> ,tags/<tag name>, or nothing
#>
function List-References {
function List-GithubSourceReferences {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -151,7 +160,7 @@ function List-References {
return Invoke-GitHubAPIGet -apiURI $uri
}

function Get-PullRequest {
function Get-GithubPullRequest {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -161,10 +170,11 @@ function Get-PullRequest {
$PullRequestNumber
)

return List-PullRequests -RepoOwner $RepoOwner -RepoName $RepoName -PullRequestNumber $PullRequestNumber
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls/$PullRequestNumber"
return Invoke-GitHubAPIGet -apiURI $uri
}

function Create-PullRequest {
function Create-GithubPullRequest {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -189,14 +199,14 @@ function Create-PullRequest {
base = $Base
body = $Body
maintainer_can_modify = $Maintainer_Can_Modify
$draft = $Draft
draft = $Draft
}

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls"
return Invoke-GitHubAPIPost -apiURI $uri -body $parameters -token $AuthToken
}

function Add-IssueComment {
function Add-GithubIssueComment {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -220,7 +230,7 @@ function Add-IssueComment {
}

# Will add labels to existing labels on the issue
function Add-IssueLabels {
function Add-GithubIssueLabels {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -242,7 +252,7 @@ function Add-IssueLabels {
}

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/labels"
$labelAdditions = SplitMembers -membersString $Labels
$labelAdditions = Split-GitHubAPIParameterMembers -membersString $Labels
$parameters = @{
labels = @($labelAdditions)
}
Expand All @@ -251,7 +261,7 @@ function Add-IssueLabels {
}

# Will add assignees to existing assignees on the issue
function Add-IssueAssignees {
function Add-GithubIssueAssignees {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -273,15 +283,15 @@ function Add-IssueAssignees {
}

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

return Invoke-GitHubAPIPost -apiURI $uri -body $parameters -token $AuthToken
}

function Request-PrReviewer {
function Request-GithubPrReviewers {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -295,43 +305,28 @@ function Request-PrReviewer {
$AuthToken
)

if (!$users -and !$teams)
{
throw "You must specify either Users or Teams."
exit 1
}

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/pulls/$PrNumber/requested_reviewers"
$parameters = @{}

if ($Users) {
if ($Users -is [array])
{
$parameters["reviewers"] = @($Users)
}
else {
$userAdditions = SplitMembers -membersString $Users
$parameters["reviewers"] = @($userAdditions)
}
if ($Users) {
$parameters = Set-GitHubAPIParameters -members $Users -parameterName "reviewers"` -parameters $parameters
}

if ($Teams) {
if ($Teams -is [array])
{
$parameters["team_reviewers"] = @($Teams)
}
else {
$teamAdditions = SplitMembers -membersString $Teams
$parameters["team_reviewers"] = @($teamAdditions)
}
if ($Teams) {
$parameters = Set-GitHubAPIParameters -members $Teams -parameterName "team_reviewers" -parameters $parameters
}

return Invoke-GitHubAPIPost -apiURI $uri -body $parameters -token AuthToken
if ($parameters.Count -gt 0) {
return Invoke-GitHubAPIPost -apiURI $uri -body $parameters -token $AuthToken
}
else {
throw "Did not fire request because of empty body."
}
}

# 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 Update-Issue {
function Update-GithubIssue {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -344,9 +339,7 @@ function Update-Issue {
[ValidateSet("open","closed")]
[string]$State,
[int]$Milestome,
[ValidateNotNullOrEmpty()]
$Labels,
[ValidateNotNullOrEmpty()]
$Assignees,
[Parameter(Mandatory = $true)]
$AuthToken
Expand All @@ -359,32 +352,25 @@ function Update-Issue {
if ($State) { $parameters["state"] = $State }
if ($Milestone) { $parameters["milestone"] = $Milestone }

if ($Labels) {
if ($Labels -is [array])
{
$parameters["labels"] = @($Labels)
}
else {
$labelAdditions = SplitMembers -membersString $Labels
$parameters["labels"] = @($labelAdditions)
}
if ($Labels) {
$parameters = Set-GitHubAPIParameters -members $Labels -parameterName "labels" `
-parameters $parameters -allowEmptyMembers $true
}

if ($Assignees) {
if ($Assignees -is [array])
{
$parameters["labels"] = @($Assignees)
}
else {
$assigneesAdditions = SplitMembers -membersString $Assignees
$parameters["assignees"] = @($assigneesAdditions)
}
if ($Assignees) {
$parameters = Set-GitHubAPIParameters -members $Assignees -parameterName "assignees" `
-parameters $parameters -allowEmptyMembers $true
}

return Invoke-GitHubAPIPatch -apiURI $uri -body $parameters -token $AuthToken
if ($parameters.Count -gt 0) {
return Invoke-GitHubAPIPatch -apiURI $uri -body $parameters -token $AuthToken
}
else {
throw "Did not fire request because of empty body."
}
}

function Delete-References {
function Delete-GithubSourceReferences {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
Expand All @@ -399,7 +385,7 @@ function Delete-References {

if ($Ref.Trim().Length -eq 0)
{
throw "You must supply a valid 'Ref' Parameter to 'Delete-Reference'."
throw "You must supply a valid 'Ref' Parameter to 'Delete-GithubSourceReferences'."
}

$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/git/refs/$Ref"
Expand Down
Loading

0 comments on commit ce4793b

Please sign in to comment.