Skip to content

Commit e8dca69

Browse files
[Feature] User - GPG keys - Use the REST API to manage GPG keys of authenticated users (#53)
- Fixes #48 - Add-GitHubUserGpgKey - Remove-GitHubUserGpgKey - Get-GitHubUserGpgKey
1 parent 4fcdb6a commit e8dca69

File tree

7 files changed

+263
-2
lines changed

7 files changed

+263
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
filter Get-GitHubUserGpgKeyForUser {
2+
<#
3+
.SYNOPSIS
4+
List GPG keys for a user
5+
6+
.DESCRIPTION
7+
Lists the GPG keys for a user. This information is accessible by anyone.
8+
9+
.EXAMPLE
10+
Get-GitHubUserGpgKeyForUser -Username 'octocat'
11+
12+
Gets all GPG keys for the 'octocat' user.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-a-user
16+
17+
#>
18+
[OutputType([pscustomobject])]
19+
[CmdletBinding()]
20+
param (
21+
# The handle for the GitHub user account.
22+
[Parameter(
23+
Mandatory,
24+
ValueFromPipelineByPropertyName
25+
)]
26+
[string] $Username,
27+
28+
# The number of results per page (max 100).
29+
[Parameter()]
30+
[int] $PerPage = 30
31+
)
32+
33+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
34+
Remove-HashtableEntries -Hashtable $body -RemoveNames 'username'
35+
36+
$inputObject = @{
37+
APIEndpoint = "/users/$Username/gpg_keys"
38+
Method = 'GET'
39+
Body = $body
40+
}
41+
42+
(Invoke-GitHubAPI @inputObject).Response
43+
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
filter Get-GitHubUserMyGpgKey {
2+
<#
3+
.SYNOPSIS
4+
List GPG keys for the authenticated user
5+
6+
.DESCRIPTION
7+
Lists the current user's GPG keys.
8+
Requires that you are authenticated via Basic Auth or via OAuth with at least `read:gpg_key` [scope](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/).
9+
10+
.EXAMPLE
11+
Get-GitHubUserMyGpgKey
12+
13+
Gets all GPG keys for the authenticated user.
14+
15+
.NOTES
16+
https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user
17+
18+
#>
19+
[OutputType([pscustomobject])]
20+
[CmdletBinding()]
21+
param (
22+
# The number of results per page (max 100).
23+
[Parameter()]
24+
[int] $PerPage = 30
25+
)
26+
27+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
28+
29+
$inputObject = @{
30+
APIEndpoint = '/user/gpg_keys'
31+
Method = 'GET'
32+
Body = $body
33+
}
34+
35+
(Invoke-GitHubAPI @inputObject).Response
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
filter Get-GitHubUserMyGpgKeyById {
2+
<#
3+
.SYNOPSIS
4+
Get a GPG key for the authenticated user
5+
6+
.DESCRIPTION
7+
View extended details for a single GPG key.
8+
Requires that you are authenticated via Basic Auth or via OAuth with at least `read:gpg_key` [scope](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/).
9+
10+
.EXAMPLE
11+
Get-GitHubUserMyGpgKeyById -GpgKeyID '4AEE18F83AFDEB23'
12+
13+
Gets the GPG key with ID '4AEE18F83AFDEB23' for the authenticated user.
14+
15+
.NOTES
16+
https://docs.github.com/rest/users/gpg-keys#get-a-gpg-key-for-the-authenticated-user
17+
18+
#>
19+
[OutputType([pscustomobject])]
20+
[CmdletBinding()]
21+
param (
22+
# The ID of the GPG key.
23+
[Parameter(
24+
Mandatory
25+
)]
26+
[Alias('gpg_key_id')]
27+
[string] $ID
28+
)
29+
30+
$inputObject = @{
31+
APIEndpoint = "/user/gpg_keys/$ID"
32+
Method = 'GET'
33+
}
34+
35+
(Invoke-GitHubAPI @inputObject).Response
36+
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
filter Add-GitHubUserGpgKey {
2+
<#
3+
.SYNOPSIS
4+
Create a GPG key for the authenticated user
5+
6+
.DESCRIPTION
7+
Adds a GPG key to the authenticated user's GitHub account.
8+
Requires that you are authenticated via Basic Auth, or OAuth with at least `write:gpg_key` [scope](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/).
9+
10+
.EXAMPLE
11+
Add-GitHubUserGpgKey -Name 'GPG key for GitHub' -ArmoredPublicKey '-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: GnuPG v1\n\nmQINBFnZ2ZIBEADQ2Z7Z7\n-----END PGP PUBLIC KEY BLOCK-----'
12+
13+
Adds a GPG key to the authenticated user's GitHub account.
14+
15+
.NOTES
16+
https://docs.github.com/rest/users/gpg-keys#create-a-gpg-key-for-the-authenticated-user
17+
18+
#>
19+
[OutputType([pscustomobject])]
20+
[CmdletBinding()]
21+
param (
22+
# A descriptive name for the new key.
23+
[Parameter(
24+
Mandatory,
25+
ValueFromPipeline,
26+
ValueFromPipelineByPropertyName
27+
)]
28+
[string] $Name,
29+
30+
# A GPG key in ASCII-armored format.
31+
[Parameter(
32+
Mandatory,
33+
ValueFromPipelineByPropertyName
34+
)]
35+
[Alias('armored_public_key')]
36+
[string] $ArmoredPublicKey
37+
38+
)
39+
40+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
41+
42+
$inputObject = @{
43+
APIEndpoint = "/user/gpg_keys"
44+
Method = 'POST'
45+
Body = $body
46+
}
47+
48+
(Invoke-GitHubAPI @inputObject).Response
49+
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
filter Get-GitHubUserGpgKey {
2+
<#
3+
.SYNOPSIS
4+
List GPG keys for a given user or the authenticated user
5+
6+
.DESCRIPTION
7+
Lists a given user's or the current user's GPG keys.
8+
9+
.EXAMPLE
10+
Get-GitHubUserGpgKey
11+
12+
Gets all GPG keys for the authenticated user.
13+
14+
.EXAMPLE
15+
Get-GitHubUserGpgKey -Username 'octocat'
16+
17+
Gets all GPG keys for the 'octocat' user.
18+
19+
.NOTES
20+
https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user
21+
22+
#>
23+
[OutputType([pscustomobject])]
24+
[CmdletBinding()]
25+
param (
26+
# The handle for the GitHub user account.
27+
[Parameter(
28+
Mandatory,
29+
ValueFromPipeline,
30+
ValueFromPipelineByPropertyName,
31+
ParameterSetName = 'Username'
32+
)]
33+
[string] $Username,
34+
35+
# The ID of the GPG key.
36+
[Parameter(
37+
ParameterSetName = 'Me'
38+
)]
39+
[Alias('gpg_key_id')]
40+
[string] $ID,
41+
42+
# The number of results per page (max 100).
43+
[Parameter()]
44+
[int] $PerPage = 30
45+
)
46+
47+
if ($Username) {
48+
Get-GitHubUserGpgKeyForUser -Username $Username -PerPage $PerPage
49+
} else {
50+
if ($ID) {
51+
Get-GitHubUserMyGpgKeyById -ID $ID
52+
} else {
53+
Get-GitHubUserMyGpgKey -PerPage $PerPage
54+
}
55+
}
56+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
filter Remove-GitHubUserGpgKey {
2+
<#
3+
.SYNOPSIS
4+
Delete a GPG key for the authenticated user
5+
6+
.DESCRIPTION
7+
Removes a GPG key from the authenticated user's GitHub account.
8+
Requires that you are authenticated via Basic Auth or via OAuth with at least `admin:gpg_key` [scope](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/).
9+
10+
.EXAMPLE
11+
Remove-GitHubUserGpgKey -ID '1234567'
12+
13+
Gets the GPG key with ID '1234567' for the authenticated user.
14+
15+
.NOTES
16+
https://docs.github.com/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user
17+
18+
#>
19+
[OutputType([pscustomobject])]
20+
[CmdletBinding()]
21+
param (
22+
# The ID of the GPG key.
23+
[Parameter(
24+
Mandatory
25+
)]
26+
[Alias('gpg_key_id')]
27+
[string] $ID
28+
)
29+
30+
$inputObject = @{
31+
APIEndpoint = "/user/gpg_keys/$ID"
32+
Method = 'DELETE'
33+
}
34+
35+
$null = (Invoke-GitHubAPI @inputObject).Response
36+
37+
}

tools/utilities/GitHubAPI.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get
2121
# @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, `
2222
# @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table
2323

24-
$path = '/users/{username}/following/{target_user}'
25-
$method = 'GET'
24+
$path = '/user/gpg_keys/{gpg_key_id}'
25+
$method = 'DELETE'
2626
$response.paths.$path.$method
2727
$response.paths.$path.$method.tags | clip # -> Namespace/foldername
2828
$response.paths.$path.$method.operationId | clip # -> FunctionName

0 commit comments

Comments
 (0)