From f56695e447ad11e8440a31d2815fefe37d1badbe Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 13:44:53 +0200 Subject: [PATCH 1/6] Add Get-GitHubUserGpgKey --- .../Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 | 37 +++++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 diff --git a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 new file mode 100644 index 000000000..f48795020 --- /dev/null +++ b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserGpgKey { + <# + .SYNOPSIS + List GPG keys for the authenticated user + + .DESCRIPTION + Lists the current user's GPG keys. + 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/). + + .EXAMPLE + Get-GitHubUserGpgKey + + Gets all GPG keys for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/gpg_keys' + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index ae6b030d3..25bced9b8 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,7 +21,7 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/users/{username}/following/{target_user}' +$path = '/user/gpg_keys' $method = 'GET' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername From 7440bd69e2a5812a792285b8fe031dfa61528ebd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 13:52:17 +0200 Subject: [PATCH 2/6] Update Get-GitHubUserGpgKey to support given user --- .../GPG-Keys/Get-GitHubUserGpgKeyForUser.ps1 | 44 +++++++++++++++++++ .../Users/GPG-Keys/Get-GitHubUserMyGpgKey.ps1 | 37 ++++++++++++++++ .../Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 | 29 +++++++----- tools/utilities/GitHubAPI.ps1 | 2 +- 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 src/GitHub/private/Users/GPG-Keys/Get-GitHubUserGpgKeyForUser.ps1 create mode 100644 src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKey.ps1 diff --git a/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserGpgKeyForUser.ps1 b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserGpgKeyForUser.ps1 new file mode 100644 index 000000000..035b0f7c4 --- /dev/null +++ b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserGpgKeyForUser.ps1 @@ -0,0 +1,44 @@ +filter Get-GitHubUserGpgKeyForUser { + <# + .SYNOPSIS + List GPG keys for a user + + .DESCRIPTION + Lists the GPG keys for a user. This information is accessible by anyone. + + .EXAMPLE + Get-GitHubUserGpgKeyForUser -Username 'octocat' + + Gets all GPG keys for the 'octocat' user. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-a-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Username, + + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + Remove-HashtableEntries -Hashtable $body -RemoveNames 'username' + + $inputObject = @{ + APIEndpoint = "/users/$Username/gpg_keys" + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKey.ps1 b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKey.ps1 new file mode 100644 index 000000000..bacfe48ff --- /dev/null +++ b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKey.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserMyGpgKey { + <# + .SYNOPSIS + List GPG keys for the authenticated user + + .DESCRIPTION + Lists the current user's GPG keys. + 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/). + + .EXAMPLE + Get-GitHubUserMyGpgKey + + Gets all GPG keys for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/gpg_keys' + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 index f48795020..b7db484f4 100644 --- a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 +++ b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 @@ -1,17 +1,21 @@ filter Get-GitHubUserGpgKey { <# .SYNOPSIS - List GPG keys for the authenticated user + List GPG keys for a given user or the authenticated user .DESCRIPTION - Lists the current user's GPG keys. - 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/). + Lists a given user's or the current user's GPG keys. .EXAMPLE Get-GitHubUserGpgKey Gets all GPG keys for the authenticated user. + .EXAMPLE + Get-GitHubUserGpgKey -Username 'octocat' + + Gets all GPG keys for the 'octocat' user. + .NOTES https://docs.github.com/rest/users/gpg-keys#list-gpg-keys-for-the-authenticated-user @@ -19,19 +23,22 @@ [OutputType([pscustomobject])] [CmdletBinding()] param ( + # The handle for the GitHub user account. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [string] $Username, + # The number of results per page (max 100). [Parameter()] [int] $PerPage = 30 ) - $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case - - $inputObject = @{ - APIEndpoint = '/user/gpg_keys' - Method = 'GET' - Body = $body + if ($Username) { + Get-GitHubUserGpgKeyForUser -Username $Username -PerPage $PerPage + } else { + Get-GitHubUserMyGpgKey -PerPage $PerPage } - (Invoke-GitHubAPI @inputObject).Response - } diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 25bced9b8..a170c233a 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,7 +21,7 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/user/gpg_keys' +$path = '/users/{username}/gpg_keys' $method = 'GET' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername From f50f9f7e2a496d3cbeecabc16b4b3701178ad5da Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 13:58:32 +0200 Subject: [PATCH 3/6] Updated Get-GitHubUserGpgKey to support id, to get specific key for authed user --- .../GPG-Keys/Get-GitHubUserMyGpgKeyById.ps1 | 37 +++++++++++++++++++ .../Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 | 18 +++++++-- tools/utilities/GitHubAPI.ps1 | 2 +- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKeyById.ps1 diff --git a/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKeyById.ps1 b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKeyById.ps1 new file mode 100644 index 000000000..144d615e1 --- /dev/null +++ b/src/GitHub/private/Users/GPG-Keys/Get-GitHubUserMyGpgKeyById.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserMyGpgKeyById { + <# + .SYNOPSIS + Get a GPG key for the authenticated user + + .DESCRIPTION + View extended details for a single GPG key. + 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/). + + .EXAMPLE + Get-GitHubUserMyGpgKeyById -GpgKeyID '4AEE18F83AFDEB23' + + Gets the GPG key with ID '4AEE18F83AFDEB23' for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#get-a-gpg-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The ID of the GPG key. + [Parameter( + Mandatory + )] + [Alias('gpg_key_id')] + [string] $ID + ) + + $inputObject = @{ + APIEndpoint = "/user/gpg_keys/$ID" + Method = 'GET' + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 index b7db484f4..6f11ace0f 100644 --- a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 +++ b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 @@ -25,11 +25,20 @@ param ( # The handle for the GitHub user account. [Parameter( + Mandatory, ValueFromPipeline, - ValueFromPipelineByPropertyName + ValueFromPipelineByPropertyName, + ParameterSetName = 'Username' )] [string] $Username, + # The ID of the GPG key. + [Parameter( + ParameterSetName = 'Me' + )] + [Alias('gpg_key_id')] + [string] $ID, + # The number of results per page (max 100). [Parameter()] [int] $PerPage = 30 @@ -38,7 +47,10 @@ if ($Username) { Get-GitHubUserGpgKeyForUser -Username $Username -PerPage $PerPage } else { - Get-GitHubUserMyGpgKey -PerPage $PerPage + if ($ID) { + Get-GitHubUserMyGpgKeyById -ID $ID + } else { + Get-GitHubUserMyGpgKey -PerPage $PerPage + } } - } diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index a170c233a..e158b1788 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,7 +21,7 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/users/{username}/gpg_keys' +$path = '/user/gpg_keys/{gpg_key_id}' $method = 'GET' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername From b8b6b6dcc5386ea53d18702c8c430b3b3c545c55 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 14:09:33 +0200 Subject: [PATCH 4/6] Add Add-GitHubUserGpgKey --- .../Users/GPG-Keys/Add-GitHubUserGpgKey.ps1 | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/GitHub/public/Users/GPG-Keys/Add-GitHubUserGpgKey.ps1 diff --git a/src/GitHub/public/Users/GPG-Keys/Add-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Add-GitHubUserGpgKey.ps1 new file mode 100644 index 000000000..fee17853e --- /dev/null +++ b/src/GitHub/public/Users/GPG-Keys/Add-GitHubUserGpgKey.ps1 @@ -0,0 +1,50 @@ +filter Add-GitHubUserGpgKey { + <# + .SYNOPSIS + Create a GPG key for the authenticated user + + .DESCRIPTION + Adds a GPG key to the authenticated user's GitHub account. + 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/). + + .EXAMPLE + Add-GitHubUserGpgKey -Name 'GPG key for GitHub' -ArmoredPublicKey '-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: GnuPG v1\n\nmQINBFnZ2ZIBEADQ2Z7Z7\n-----END PGP PUBLIC KEY BLOCK-----' + + Adds a GPG key to the authenticated user's GitHub account. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#create-a-gpg-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # A descriptive name for the new key. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [string] $Name, + + # A GPG key in ASCII-armored format. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [Alias('armored_public_key')] + [string] $ArmoredPublicKey + + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = "/user/gpg_keys" + Method = 'POST' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} From 84859f39981b16681258177003ad5354623b8cc6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 14:09:45 +0200 Subject: [PATCH 5/6] Add RemoveGitHubUserGpgKey --- .../GPG-Keys/Remove-GitHubUserGpgKey.ps1 | 37 +++++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 diff --git a/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 new file mode 100644 index 000000000..3b30b3c4e --- /dev/null +++ b/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 @@ -0,0 +1,37 @@ +filter Remove-GitHubUserGpgKey { + <# + .SYNOPSIS + Delete a GPG key for the authenticated user + + .DESCRIPTION + Removes a GPG key from the authenticated user's GitHub account. + 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/). + + .EXAMPLE + Remove-GitHubUserGpgKey -ID '1234567' + + Gets the GPG key with ID '1234567' for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/gpg-keys#delete-a-gpg-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The ID of the GPG key. + [Parameter( + Mandatory + )] + [Alias('gpg_key_id')] + [string] $ID + ) + + $inputObject = @{ + APIEndpoint = "/user/gpg_keys/$ID" + Method = 'DELETE' + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index e158b1788..d4f716447 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -22,7 +22,7 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table $path = '/user/gpg_keys/{gpg_key_id}' -$method = 'GET' +$method = 'DELETE' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName From 67984769cb4b9b259bc4ae906f142509d096bafe Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 14:10:57 +0200 Subject: [PATCH 6/6] mini fix for output of remove --- src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 index 3b30b3c4e..fac0d3ff2 100644 --- a/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 +++ b/src/GitHub/public/Users/GPG-Keys/Remove-GitHubUserGpgKey.ps1 @@ -32,6 +32,6 @@ Method = 'DELETE' } - (Invoke-GitHubAPI @inputObject).Response + $null = (Invoke-GitHubAPI @inputObject).Response }