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

Add functions Get-GitHubIssue and Get-GitHubComment #35

Merged
merged 11 commits into from Sep 25, 2017
Binary file added 0
Binary file not shown.
129 changes: 129 additions & 0 deletions Functions/Public/Get-GitHubComment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
function Get-GitHubComment {
<#
.SYNOPSIS
Gets GitHub issue comments.

.DESCRIPTION
Gets a single comment, or all comments on an issue, or all comments in a
repository, subject to filtering parameters.

.PARAMETER Owner
The GitHub username of the account or organization that owns the GitHub repository
specified in the -Repository parameter.

.PARAMETER Repository
The name of the GitHub repository containing the issue.

.PARAMETER All
Retrieve all comments in the GitHub repository specified by the -Owner and
-Repository parameters.

.PARAMETER IssueNumber
The number of the issue to retrieve.

.PARAMETER CommentId
The id of the comment to retrieve. Requires -Issue to be present.

.PARAMETER Page
The page number of the results to return. Default: 1
Note: The GitHub documentation of pagination warns to always rely on the
links provided in the response headers, rather than attempting to construct
the page URLs by hand. Unfortunately, as of PowerShell 5.1, Invoke-RestApi
does not provide access to the response headers.
https://developer.github.com/v3/guides/traversing-with-pagination/

.PARAMETER Sort
What to sort results by. Valid values: created, updated.
Default: created.

.PARAMETER Direction
The direction to sort. Valid values: asc, desc. Default: desc

.PARAMETER Since
Limit the results to issues updated at or after the specified time. The time
is specified in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ

.EXAMPLE
# Retrieve all comments in the repository Mary/WebApps:
Get-GitHubComment -Owner Mary -Repository WebApps -All

.EXAMPLE
# Retrieve all comments on Issue #42 in the repository Mary/WebApps:
Get-GitHubComment -Owner Mary -Repository WebApps -Issue 42

.EXAMPLE
# Retrieve all comments in the repository Mary/WebApps in 2017 or later.
Get-GitHubComment -Owner Mary -Repository WebApps -Since 2017-01-01T00:00:00Z

.EXAMPLE
# Retrieve comment #3 on Issue #42 in the repository Mary/WebApps:
Get-GitHubComment -Owner Mary -Repository WebApps -Issue 42 -Comment 3

#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias('User')]
[string] $Owner
, [Parameter(Mandatory = $true)]
[string] $Repository
, [Parameter(Mandatory = $true, ParameterSetName = 'InRepo')]
[switch] $All
, [Parameter(Mandatory = $true, ParameterSetName = 'InIssue')]
[ValidateRange(1, [int]::MaxValue)]
[int] $IssueNumber
, [Parameter(Mandatory = $true, ParameterSetName = 'Single')]
[ValidateRange(1, [int]::MaxValue)]
[int] $CommentId
, [Parameter(Mandatory = $false, ParameterSetName = 'InRepo')]
[Parameter(Mandatory = $false, ParameterSetName = 'InIssue')]
[ValidateRange(1, [int]::MaxValue)]
[int] $Page
, [Parameter(Mandatory = $false, ParameterSetName = 'InRepo')]
[ValidateSet('created', 'updated')]
[string] $Sort
, [Parameter(Mandatory = $false, ParameterSetName = 'InRepo')]
[ValidateSet('asc', 'desc')]
[string] $Direction
, [Parameter(Mandatory = $false, ParameterSetName = 'InRepo')]
[Parameter(Mandatory = $false, ParameterSetName = 'InIssue')]
[string] $Since
)

$restMethod = 'repos/{0}/{1}/issues' -f $Owner, $Repository
if ($All) {
$restMethod += '/comments'
} elseif ($IssueNumber) {
$restMethod += '/{0}/comments' -f $IssueNumber
} elseif ($CommentId) {
$restMethod += '/comments/{0}' -f $CommentId
}

$queryParameters = @()
if ($Page) {
$queryParameters += "page=$Page"
}

if ($Sort) {
$queryParameters += "sort=$Sort"
}

if ($Direction) {
$queryParameters += "direction=$Direction"
}

if ($Since) {
$queryParameters += "since=$Since"
}

if ($queryParameters) {
$restMethod += "?" + ($queryParameters -join '&')
}

$apiCall = @{
Method = 'Get';
RestMethod = $restMethod
}

Invoke-GitHubApi @apiCall
}
184 changes: 184 additions & 0 deletions Functions/Public/Get-GitHubIssue.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
function Get-GitHubIssue {
<#
.SYNOPSIS
Gets GitHub issues.

.DESCRIPTION
This command retrieves GitHub issues either for the authenticated user or from
a specified repository.

.PARAMETER All
Retrieve "all issues assigned to the authenticated user across all visible
repositories including owned repositories, member repositories, and organization
repositories" (https://developer.github.com/v3/issues/).

.PARAMETER ForUser
Retrieve issues assigned to the authenticated user in those repos owned by
the authenticated user, or of which the authenticated user is a member.

.PARAMETER Organization
Retrieve issues assigned to the authenticated user in repos belonging to the
specified organization.

.PARAMETER Owner
The GitHub username of the account or organization that owns the GitHub repository
specified in the -Repository parameter.

.PARAMETER Repository
Retrieve all open issues from the GitHub repository with the specified name,
owned by the GitHub user specified by -Owner.

.PARAMETER Number
Retrieve the single issue with the specified number from the repo specified
by -Owner and -Repository.

.PARAMETER Page
The page number of the results to return. Default: 1
Note: The GitHub documentation of pagination warns to always rely on the
links provided in the response headers, rather than attempting to construct
the page URLs by hand. Unfortunately, as of PowerShell 5.1, Invoke-RestApi
does not provide access to the response headers.
https://developer.github.com/v3/guides/traversing-with-pagination/

.PARAMETER Filter
Indicates which sorts of issues to return. Valid values:
* assigned: Issues assigned to the authenticated user.
* created: Issues created by the authenticated user.
* mentioned: Issues mentioning the authenticated user.
* subscribed: Issues for which the authenticated user has subscribed to updates.
* all: All issues the authenticated user can see, regardless of participation or creation.
Default: assigned

.PARAMETER State
Limit the results to issues with the specified state. Valid values: open,
closed, all. Default: open.

.PARAMETER Labels
Limit the results to issues with all of of the specified, comma-separated
list of labels.

.PARAMETER Sort
What to sort results by. Valid values: created, updated, comments.
Default: created.

.PARAMETER Direction
The direction to sort. Valid values: asc, desc. Default: desc

.PARAMETER Since
Limit the results to issues updated at or after the specified time. The time
is specified in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ

.EXAMPLE
# Retrieve all open issues for the authenticated user, including issues from
# owned, member, and organization repositories:
Get-GitHubIssue -All

.EXAMPLE
# Retrieve open issues for the authenticated user, including only issues from
# owned and member repositories.
Get-GitHubIssue -ForUser

.EXAMPLE
# Retrieve open issues assigned to the authenticated user in repos owned by
# the organization ExampleOrg:
Get-GitHubIssue -Organization ExampleOrg

.EXAMPLE
# Retrieve all open issues in the repository Mary/WebApps:
Get-GitHubIssue -Owner Mary -Repository WebApps

.EXAMPLE
# Retrieve all issues (both open and closed) in the repository Mary/WebApps:
Get-GitHubIssue -Owner Mary -Repository WebApps -State all

#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'All')]
[switch] $All
, [Parameter(Mandatory = $true, ParameterSetName = 'ForUser')]
[switch] $ForUser
, [Parameter(Mandatory = $true, ParameterSetName = 'Organization')]
[string] $Organization
, [Parameter(Mandatory = $true, ParameterSetName = 'Repository')]
[Alias('User')]
[string] $Owner
, [Parameter(Mandatory = $true, ParameterSetName = 'Repository')]
[string] $Repository
, [Parameter(Mandatory = $false, ParameterSetName = 'Repository')]
[ValidateRange(1, [int]::MaxValue)]
[int] $Number
, [Parameter()]
[ValidateRange(1, [int]::MaxValue)]
[int] $Page
, [Parameter()]
[ValidateSet('assigned', 'created', 'mentioned', 'subscribed', 'all')]
[string] $Filter
, [Parameter()]
[ValidateSet('open', 'closed', 'all')]
[string] $State
, [Parameter()]
[string[]] $Labels
, [Parameter()]
[ValidateSet('created', 'updated', 'comments')]
[string] $Sort
, [Parameter()]
[ValidateSet('asc', 'desc')]
[string] $Direction
, [Parameter()]
[string] $Since
)

if ($Repository) {
$restMethod = 'repos/{0}/{1}/issues' -f $Owner, $Repository
if ($Number -gt 0) {
$restMethod += ("/{0}" -f $Number)
}
} elseif ($Organization) {
$restMethod = 'orgs/{0}/issues' -f $Organization
} elseif ($ForUser) {
$restMethod = 'user/issues'
} else {
$restMethod = 'issues'
}

$queryParameters = @()
if ($Page) {
$queryParameters += "page=$Page"
}

if ($Filter) {
$queryParameters += "filter=$Filter"
}

if ($State) {
$queryParameters += "state=$State"
}

if ($Labels) {
$queryParameters += "labels=" + ($Labels -join ',')
}

if ($Sort) {
$queryParameters += "sort=$Sort"
}

if ($Direction) {
$queryParameters += "direction=$Direction"
}

if ($Since) {
$queryParameters += "since=$Since"
}

if ($queryParameters) {
$restMethod += "?" + ($queryParameters -join '&')
}

$apiCall = @{
Method = 'Get';
RestMethod = $restMethod
}

Invoke-GitHubApi @apiCall
}
4 changes: 4 additions & 0 deletions PSGitHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ FunctionsToExport = @(
'Get-GitHubMilestone',
'Get-GitHubAssignee',
'Test-GitHubAssignee',
'Get-GitHubIssue',

### GitHub Comment commands
'Get-GitHubComment',

### GitHub Release commands
'Get-GitHubRelease',
Expand Down