diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index e41175727..736d8fdb9 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -1,4 +1,4 @@ -class GitHubContext : Context { +class GitHubContext : Context { # The GitHub Context Name. # HostName/Username or HostName/AppSlug # github.com/Octocat @@ -18,7 +18,7 @@ # The API base URI. # https://api.github.com - [string] $ApiBaseUri + [uri] $ApiBaseUri # The GitHub API version. # 2022-11-28 diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index 3ec9dcff6..462390d62 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -131,7 +131,7 @@ $headers | Remove-HashtableEntry -NullOrEmptyValues if (-not $URI) { - $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') + $URI = ("$ApiBaseUri" -replace '/$'), ("$ApiEndpoint" -replace '^/') -join '/' } $APICall = @{ diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 index 9055ec5ee..bbcd89b33 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 @@ -40,6 +40,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App switch ($PSCmdlet.ParameterSetName) { 'BySlug' { diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 index 79f051134..fb8c1aefd 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 @@ -26,6 +26,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 index d4d3667e3..34bf8658d 100644 --- a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 +++ b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 @@ -66,6 +66,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 64b8dbdf0..163b65fdd 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -27,6 +27,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index b2ad5b3ae..e5a256094 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -26,6 +26,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index e53b67ae0..ffa46dd35 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -18,16 +18,27 @@ Mandatory, ValueFromPipeline )] - [GitHubContext] $Context, + [object] $Context, # The required authtypes for the command. [Parameter(Mandatory)] [string[]] $AuthType ) - $command = (Get-PSCallStack)[1].Command + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + } + + process { + $command = (Get-PSCallStack)[1].Command + + if ($Context.AuthType -notin $AuthType) { + throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]." + } + } - if ($Context.AuthType -notin $AuthType) { - throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]." + end { + Write-Verbose "[$commandName] - End" } } diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 498ab089b..0fa7235fb 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -1,4 +1,4 @@ -function Connect-GitHubApp { +function Connect-GitHubApp { <# .SYNOPSIS Connects to GitHub as a installation using a GitHub App. @@ -104,7 +104,7 @@ AuthType = [string]'IAT' TokenType = [string]'ghs' DisplayName = [string]$Context.DisplayName - ApiBaseUri = [string]$Context.ApiBaseUri + ApiBaseUri = [uri]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName ClientID = [string]$Context.ClientID @@ -113,18 +113,18 @@ Events = [string[]]$installation.events TargetType = [string]$installation.target_type Token = [securestring]$token.Token - TokenExpirationDate = [string]$token.ExpiresAt + TokenExpirationDate = [datetime]$token.ExpiresAt } switch ($installation.target_type) { 'User' { - $contextParams['TargetName'] = $installation.account.login + $contextParams['TargetName'] = [string]$installation.account.login } 'Organization' { - $contextParams['TargetName'] = $installation.account.login + $contextParams['TargetName'] = [string]$installation.account.login } 'Enterprise' { - $contextParams['TargetName'] = $installation.account.slug + $contextParams['TargetName'] = [string]$installation.account.slug } } Write-Verbose 'Logging in using a managed installation access token...'