Skip to content

Commit cb778c0

Browse files
committed
Add support for gists
Adds full support for all gist related API's (core and comments) as described in https://developer.github.com/v3/gists/ and https://developer.github.com/v3/gists/comments/. * No tests yet. * Test-GitHubGistStarred doesn't appear to be working right, despite being coded against the proper API spec.
1 parent 17f6122 commit cb778c0

File tree

4 files changed

+1233
-0
lines changed

4 files changed

+1233
-0
lines changed

GitHubCore.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ function Invoke-GHRestMethod
353353
{
354354
$finalResult = $finalResult | ConvertFrom-Json
355355
}
356+
catch [InvalidOperationException]
357+
{
358+
# In some cases, the returned data might have two different keys of the same characters
359+
# but different casing (this can happen with gists with two files named 'a.txt' and 'A.txt').
360+
# PowerShell 6 introduced the -AsHashtable switch to work around this issue, but this
361+
# module wants to be compatible down to PowerShell 4, so we're unable to use that feature.
362+
Write-Log -Message 'The returned object likely contains keys that differ only in casing. Unable to convert to an object. Returning the raw JSON as a fallback.' -Level Warning
363+
$finalResult = $finalResult
364+
}
356365
catch [ArgumentException]
357366
{
358367
# The content must not be JSON (which is a legitimate situation).

GitHubGistComments.ps1

+323
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubGistComment
5+
{
6+
<#
7+
.SYNOPSIS
8+
Retrieves comments for a specific gist from GitHub.
9+
10+
.DESCRIPTION
11+
Retrieves comments for a specific gist from GitHub.
12+
13+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
14+
15+
.PARAMETER Id
16+
The ID of the specific gist that you wish to retrieve the comments for.
17+
18+
.PARAMETER CommentId
19+
The ID of the specific comment on the gist that you wish to retrieve.
20+
21+
.PARAMETER MediaType
22+
The format in which the API will return the body of the comment.
23+
24+
Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type.
25+
Text - Return a text only representation of the markdown body. Response will include body_text.
26+
Html - Return HTML rendered from the body's markdown. Response will include body_html.
27+
Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html.
28+
29+
.PARAMETER AccessToken
30+
If provided, this will be used as the AccessToken for authentication with the
31+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
32+
33+
.PARAMETER NoStatus
34+
If this switch is specified, long-running commands will run on the main thread
35+
with no commandline status update. When not specified, those commands run in
36+
the background, enabling the command prompt to provide status information.
37+
If not supplied here, the DefaultNoStatus configuration property value will be used.
38+
39+
.EXAMPLE
40+
Get-GitHubGistComment -Id 6cad326836d38bd3a7ae
41+
42+
Gets all comments on octocat's "hello_world.rb" gist.
43+
44+
.EXAMPLE
45+
Get-GitHubGistComment -Id 6cad326836d38bd3a7ae -CommentId 1507813
46+
47+
Gets comment 1507813 from octocat's "hello_world.rb" gist.
48+
#>
49+
[CmdletBinding(SupportsShouldProcess)]
50+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
51+
param(
52+
[Parameter(Mandatory)]
53+
[Alias('GistId')]
54+
[string] $Id,
55+
56+
[string] $CommentId,
57+
58+
[ValidateSet('Raw', 'Text', 'Html', 'Full')]
59+
[string] $MediaType = 'Raw',
60+
61+
[string] $AccessToken,
62+
63+
[switch] $NoStatus
64+
)
65+
66+
Write-InvocationLog -Invocation $MyInvocation
67+
68+
$telemetryProperties = @{}
69+
70+
$uriFragment = [String]::Empty
71+
$description = [String]::Empty
72+
73+
if ([String]::IsNullOrWhiteSpace($CommentId))
74+
{
75+
$uriFragment = "gists/$Id/comments"
76+
$description = "Getting comments for gist $Id"
77+
}
78+
else
79+
{
80+
$telemetryProperties['SpecifiedCommentId'] = $true
81+
82+
$uriFragment = "gists/$Id/comments/$CommentId"
83+
$description = "Getting comment $CommentId for gist $Id"
84+
}
85+
86+
$params = @{
87+
'UriFragment' = $uriFragment
88+
'Description' = $description
89+
'AccessToken' = $AccessToken
90+
'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AsJson)
91+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
92+
'TelemetryProperties' = $telemetryProperties
93+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
94+
}
95+
96+
$result = Invoke-GHRestMethodMultipleResult @params
97+
98+
return $result
99+
}
100+
101+
function Remove-GitHubGistComment
102+
{
103+
<#
104+
.SYNOPSIS
105+
Removes/deletes a comment from a gist on GitHub.
106+
107+
.DESCRIPTION
108+
Removes/deletes a comment from a gist on GitHub.
109+
110+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
111+
112+
.PARAMETER Id
113+
The ID of the specific gist that you wish to remove the comment from.
114+
115+
.PARAMETER CommentId
116+
The ID of the comment to remove from the gist.
117+
118+
.PARAMETER AccessToken
119+
If provided, this will be used as the AccessToken for authentication with the
120+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
121+
122+
.PARAMETER NoStatus
123+
If this switch is specified, long-running commands will run on the main thread
124+
with no commandline status update. When not specified, those commands run in
125+
the background, enabling the command prompt to provide status information.
126+
If not supplied here, the DefaultNoStatus configuration property value will be used.
127+
128+
.EXAMPLE
129+
Remove-GitHubGist -Id 6cad326836d38bd3a7ae
130+
131+
Removes octocat's "hello_world.rb" gist (assuming you have permission).
132+
133+
.EXAMPLE
134+
Remove-GitHubGist -Id 6cad326836d38bd3a7ae -Confirm:$false
135+
136+
Removes octocat's "hello_world.rb" gist (assuming you have permission).
137+
Will not prompt for confirmation, as -Confirm:$false was specified.
138+
#>
139+
[CmdletBinding(
140+
SupportsShouldProcess,
141+
ConfirmImpact="High")]
142+
[Alias('Delete-GitHubGist')]
143+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
144+
param(
145+
[Parameter(Mandatory)]
146+
[Alias('GistId')]
147+
[ValidateNotNullOrEmpty()]
148+
[string] $Id,
149+
150+
[Parameter(Mandatory)]
151+
[ValidateNotNullOrEmpty()]
152+
[string] $CommentId,
153+
154+
[string] $AccessToken,
155+
156+
[switch] $NoStatus
157+
)
158+
159+
Write-InvocationLog -Invocation $MyInvocation
160+
161+
if ($PSCmdlet.ShouldProcess($CommentId, "Delete comment from gist $Id"))
162+
{
163+
$telemetryProperties = @{}
164+
$params = @{
165+
'UriFragment' = "gists/$Id/comments/$CommentId"
166+
'Method' = 'Delete'
167+
'Description' = "Removing comment $CommentId from gist $Id"
168+
'AccessToken' = $AccessToken
169+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
170+
'TelemetryProperties' = $telemetryProperties
171+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
172+
}
173+
174+
return Invoke-GHRestMethod @params
175+
}
176+
}
177+
178+
function New-GitHubGistComment
179+
{
180+
<#
181+
.SYNOPSIS
182+
Creates a new comment on the specified gist from GitHub.
183+
184+
.DESCRIPTION
185+
Creates a new comment on the specified gist from GitHub.
186+
187+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
188+
189+
.PARAMETER Id
190+
The ID of the specific gist that you wish to add the comment to.
191+
192+
.PARAMETER Comment
193+
The text of the comment that you wish to leave on the gist.
194+
195+
.PARAMETER AccessToken
196+
If provided, this will be used as the AccessToken for authentication with the
197+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
198+
199+
.PARAMETER NoStatus
200+
If this switch is specified, long-running commands will run on the main thread
201+
with no commandline status update. When not specified, those commands run in
202+
the background, enabling the command prompt to provide status information.
203+
If not supplied here, the DefaultNoStatus configuration property value will be used.
204+
205+
.EXAMPLE
206+
New-GitHubGistComment -Id 6cad326836d38bd3a7ae -Comment 'Hello World'
207+
208+
Adds a new comment of "Hello World" to octocat's "hello_world.rb" gist.
209+
#>
210+
[CmdletBinding(SupportsShouldProcess)]
211+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
212+
param(
213+
[Parameter(Mandatory)]
214+
[Alias('GistId')]
215+
[ValidateNotNullOrEmpty()]
216+
[string] $Id,
217+
218+
[Parameter(Mandatory)]
219+
[ValidateNotNullOrEmpty()]
220+
[string] $Comment,
221+
222+
[string] $AccessToken,
223+
224+
[switch] $NoStatus
225+
)
226+
227+
Write-InvocationLog -Invocation $MyInvocation
228+
229+
$hashBody = @{
230+
'body' = $Comment
231+
}
232+
233+
$telemetryProperties = @{}
234+
$params = @{
235+
'UriFragment' = "gists/$Id/comments"
236+
'Body' = (ConvertTo-Json -InputObject $hashBody)
237+
'Method' = 'Post'
238+
'Description' = "Creating new comment on gist $Id"
239+
'AccessToken' = $AccessToken
240+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
241+
'TelemetryProperties' = $telemetryProperties
242+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
243+
}
244+
245+
return Invoke-GHRestMethod @params
246+
}
247+
248+
function Set-GitHubGistComment
249+
{
250+
<#
251+
.SYNOPSIS
252+
Edits a comment on the specified gist from GitHub.
253+
254+
.DESCRIPTION
255+
Edits a comment on the specified gist from GitHub.
256+
257+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
258+
259+
.PARAMETER Id
260+
The ID of the gist that the comment is on.
261+
262+
.PARAMETER CommentId
263+
The ID of the comment that you wish to edit.
264+
265+
.PARAMETER Comment
266+
The new text of the comment that you wish to leave on the gist.
267+
268+
.PARAMETER AccessToken
269+
If provided, this will be used as the AccessToken for authentication with the
270+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
271+
272+
.PARAMETER NoStatus
273+
If this switch is specified, long-running commands will run on the main thread
274+
with no commandline status update. When not specified, those commands run in
275+
the background, enabling the command prompt to provide status information.
276+
If not supplied here, the DefaultNoStatus configuration property value will be used.
277+
278+
.EXAMPLE
279+
New-GitHubGistComment -Id 6cad326836d38bd3a7ae -Comment 'Hello World'
280+
281+
Adds a new comment of "Hello World" to octocat's "hello_world.rb" gist.
282+
#>
283+
[CmdletBinding(SupportsShouldProcess)]
284+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
285+
param(
286+
[Parameter(Mandatory)]
287+
[Alias('GistId')]
288+
[ValidateNotNullOrEmpty()]
289+
[string] $Id,
290+
291+
[Parameter(Mandatory)]
292+
[ValidateNotNullOrEmpty()]
293+
[string] $CommentId,
294+
295+
[Parameter(Mandatory)]
296+
[ValidateNotNullOrEmpty()]
297+
[string] $Comment,
298+
299+
[string] $AccessToken,
300+
301+
[switch] $NoStatus
302+
)
303+
304+
Write-InvocationLog -Invocation $MyInvocation
305+
306+
$hashBody = @{
307+
'body' = $Comment
308+
}
309+
310+
$telemetryProperties = @{}
311+
$params = @{
312+
'UriFragment' = "gists/$Id/comments/$CommentId"
313+
'Body' = (ConvertTo-Json -InputObject $hashBody)
314+
'Method' = 'Patch'
315+
'Description' = "Creating new comment on gist $Id"
316+
'AccessToken' = $AccessToken
317+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
318+
'TelemetryProperties' = $telemetryProperties
319+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
320+
}
321+
322+
return Invoke-GHRestMethod @params
323+
}

0 commit comments

Comments
 (0)