From 704ffbe60915387a11b18134a6662c40b4a6f2e2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 19 Dec 2024 09:49:08 +0100 Subject: [PATCH 1/3] Add Get-GitHubContextInfo function to list available GitHub contexts --- .../Auth/Context/Get-GitHubContextInfo.ps1 | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 diff --git a/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 b/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 new file mode 100644 index 000000000..10d0f7205 --- /dev/null +++ b/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 @@ -0,0 +1,41 @@ +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } + +function Get-GitHubContextInfo { + <# + .SYNOPSIS + Lists the available GitHub contexts without getting the context data. + + .DESCRIPTION + Lists the available GitHub contexts without getting the context data. + + .EXAMPLE + Get-GitHubContextInfo + + Gets the current GitHub context. + #> + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingConvertToSecureStringWithPlainText', '', + Justification = 'Encapsulated in a function. Never leaves as a plain text.' + )] + [OutputType([GitHubContext])] + [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] + param() + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + Initialize-GitHubConfig + } + + process { + try { + Get-ContextInfo -ID "$($script:GitHub.Config.ID)/*" + } catch { + throw $_ + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} From 2bc6c03191af5da456c09e04bf7c38dd07fabda6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 19 Dec 2024 11:11:02 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20required?= =?UTF-8?q?=20version=20of=20'Context'=20module=20to=205.0.5=20in=20multip?= =?UTF-8?q?le=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Auth/Context/Remove-GitHubContext.ps1 | 2 +- .../Auth/Context/Set-GitHubContext.ps1 | 2 +- .../public/Auth/Context/Get-GitHubContext.ps1 | 2 +- .../Auth/Context/Get-GitHubContextInfo.ps1 | 21 ++++++++++++++++--- .../Auth/Update-GitHubUserAccessToken.ps1 | 7 +++++-- .../public/Config/Get-GitHubConfig.ps1 | 2 +- .../public/Config/Remove-GitHubConfig.ps1 | 2 +- .../public/Config/Set-GitHubConfig.ps1 | 2 +- tests/GitHub.Tests.ps1 | 8 +++---- 9 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/functions/private/Auth/Context/Remove-GitHubContext.ps1 b/src/functions/private/Auth/Context/Remove-GitHubContext.ps1 index 72b5b4246..d3c5a8f37 100644 --- a/src/functions/private/Auth/Context/Remove-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Remove-GitHubContext.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } filter Remove-GitHubContext { <# diff --git a/src/functions/private/Auth/Context/Set-GitHubContext.ps1 b/src/functions/private/Auth/Context/Set-GitHubContext.ps1 index 902f7604a..fbba40931 100644 --- a/src/functions/private/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Set-GitHubContext.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Set-GitHubContext { <# diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index f14da0dd1..87857b38f 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Get-GitHubContext { <# diff --git a/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 b/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 index 10d0f7205..cd43e4bfe 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContextInfo.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Get-GitHubContextInfo { <# @@ -12,6 +12,16 @@ function Get-GitHubContextInfo { Get-GitHubContextInfo Gets the current GitHub context. + + .EXAMPLE + Get-GitHubContextInfo -Name 'github.com*' + + Gets the GitHub context that matches the name 'github.com*'. + + .EXAMPLE + Get-GitHubContextInfo -Name '*/Organization/*' + + Gets the GitHub context that matches the name '*/Organization/*'. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSAvoidUsingConvertToSecureStringWithPlainText', '', @@ -19,7 +29,12 @@ function Get-GitHubContextInfo { )] [OutputType([GitHubContext])] [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] - param() + param( + # The name of the context to get. + [Parameter()] + [SupportsWildcards()] + [string] $Name = '*' + ) begin { $stackPath = Get-PSCallStackPath @@ -29,7 +44,7 @@ function Get-GitHubContextInfo { process { try { - Get-ContextInfo -ID "$($script:GitHub.Config.ID)/*" + Get-ContextInfo -ID "$($script:GitHub.Config.ID)/$Name" } catch { throw $_ } diff --git a/src/functions/public/Auth/Update-GitHubUserAccessToken.ps1 b/src/functions/public/Auth/Update-GitHubUserAccessToken.ps1 index 237b86f4d..bbce1c0c2 100644 --- a/src/functions/public/Auth/Update-GitHubUserAccessToken.ps1 +++ b/src/functions/public/Auth/Update-GitHubUserAccessToken.ps1 @@ -1,4 +1,6 @@ -function Update-GitHubUserAccessToken { +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } + +function Update-GitHubUserAccessToken { <# .SYNOPSIS Updates the GitHub access token. @@ -22,7 +24,8 @@ [OutputType([securestring])] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links for documentation.')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Is the CLI part of the module.')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'The tokens are recieved as clear text. Mitigating exposure by removing variables and performing garbage collection.')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', + Justification = 'The tokens are recieved as clear text. Mitigating exposure by removing variables and performing garbage collection.')] [CmdletBinding(SupportsShouldProcess)] param( # The context to run the command in. Used to get the details for the API call. diff --git a/src/functions/public/Config/Get-GitHubConfig.ps1 b/src/functions/public/Config/Get-GitHubConfig.ps1 index 80ad7190b..0e09e7501 100644 --- a/src/functions/public/Config/Get-GitHubConfig.ps1 +++ b/src/functions/public/Config/Get-GitHubConfig.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Get-GitHubConfig { <# diff --git a/src/functions/public/Config/Remove-GitHubConfig.ps1 b/src/functions/public/Config/Remove-GitHubConfig.ps1 index fc882ed6c..21da4986c 100644 --- a/src/functions/public/Config/Remove-GitHubConfig.ps1 +++ b/src/functions/public/Config/Remove-GitHubConfig.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Remove-GitHubConfig { <# diff --git a/src/functions/public/Config/Set-GitHubConfig.ps1 b/src/functions/public/Config/Set-GitHubConfig.ps1 index feebfc01b..00cc7c744 100644 --- a/src/functions/public/Config/Set-GitHubConfig.ps1 +++ b/src/functions/public/Config/Set-GitHubConfig.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.4' } +#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.5' } function Set-GitHubConfig { <# diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index a9568e5bd..8fbbb2836 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -62,7 +62,7 @@ Describe 'GitHub' { PrivateKey = $env:TEST_APP_PRIVATE_KEY } { Connect-GitHubAccount @params } | Should -Not -Throw - $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + $contexts = Get-GitHubContextInfo -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose ($contexts).Count | Should -Be 3 } @@ -72,16 +72,16 @@ Describe 'GitHub' { PrivateKey = $env:TEST_APP_PRIVATE_KEY } { Connect-GitHubAccount @params -AutoloadInstallations } | Should -Not -Throw - $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + $contexts = Get-GitHubContextInfo -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose ($contexts).Count | Should -Be 7 } It 'Can disconnect a specific context' { { Disconnect-GitHubAccount -Context 'github.com/psmodule-test-app/Organization/PSModule' -Silent } | Should -Not -Throw - $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + $contexts = Get-GitHubContextInfo -Name '*psmodule-test-app*' -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose - ($contexts).Count | Should -Be 6 + ($contexts).Count | Should -Be 3 Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY -AutoloadInstallations $contexts = Get-GitHubContext -ListAvailable -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose From 58548a53baaf437883db1b08a8729e0afc8e958c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 19 Dec 2024 11:30:41 +0100 Subject: [PATCH 3/3] Fix --- tests/GitHub.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 8fbbb2836..192e0ccaa 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -79,7 +79,7 @@ Describe 'GitHub' { It 'Can disconnect a specific context' { { Disconnect-GitHubAccount -Context 'github.com/psmodule-test-app/Organization/PSModule' -Silent } | Should -Not -Throw - $contexts = Get-GitHubContextInfo -Name '*psmodule-test-app*' -Verbose:$false + $contexts = Get-GitHubContextInfo -Name 'github.com/psmodule-test-app/*' -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose ($contexts).Count | Should -Be 3 Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY -AutoloadInstallations