From 3cf4a746d2c2ae49c6c2ababa95ac566738f18fb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 6 Dec 2024 20:21:17 +0100 Subject: [PATCH] Refactor Resolve-GitHubContext to improve context validation and logging --- .../Auth/Context/Resolve-GitHubContext.ps1 | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 index d03b5c37..560abf23 100644 --- a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 @@ -29,34 +29,44 @@ [object] $Context = (Get-GitHubContext) ) - if ([string]::IsNullOrEmpty($Context)) { - throw "No contexts has been specified. Please provide a context or log in using 'Connect-GitHub'." + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + Write-Verbose "Context:" + Write-Verbose ($Context | Out-String) } - if ($Context -is [string]) { - $contextName = $Context - Write-Debug "Getting context: [$contextName]" - $Context = Get-GitHubContext -Context $contextName - } + process { + if ([string]::IsNullOrEmpty($Context)) { + throw "No contexts has been specified. Please provide a context or log in using 'Connect-GitHub'." + } - if (-not $Context) { - throw "Context [$contextName] not found. Please provide a valid context or log in using 'Connect-GitHub'." - } + if ($Context -is [string]) { + $contextName = $Context + Write-Debug "Getting context: [$contextName]" + $Context = Get-GitHubContext -Context $contextName + } - switch ($Context.Type) { - 'App' { - $availableContexts = Get-GitHubContext -ListAvailable | Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID } - $params = Get-FunctionParameter -Scope 2 - Write-Verbose "Resolving parameters used in called function" - Write-Verbose ($params | Out-String) - if ($params.Keys -in 'Owner', 'Organization') { - $foundContext = $availableContexts | Where-Object { $_.Type -eq 'Installation' -and $_.Owner -eq $params.Owner } - if ($foundContext) { - return $foundContext + if (-not $Context) { + throw "Context [$contextName] not found. Please provide a valid context or log in using 'Connect-GitHub'." + } + + switch ($Context.Type) { + 'App' { + $availableContexts = Get-GitHubContext -ListAvailable | + Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID } + $params = Get-FunctionParameter -Scope 2 + Write-Verbose 'Resolving parameters used in called function' + Write-Verbose ($params | Out-String) + if ($params.Keys -in 'Owner', 'Organization') { + $Context = $availableContexts | Where-Object { $_.Owner -eq $params.Owner } } } } } - $Context + end { + Write-Output $Context + Write-Verbose "[$commandName] - End" + } }