diff --git a/src/classes/public/Config/GitHubConfig.ps1 b/src/classes/public/Config/GitHubConfig.ps1 index 1773ef9cd..02fdd1eb5 100644 --- a/src/classes/public/Config/GitHubConfig.ps1 +++ b/src/classes/public/Config/GitHubConfig.ps1 @@ -17,6 +17,15 @@ # The default OAuth app client ID. [string] $OAuthAppClientID + # The default value for the GitHub API version to use. + [string] $ApiVersion + + # The default value for the HTTP protocol version. + [string] $HttpVersion + + # The default value for the 'per_page' API parameter used in 'Get' functions that support paging. + [int] $PerPage + # Simple parameterless constructor GitHubConfig() {} diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index eb4701ca6..232c2d21a 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -56,6 +56,9 @@ class GitHubContext { # The default value for the Repo parameter. [string] $Repo + # The default value for the HTTP protocol version. + [string] $HttpVersion + # The default value for the 'per_page' API parameter used in 'Get' functions that support paging. [int] $PerPage diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index 74a402946..9f3ac0f34 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -51,7 +51,7 @@ # Specifies the HTTP version used for the request. [Parameter()] - [version] $HttpVersion = '2.0', + [string] $HttpVersion, # The file path to be used for the API request. This is used for uploading files. [Parameter()] @@ -97,20 +97,25 @@ $Token = $Context.Token Write-Debug "Token : [$Token]" - if ([string]::IsNullOrEmpty($TokenType)) { - $TokenType = $Context.TokenType + if ([string]::IsNullOrEmpty($HttpVersion)) { + $HttpVersion = $Context.HttpVersion } - Write-Debug "TokenType : [$($Context.TokenType)]" + Write-Debug "HttpVersion: [$HttpVersion]" if ([string]::IsNullOrEmpty($ApiBaseUri)) { $ApiBaseUri = $Context.ApiBaseUri } - Write-Debug "ApiBaseUri: [$($Context.ApiBaseUri)]" + Write-Debug "ApiBaseUri: [$ApiBaseUri]" if ([string]::IsNullOrEmpty($ApiVersion)) { $ApiVersion = $Context.ApiVersion } - Write-Debug "ApiVersion: [$($Context.ApiVersion)]" + Write-Debug "ApiVersion: [$ApiVersion]" + + if ([string]::IsNullOrEmpty($TokenType)) { + $TokenType = $Context.TokenType + } + Write-Debug "TokenType : [$TokenType]" switch ($TokenType) { 'ghu' { diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 5e35c2b42..92ca09f6f 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -124,10 +124,6 @@ [Alias('Repository')] [string] $Repo, - # API version used for API requests. - [Parameter()] - [string] $ApiVersion = '2022-11-28', - # The host to connect to. Can use $env:GITHUB_SERVER_URL to set the host, as the protocol is removed automatically. # Example: github.com, github.enterprise.com, msx.ghe.com [Parameter()] @@ -166,6 +162,9 @@ if (-not $HostName) { $HostName = $script:GitHub.Config.HostName } + $httpVersion = $script:GitHub.Config.HttpVersion + $perPage = $script:GitHub.Config.PerPage + $ApiVersion = $script:GitHub.Config.ApiVersion $HostName = $HostName -replace '^https?://' $ApiBaseUri = "https://api.$HostName" $authType = $PSCmdlet.ParameterSetName @@ -184,14 +183,15 @@ } $context = @{ - ApiBaseUri = [string]$ApiBaseUri - ApiVersion = [string]$ApiVersion - HostName = [string]$HostName - AuthType = [string]$authType - Enterprise = [string]$Enterprise - Owner = [string]$Owner - Repo = [string]$Repo - PerPage = 100 + ApiBaseUri = [string]$ApiBaseUri + ApiVersion = [string]$ApiVersion + HostName = [string]$HostName + HttpVersion = [string]$httpVersion + PerPage = [int]$perPage + AuthType = [string]$authType + Enterprise = [string]$Enterprise + Owner = [string]$Owner + Repo = [string]$Repo } Write-Verbose ($context | Format-Table | Out-String) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 8f24c712e..2b15b2dab 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -115,7 +115,8 @@ ApiBaseUri = [string]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName - PerPage = 100 + HttpVersion = [string]$Context.HttpVersion + PerPage = [int]$Context.PerPage ClientID = [string]$Context.ClientID InstallationID = [string]$installation.id Permissions = [pscustomobject]$installation.permissions diff --git a/src/variables/private/GitHub.ps1 b/src/variables/private/GitHub.ps1 index 5e689ef8a..9ca9b6847 100644 --- a/src/variables/private/GitHub.ps1 +++ b/src/variables/private/GitHub.ps1 @@ -14,6 +14,9 @@ GitHubAppClientID = 'Iv1.f26b61bc99e69405' OAuthAppClientID = '7204ae9b0580f2cb8288' DefaultContext = '' + ApiVersion = '2022-11-28' + HttpVersion = '2.0' + PerPage = 100 } Config = $null Event = $null diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 681f0844d..d657262cd 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -12,7 +12,7 @@ param() BeforeAll { Get-SecretInfo | Remove-Secret Get-SecretVault | Unregister-SecretVault - Import-Module -Name Context -Force -RequiredVersion 6.0.0 + Get-Module -ListAvailable -Name Context | Sort-Object -Property Version | Select-Object -Last 1 | Import-Module -Force } Describe 'GitHub' { @@ -23,9 +23,11 @@ Describe 'GitHub' { $config | Should -Not -BeNullOrEmpty } It 'Get-GitHubConfig - Gets a configuration item by name' { - $config = Get-GitHubConfig -Name 'HostName' - Write-Verbose ($config | Format-Table | Out-String) -Verbose - $config | Should -Not -BeNullOrEmpty + $config = Get-GitHubConfig + $config.AccessTokenGracePeriodInHours | Should -Be 4 + $config.HostName | Should -Be 'github.com' + $config.HttpVersion | Should -Be '2.0' + $config.PerPage | Should -Be 100 } It 'Set-GitHubConfig - Sets a configuration item' { Set-GitHubConfig -Name 'HostName' -Value 'msx.ghe.com' @@ -66,6 +68,18 @@ Describe 'GitHub' { Write-Verbose (Get-GitHubContext | Out-String) -Verbose $context | Should -Not -BeNullOrEmpty } + It 'Connect-GitHubAccount - Connects with default settings' { + $context = Get-GitHubContext + Write-Verbose ($context | Select-Object -Property * | Out-String) -Verbose + $context | Should -Not -BeNullOrEmpty + $context.ApiBaseUri | Should -Be 'https://api.github.com' + $context.ApiVersion | Should -Be '2022-11-28' + $context.AuthType | Should -Be 'IAT' + $context.HostName | Should -Be 'github.com' + $context.HttpVersion | Should -Be '2.0' + $context.TokenType | Should -Be 'ghs' + $context.Name | Should -Be 'github.com/github-actions/Organization/PSModule' + } It 'Disconnect-GitHubAccount - Disconnects the context from the pipeline' { $context = Get-GitHubContext { $context | Disconnect-GitHubAccount } | Should -Not -Throw