From 5eb2475c72f828fe764c184f509cd603101d6358 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 9 Dec 2024 08:58:17 +0100 Subject: [PATCH] Refactor GitHub context handling and improve verbosity in authentication scripts --- .../Auth/Context/Resolve-GitHubContext.ps1 | 5 ++- .../public/Apps/Get-GitHubAppJSONWebToken.ps1 | 4 --- .../public/Auth/Connect-GitHubAccount.ps1 | 6 ---- .../public/Auth/Connect-GitHubApp.ps1 | 35 ------------------- .../public/Auth/Context/Set-GitHubContext.ps1 | 4 +-- .../Auth/Context/Set-GitHubDefaultContext.ps1 | 25 ++++++++----- src/functions/public/Auth/completers.ps1 | 28 +++++++++++++++ 7 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 src/functions/public/Auth/completers.ps1 diff --git a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 index c2b90a22..1c4c5393 100644 --- a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 @@ -33,7 +33,7 @@ $commandName = $MyInvocation.MyCommand.Name Write-Verbose "[$commandName] - Start" Write-Verbose 'Context:' - $Context | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ } + $Context | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ } } process { @@ -59,6 +59,9 @@ # } # } # } + + Write-Verbose 'Resolved Context:' + $Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ } } end { diff --git a/src/functions/public/Apps/Get-GitHubAppJSONWebToken.ps1 b/src/functions/public/Apps/Get-GitHubAppJSONWebToken.ps1 index 27c4cecd..16ce09df 100644 --- a/src/functions/public/Apps/Get-GitHubAppJSONWebToken.ps1 +++ b/src/functions/public/Apps/Get-GitHubAppJSONWebToken.ps1 @@ -120,8 +120,4 @@ function Get-GitHubAppJSONWebToken { ExpiresAt = $exp Issuer = $ClientId } - Remove-Variable -Name jwt -ErrorAction SilentlyContinue - Remove-Variable -Name rsa -ErrorAction SilentlyContinue - Remove-Variable -Name signature -ErrorAction SilentlyContinue - [System.GC]::Collect() } diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 14c8ad99..51a20c4a 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -294,12 +294,6 @@ Write-Error $_ Write-Error (Get-PSCallStack | Format-Table | Out-String) throw 'Failed to connect to GitHub.' - } finally { - Remove-Variable -Name tokenResponse -ErrorAction SilentlyContinue - Remove-Variable -Name context -ErrorAction SilentlyContinue - Remove-Variable -Name contextData -ErrorAction SilentlyContinue - Remove-Variable -Name Token -ErrorAction SilentlyContinue - [System.GC]::Collect() } Write-Verbose "[$commandName] - End" } diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 0f6c4ef2..cdc76b93 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -124,46 +124,11 @@ $name = $tmpContext.name Write-Host "Connected $name" } - Remove-Variable -Name tmpContext -ErrorAction SilentlyContinue - Remove-Variable -Name token -ErrorAction SilentlyContinue } } catch { Write-Error $_ Write-Error (Get-PSCallStack | Format-Table | Out-String) throw 'Failed to connect to GitHub using a GitHub App.' - } finally { - } clean { - Remove-Variable -Name tmpContext -ErrorAction SilentlyContinue - Remove-Variable -Name token -ErrorAction SilentlyContinue - [System.GC]::Collect() } Write-Verbose "[$commandName] - End" } - -Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock { - param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) - $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - - Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'User' -and $_.account.login -like "$wordToComplete*" } | - ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login) - } -} -Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock { - param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) - $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - - Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -like "$wordToComplete*" } | - ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login) - } -} -Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock { - param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) - $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - - Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -like "$wordToComplete*" } | - ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.account.slug, $_.account.slug, 'ParameterValue', $_.account.slug) - } -} diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index acbddc79..bb6737f8 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -95,9 +95,9 @@ function Set-GitHubContext { if ($PSCmdlet.ShouldProcess('Context', 'Set')) { Set-Context -ID "$($script:GitHub.Config.ID)/$($context['Name'])" -Context $context if ($Default) { - Set-GitHubDefaultContext -Context $($context['Name']) + Set-GitHubDefaultContext -Context $context['Name'] if ($Context['AuthType'] -eq 'IAT' -and $script:GitHub.EnvironmentType -eq 'GHA') { - Set-GitHubGitConfig -Context $($context['Name']) + Set-GitHubGitConfig -Context $context['Name'] } } if ($PassThru) { diff --git a/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 index 691e6ed7..82a51d3b 100644 --- a/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 @@ -11,18 +11,25 @@ #> [CmdletBinding(SupportsShouldProcess)] param( - # The context to set as the default. - [Parameter(Mandatory)] - [Alias('Name')] - [string] $Context + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) ) - $commandName = $MyInvocation.MyCommand.Name - Write-Verbose "[$commandName] - Start" + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + $Context = Resolve-GitHubContext -Context $Context + } - if ($PSCmdlet.ShouldProcess("$Context", 'Set default context')) { - Set-GitHubConfig -Name 'DefaultContext' -Value $Context + process { + if ($PSCmdlet.ShouldProcess("$Context", 'Set default context')) { + Set-GitHubConfig -Name 'DefaultContext' -Value $Context.Name + } } - Write-Verbose "[$commandName] - End" + end { + Write-Verbose "[$commandName] - End" + } } diff --git a/src/functions/public/Auth/completers.ps1 b/src/functions/public/Auth/completers.ps1 new file mode 100644 index 00000000..d4b5e161 --- /dev/null +++ b/src/functions/public/Auth/completers.ps1 @@ -0,0 +1,28 @@ + +Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'User' -and $_.account.login -like "$wordToComplete*" } | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login) + } +} +Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -like "$wordToComplete*" } | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login) + } +} +Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -like "$wordToComplete*" } | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.account.slug, $_.account.slug, 'ParameterValue', $_.account.slug) + } +}