Skip to content

Commit 06e2524

Browse files
joseartriveraHowardWolosky
authored andcommitted
Add support for Issue Events Github API (#64)
Added support for the [Github Issue Events](https://developer.github.com/v3/issues/events/) API.
1 parent 9034dad commit 06e2524

File tree

4 files changed

+308
-0
lines changed

4 files changed

+308
-0
lines changed

GitHubEvents.ps1

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubEvent
5+
{
6+
<#
7+
.DESCRIPTION
8+
Lists events for an issue, repository, or a single event
9+
10+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
11+
12+
.PARAMETER OwnerName
13+
Owner of the repository.
14+
If not supplied here, the DefaultOwnerName configuration property value will be used.
15+
16+
.PARAMETER RepositoryName
17+
Name of the repository.
18+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
19+
20+
.PARAMETER Uri
21+
Uri for the repository.
22+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
23+
them individually.
24+
25+
.PARAMETER EventID
26+
The ID of a specific event to get. If not supplied, will return back all events for this repository.
27+
28+
.PARAMETER Issue
29+
Issue number to get events for. If not supplied, will return back all events for this repository.
30+
31+
.PARAMETER AccessToken
32+
If provided, this will be used as the AccessToken for authentication with the
33+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
34+
35+
.PARAMETER NoStatus
36+
If this switch is specified, long-running commands will run on the main thread
37+
with no commandline status update. When not specified, those commands run in
38+
the background, enabling the command prompt to provide status information.
39+
If not supplied here, the DefaultNoStatus configuration property value will be used.
40+
41+
.EXAMPLE
42+
Get-GitHubEvent -OwnerName Powershell -RepositoryName PowerShellForGitHub
43+
44+
Get the events for the PowerShell\PowerShellForGitHub project.
45+
#>
46+
[CmdletBinding(
47+
SupportsShouldProcess,
48+
DefaultParametersetName='RepositoryElements')]
49+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
50+
param(
51+
[Parameter(Mandatory, ParameterSetName='RepositoryElements')]
52+
[Parameter(Mandatory, ParameterSetName='IssueElements')]
53+
[Parameter(Mandatory, ParameterSetName='EventElements')]
54+
[string] $OwnerName,
55+
56+
[Parameter(Mandatory, ParameterSetName='RepositoryElements')]
57+
[Parameter(Mandatory, ParameterSetName='IssueElements')]
58+
[Parameter(Mandatory, ParameterSetName='EventElements')]
59+
[string] $RepositoryName,
60+
61+
[Parameter(Mandatory, ParameterSetName='RepositoryUri')]
62+
[Parameter(Mandatory, ParameterSetName='IssueUri')]
63+
[Parameter(Mandatory, ParameterSetName='EventUri')]
64+
[string] $Uri,
65+
66+
[Parameter(Mandatory, ParameterSetName='EventUri')]
67+
[Parameter(Mandatory, ParameterSetName='EventElements')]
68+
[int] $EventID,
69+
70+
[Parameter(Mandatory, ParameterSetName='IssueUri')]
71+
[Parameter(Mandatory, ParameterSetName='IssueElements')]
72+
[int] $Issue,
73+
74+
[string] $AccessToken,
75+
76+
[switch] $NoStatus
77+
)
78+
79+
Write-InvocationLog
80+
81+
$elements = Resolve-RepositoryElements
82+
$OwnerName = $elements.ownerName
83+
$RepositoryName = $elements.repositoryName
84+
85+
$telemetryProperties = @{
86+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
87+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
88+
'ProvidedIssue' = $PSBoundParameters.ContainsKey('Issue')
89+
'ProvidedEvent' = $PSBoundParameters.ContainsKey('EventID')
90+
}
91+
92+
if ($PSBoundParameters.ContainsKey('EventID'))
93+
{
94+
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/events/$EventID"
95+
$description = "Getting event $EventID for $RepositoryName"
96+
}
97+
elseif ($PSBoundParameters.ContainsKey('Issue'))
98+
{
99+
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/$Issue/events"
100+
$description = "Getting events for issue $Issue in $RepositoryName"
101+
}
102+
else
103+
{
104+
$uriFragment = "repos/$OwnerName/$RepositoryName/issues/events"
105+
$description = "Getting events for $RepositoryName"
106+
}
107+
108+
$acceptHeaders = @(
109+
'application/vnd.github.starfox-preview+json',
110+
'application/vnd.github.sailer-v-preview+json',
111+
'application/vnd.github.symmetra-preview+json',
112+
'application/vnd.github.machine-man-preview')
113+
114+
$params = @{
115+
'UriFragment' = $uriFragment
116+
'Description' = $description
117+
'AccessToken' = $AccessToken
118+
'AcceptHeader' = $acceptHeaders -join ','
119+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
120+
'TelemetryProperties' = $telemetryProperties
121+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
122+
}
123+
124+
return Invoke-GHRestMethodMultipleResult @params
125+
}

PowerShellForGitHub.psd1

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'GitHubBranches.ps1',
2525
'GitHubCore.ps1',
2626
'GitHubComments.ps1',
27+
'GitHubEvents.ps1',
2728
'GitHubIssues.ps1',
2829
'GitHubLabels.ps1',
2930
'GitHubMiscellaneous.ps1',
@@ -51,6 +52,7 @@
5152
'Get-GitHubComment',
5253
'Get-GitHubConfiguration',
5354
'Get-GitHubEmoji',
55+
'Get-GitHubEvent',
5456
'Get-GitHubGitIgnore',
5557
'Get-GitHubIssue',
5658
'Get-GitHubIssueTimeline',

Tests/GitHubEvents.tests.ps1

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.Synopsis
6+
Tests for GitHubEvents.ps1 module
7+
#>
8+
9+
[String] $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
10+
. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1')
11+
Import-Module -Name $root -Force
12+
13+
function Initialize-AppVeyor
14+
{
15+
<#
16+
.SYNOPSIS
17+
Configures the tests to run with the authentication information stored in AppVeyor
18+
(if that information exists in the environment).
19+
20+
.DESCRIPTION
21+
Configures the tests to run with the authentication information stored in AppVeyor
22+
(if that information exists in the environment).
23+
24+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
25+
26+
.NOTES
27+
Internal-only helper method.
28+
29+
The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute,
30+
which can only be applied to functions.
31+
32+
We call this immediately after the declaration so that AppVeyor initialization can heppen
33+
(if applicable).
34+
35+
#>
36+
[CmdletBinding()]
37+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")]
38+
param()
39+
40+
if ($env:AppVeyor)
41+
{
42+
$secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force
43+
$cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString
44+
Set-GitHubAuthentication -Credential $cred
45+
46+
$script:ownerName = $env:avOwnerName
47+
$script:organizationName = $env:avOrganizationName
48+
49+
$message = @(
50+
'This run is executed in the AppVeyor environment.',
51+
'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.',
52+
'403 errors possible due to GitHub hourly limit for unauthenticated queries.',
53+
'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,',
54+
'and run tests on your machine first.')
55+
Write-Warning -Message ($message -join [Environment]::NewLine)
56+
}
57+
}
58+
59+
Initialize-AppVeyor
60+
61+
$accessTokenConfigured = Test-GitHubAuthenticationConfigured
62+
if (-not $accessTokenConfigured)
63+
{
64+
$message = @(
65+
'GitHub API Token not defined, some of the tests will be skipped.',
66+
'403 errors possible due to GitHub hourly limit for unauthenticated queries.')
67+
Write-Warning -Message ($message -join [Environment]::NewLine)
68+
}
69+
70+
# Backup the user's configuration before we begin, and ensure we're at a pure state before running
71+
# the tests. We'll restore it at the end.
72+
$configFile = New-TemporaryFile
73+
74+
try
75+
{
76+
Backup-GitHubConfiguration -Path $configFile
77+
Reset-GitHubConfiguration
78+
79+
if ($accessTokenConfigured)
80+
{
81+
Describe 'Getting events from repository' {
82+
$repositoryName = [Guid]::NewGuid()
83+
$null = New-GitHubRepository -RepositoryName $repositoryName
84+
85+
Context 'For getting events from a new repository' {
86+
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
87+
88+
It 'Should have no events' {
89+
$events.Count | Should be 0
90+
}
91+
}
92+
93+
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
94+
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed
95+
96+
Context 'For getting events from a repository' {
97+
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
98+
99+
It 'Should have an event from closing an issue' {
100+
$events.Count | Should be 1
101+
}
102+
}
103+
104+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
105+
}
106+
107+
Describe 'Getting events from an issue' {
108+
$repositoryName = [Guid]::NewGuid()
109+
$null = New-GitHubRepository -RepositoryName $repositoryName
110+
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
111+
112+
Context 'For getting events from a new issue' {
113+
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number)
114+
115+
It 'Should have no events' {
116+
$events.Count | Should be 0
117+
}
118+
}
119+
120+
Context 'For getting events from an issue' {
121+
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed
122+
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State open
123+
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
124+
125+
It 'Should have two events from closing and opening the issue' {
126+
$events.Count | Should be 2
127+
}
128+
}
129+
130+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
131+
}
132+
133+
Describe 'Getting an event directly' {
134+
$repositoryName = [Guid]::NewGuid()
135+
$null = New-GitHubRepository -RepositoryName $repositoryName
136+
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
137+
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed
138+
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State open
139+
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
140+
141+
Context 'For getting an event directly'{
142+
$singleEvent = Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -EventID $events[0].id
143+
144+
It 'Should have the correct event type'{
145+
$singleEvent.event | Should be 'reopened'
146+
}
147+
}
148+
149+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
150+
}
151+
}
152+
}
153+
catch
154+
{
155+
# Restore the user's configuration to its pre-test state
156+
Restore-GitHubConfiguration -Path $configFile
157+
}
158+

USAGE.md

+23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
* [Adding a new comment to an issue](#adding-a-new-comment-to-an-issue)
4343
* [Editing an existing comment](#editing-an-existing-comment)
4444
* [Removing a comment](#removing-a-comment)
45+
* [Events](#Events)
46+
* [Get events from a repository](#get-events-from-a-repository)
47+
* [Get events from an issue](#get-events-from-an-issue)
48+
* [Get a single event](#get-a-single-event])
4549
----------
4650

4751
## Logging
@@ -411,3 +415,22 @@ Set-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -Com
411415
```powershell
412416
Remove-GitHubComment -OwnerName Powershell -RepositoryName PowerShellForGitHub -CommentID 1
413417
```
418+
419+
----------
420+
421+
### Events
422+
423+
#### Get events from a repository
424+
```powershell
425+
Get-GitHubEvent -OwnerName Powershell -RepositoryName PowerShellForGitHub
426+
```
427+
428+
#### Get events from an issue
429+
```powershell
430+
Get-GitHubEvent -OwnerName Powershell -RepositoryName PowerShellForGitHub -Issue 1
431+
```
432+
433+
#### Get a single event
434+
```powershell
435+
Get-GitHubEvent -OwnerName Powershell -RepositoryName PowerShellForGitHub -EventID 1
436+
```

0 commit comments

Comments
 (0)