Skip to content

Commit 92c4aa8

Browse files
Add support for gists (#172)
This completes the required work to support the full set of API's around gists. It adds the following functions: * `Get-GitHubGist` * `Remove-GitHubGist` * `Copy-GitHubGist` (aka. `Fork-GitHubGist`) * `Add-GitHubGistStar` * `Remove-GitHubGistStar` * `Set-GitHubGistStar` (just a wrapper around `Add/Remove-GitHubGistStar` * `Test-GitHubGistStar` * `New-GitHubGist` * `Set-GitHubGist` * `Rename-GitHubGistFile` (exposed helper function) * `Remove-GitHubGistFile` (exposed helper function) * `Set-GitHubGistFile` (exposed helper function, also known as `Add-GitHubGistFile`) * `Get-GitHubGistComment` * `Set-GitHubGistComment` * `New-GitHubGistComment` * `Remove-GitHubGistComment` This also adds formatters for all newly introduced types: `GitHub.Gist`, `GitHub.GistCommit`, `GitHub.GistDetail`, and `GitHub.GistFork`. Positional Binding has been set as `false`, and `Position` attributes added to the functions' mandatory parameters. References: [GitHub Gist](https://developer.github.com/v3/gists/) [GitHub Gist Comments](https://developer.github.com/v3/gists/comments/) Fixes #32
1 parent 356af2f commit 92c4aa8

9 files changed

+4414
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<!--================== GitHub.GistComment Type View =================-->
5+
<View>
6+
<Name>GitHub.GistComment</Name>
7+
<ViewSelectedBy>
8+
<TypeName>GitHub.GistComment</TypeName>
9+
</ViewSelectedBy>
10+
<TableControl>
11+
<TableHeaders>
12+
<TableColumnHeader>
13+
<Label>ID</Label>
14+
</TableColumnHeader>
15+
<TableColumnHeader>
16+
<Label>User</Label>
17+
</TableColumnHeader>
18+
<TableColumnHeader>
19+
<Label>Comment</Label>
20+
</TableColumnHeader>
21+
<TableColumnHeader>
22+
<Label>Created</Label>
23+
</TableColumnHeader>
24+
<TableColumnHeader>
25+
<Label>Updated</Label>
26+
</TableColumnHeader>
27+
</TableHeaders>
28+
<TableRowEntries>
29+
<TableRowEntry>
30+
<TableColumnItems>
31+
<TableColumnItem>
32+
<PropertyName>id</PropertyName>
33+
</TableColumnItem>
34+
<TableColumnItem>
35+
<ScriptBlock>
36+
$_.user.login
37+
</ScriptBlock>
38+
</TableColumnItem>
39+
<TableColumnItem>
40+
<PropertyName>body</PropertyName>
41+
</TableColumnItem>
42+
<TableColumnItem>
43+
<PropertyName>created_at</PropertyName>
44+
</TableColumnItem>
45+
<TableColumnItem>
46+
<PropertyName>updated_at</PropertyName>
47+
</TableColumnItem>
48+
</TableColumnItems>
49+
</TableRowEntry>
50+
</TableRowEntries>
51+
</TableControl>
52+
</View>
53+
</ViewDefinitions>
54+
</Configuration>

Formatters/GitHubGists.Format.ps1xml

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<!--================== GitHub.Gist Type View =================-->
5+
<View>
6+
<Name>GitHub.Gist</Name>
7+
<ViewSelectedBy>
8+
<TypeName>GitHub.Gist</TypeName>
9+
</ViewSelectedBy>
10+
<TableControl>
11+
<TableHeaders>
12+
<TableColumnHeader>
13+
<Label>ID</Label>
14+
</TableColumnHeader>
15+
<TableColumnHeader>
16+
<Label>User</Label>
17+
</TableColumnHeader>
18+
<TableColumnHeader>
19+
<Label>Description</Label>
20+
</TableColumnHeader>
21+
<TableColumnHeader>
22+
<Label>Public</Label>
23+
</TableColumnHeader>
24+
<TableColumnHeader>
25+
<Label>Files</Label>
26+
</TableColumnHeader>
27+
<TableColumnHeader>
28+
<Label>Created</Label>
29+
</TableColumnHeader>
30+
<TableColumnHeader>
31+
<Label>Updated</Label>
32+
</TableColumnHeader>
33+
</TableHeaders>
34+
<TableRowEntries>
35+
<TableRowEntry>
36+
<TableColumnItems>
37+
<TableColumnItem>
38+
<PropertyName>id</PropertyName>
39+
</TableColumnItem>
40+
<TableColumnItem>
41+
<ScriptBlock>
42+
$_.owner.login
43+
</ScriptBlock>
44+
</TableColumnItem>
45+
<TableColumnItem>
46+
<PropertyName>description</PropertyName>
47+
</TableColumnItem>
48+
<TableColumnItem>
49+
<PropertyName>public</PropertyName>
50+
</TableColumnItem>
51+
<TableColumnItem>
52+
<ScriptBlock>
53+
($_.files |
54+
Get-Member -MemberType NoteProperty |
55+
Select-Object -ExpandProperty Name) -join ', '
56+
</ScriptBlock>
57+
</TableColumnItem>
58+
<TableColumnItem>
59+
<PropertyName>created_at</PropertyName>
60+
</TableColumnItem>
61+
<TableColumnItem>
62+
<PropertyName>updated_at</PropertyName>
63+
</TableColumnItem>
64+
</TableColumnItems>
65+
</TableRowEntry>
66+
</TableRowEntries>
67+
</TableControl>
68+
</View>
69+
<!--=============== GitHub.GistCommit Type View ===============-->
70+
<View>
71+
<Name>GitHub.GistCommit</Name>
72+
<ViewSelectedBy>
73+
<TypeName>GitHub.GistCommit</TypeName>
74+
</ViewSelectedBy>
75+
<TableControl>
76+
<TableHeaders>
77+
<TableColumnHeader>
78+
<Label>ID</Label>
79+
</TableColumnHeader>
80+
<TableColumnHeader>
81+
<Label>Sha</Label>
82+
</TableColumnHeader>
83+
<TableColumnHeader>
84+
<Label>User</Label>
85+
</TableColumnHeader>
86+
<TableColumnHeader>
87+
<Label>Committed</Label>
88+
</TableColumnHeader>
89+
</TableHeaders>
90+
<TableRowEntries>
91+
<TableRowEntry>
92+
<TableColumnItems>
93+
<TableColumnItem>
94+
<PropertyName>GistId</PropertyName>
95+
</TableColumnItem>
96+
<TableColumnItem>
97+
<PropertyName>version</PropertyName>
98+
</TableColumnItem>
99+
<TableColumnItem>
100+
<ScriptBlock>
101+
($_.user.login)
102+
</ScriptBlock>
103+
</TableColumnItem>
104+
<TableColumnItem>
105+
<PropertyName>committed_at</PropertyName>
106+
</TableColumnItem>
107+
</TableColumnItems>
108+
</TableRowEntry>
109+
</TableRowEntries>
110+
</TableControl>
111+
</View>
112+
<!--================== GitHub.GistDetail Type View =================-->
113+
<View>
114+
<Name>GitHub.GistDetail</Name>
115+
<ViewSelectedBy>
116+
<TypeName>GitHub.GistDetail</TypeName>
117+
</ViewSelectedBy>
118+
<TableControl>
119+
<TableHeaders>
120+
<TableColumnHeader>
121+
<Label>ID</Label>
122+
</TableColumnHeader>
123+
<TableColumnHeader>
124+
<Label>owner</Label>
125+
</TableColumnHeader>
126+
<TableColumnHeader>
127+
<Label>Description</Label>
128+
</TableColumnHeader>
129+
<TableColumnHeader>
130+
<Label>Public</Label>
131+
</TableColumnHeader>
132+
<TableColumnHeader>
133+
<Label>Files</Label>
134+
</TableColumnHeader>
135+
<TableColumnHeader>
136+
<Label>Forks</Label>
137+
</TableColumnHeader>
138+
<TableColumnHeader>
139+
<Label>Created</Label>
140+
</TableColumnHeader>
141+
<TableColumnHeader>
142+
<Label>Updated</Label>
143+
</TableColumnHeader>
144+
</TableHeaders>
145+
<TableRowEntries>
146+
<TableRowEntry>
147+
<TableColumnItems>
148+
<TableColumnItem>
149+
<PropertyName>id</PropertyName>
150+
</TableColumnItem>
151+
<TableColumnItem>
152+
<ScriptBlock>
153+
($_.owner.login)
154+
</ScriptBlock>
155+
</TableColumnItem>
156+
<TableColumnItem>
157+
<PropertyName>description</PropertyName>
158+
</TableColumnItem>
159+
<TableColumnItem>
160+
<PropertyName>public</PropertyName>
161+
</TableColumnItem>
162+
<TableColumnItem>
163+
<ScriptBlock>
164+
($_.files |
165+
Get-Member -MemberType NoteProperty |
166+
Select-Object -ExpandProperty Name) -join ', '
167+
</ScriptBlock>
168+
</TableColumnItem>
169+
<TableColumnItem>
170+
<ScriptBlock>
171+
($_.forks.Count)
172+
</ScriptBlock>
173+
</TableColumnItem>
174+
<TableColumnItem>
175+
<PropertyName>created_at</PropertyName>
176+
</TableColumnItem>
177+
<TableColumnItem>
178+
<PropertyName>updated_at</PropertyName>
179+
</TableColumnItem>
180+
</TableColumnItems>
181+
</TableRowEntry>
182+
</TableRowEntries>
183+
</TableControl>
184+
</View>
185+
<!--=============== GitHub.GistFork Type View ===============-->
186+
<View>
187+
<Name>GitHub.GistFork</Name>
188+
<ViewSelectedBy>
189+
<TypeName>GitHub.GistFork</TypeName>
190+
</ViewSelectedBy>
191+
<TableControl>
192+
<TableHeaders>
193+
<TableColumnHeader>
194+
<Label>ID</Label>
195+
</TableColumnHeader>
196+
<TableColumnHeader>
197+
<Label>User</Label>
198+
</TableColumnHeader>
199+
<TableColumnHeader>
200+
<Label>Created</Label>
201+
</TableColumnHeader>
202+
<TableColumnHeader>
203+
<Label>Updated</Label>
204+
</TableColumnHeader>
205+
</TableHeaders>
206+
<TableRowEntries>
207+
<TableRowEntry>
208+
<TableColumnItems>
209+
<TableColumnItem>
210+
<PropertyName>id</PropertyName>
211+
</TableColumnItem>
212+
<TableColumnItem>
213+
<ScriptBlock>
214+
# There appears to be a bug in the GitHub API.
215+
# Documentation says that the property should alway be "user",
216+
# but it switches between "owner" and "user" depending on if it's a property
217+
# of a gist, or the direct result from a gist forks query.
218+
# https://github.community/t/gist-api-v3-documentation-incorrect-for-forks/122545
219+
if ($null -eq $_.user)
220+
{
221+
$_.owner.login
222+
}
223+
else
224+
{
225+
$_.user.login
226+
}
227+
</ScriptBlock>
228+
</TableColumnItem>
229+
<TableColumnItem>
230+
<PropertyName>created_at</PropertyName>
231+
</TableColumnItem>
232+
<TableColumnItem>
233+
<PropertyName>updated_at</PropertyName>
234+
</TableColumnItem>
235+
</TableColumnItems>
236+
</TableRowEntry>
237+
</TableRowEntries>
238+
</TableControl>
239+
</View>
240+
</ViewDefinitions>
241+
</Configuration>

GitHubCore.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ function Invoke-GHRestMethod
329329
$finalResult = $finalResult | ConvertFrom-Json
330330
}
331331
}
332+
catch [InvalidOperationException]
333+
{
334+
# In some cases, the returned data might have two different keys of the same characters
335+
# but different casing (this can happen with gists with two files named 'a.txt' and 'A.txt').
336+
# PowerShell 6 introduced the -AsHashtable switch to work around this issue, but this
337+
# module wants to be compatible down to PowerShell 4, so we're unable to use that feature.
338+
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
339+
$finalResult = $finalResult
340+
}
332341
catch [ArgumentException]
333342
{
334343
# The content must not be JSON (which is a legitimate situation).

0 commit comments

Comments
 (0)