From b566fc1e6e35fbce7ad51cf500cbb2fd532342f0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 26 Jan 2025 17:41:57 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20`HttpVersio?= =?UTF-8?q?n`=20and=20`PerPage`=20properties=20to=20`GitHubConfig`=20and?= =?UTF-8?q?=20`GitHubContext`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/public/Config/GitHubConfig.ps1 | 6 ++++++ src/classes/public/Context/GitHubContext.ps1 | 3 +++ src/functions/public/API/Invoke-GitHubAPI.ps1 | 17 +++++++++++------ .../public/Auth/Connect-GitHubAccount.ps1 | 19 +++++++++++-------- .../public/Auth/Connect-GitHubApp.ps1 | 3 ++- src/variables/private/GitHub.ps1 | 2 ++ 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/classes/public/Config/GitHubConfig.ps1 b/src/classes/public/Config/GitHubConfig.ps1 index 1773ef9cd..5bed0f15c 100644 --- a/src/classes/public/Config/GitHubConfig.ps1 +++ b/src/classes/public/Config/GitHubConfig.ps1 @@ -17,6 +17,12 @@ # The default OAuth app client ID. [string] $OAuthAppClientID + # The default value for the HTTP protocol version. + [version] $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..8a0def6e6 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. + [version] $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..4c770fd8e 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', + [version] $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..6e95a1ded 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -166,6 +166,8 @@ if (-not $HostName) { $HostName = $script:GitHub.Config.HostName } + $httpVersion = $script:GitHub.Config.HttpVersion + $perPage = $script:GitHub.Config.PerPage $HostName = $HostName -replace '^https?://' $ApiBaseUri = "https://api.$HostName" $authType = $PSCmdlet.ParameterSetName @@ -184,14 +186,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 = [version]$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..eff588879 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 = [version]$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..591f44def 100644 --- a/src/variables/private/GitHub.ps1 +++ b/src/variables/private/GitHub.ps1 @@ -14,6 +14,8 @@ GitHubAppClientID = 'Iv1.f26b61bc99e69405' OAuthAppClientID = '7204ae9b0580f2cb8288' DefaultContext = '' + HttpVersion = '2.0' + PerPage = 100 } Config = $null Event = $null From 0c608833059ac47d90de7736ed30883dc96dabe0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 26 Jan 2025 18:06:44 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20`ApiVersion?= =?UTF-8?q?`=20property=20to=20`GitHubConfig`=20and=20update=20connection?= =?UTF-8?q?=20logic=20in=20`Connect-GitHubAccount`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/public/Config/GitHubConfig.ps1 | 3 +++ .../public/Auth/Connect-GitHubAccount.ps1 | 5 +---- src/variables/private/GitHub.ps1 | 1 + tests/GitHub.Tests.ps1 | 22 +++++++++++++++---- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/classes/public/Config/GitHubConfig.ps1 b/src/classes/public/Config/GitHubConfig.ps1 index 5bed0f15c..fee6dd60e 100644 --- a/src/classes/public/Config/GitHubConfig.ps1 +++ b/src/classes/public/Config/GitHubConfig.ps1 @@ -17,6 +17,9 @@ # 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. [version] $HttpVersion diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 6e95a1ded..eeee987a3 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()] @@ -168,6 +164,7 @@ } $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 diff --git a/src/variables/private/GitHub.ps1 b/src/variables/private/GitHub.ps1 index 591f44def..9ca9b6847 100644 --- a/src/variables/private/GitHub.ps1 +++ b/src/variables/private/GitHub.ps1 @@ -14,6 +14,7 @@ GitHubAppClientID = 'Iv1.f26b61bc99e69405' OAuthAppClientID = '7204ae9b0580f2cb8288' DefaultContext = '' + ApiVersion = '2022-11-28' HttpVersion = '2.0' PerPage = 100 } 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 From d7c77e1578dce336f221994be62314aedc570d62 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 26 Jan 2025 18:39:46 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Change=20`HttpVer?= =?UTF-8?q?sion`=20type=20from=20`version`=20to=20`string`=20in=20multiple?= =?UTF-8?q?=20files=20for=20consistency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/public/Config/GitHubConfig.ps1 | 2 +- src/classes/public/Context/GitHubContext.ps1 | 2 +- src/functions/public/API/Invoke-GitHubAPI.ps1 | 2 +- src/functions/public/Auth/Connect-GitHubAccount.ps1 | 2 +- src/functions/public/Auth/Connect-GitHubApp.ps1 | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/classes/public/Config/GitHubConfig.ps1 b/src/classes/public/Config/GitHubConfig.ps1 index fee6dd60e..02fdd1eb5 100644 --- a/src/classes/public/Config/GitHubConfig.ps1 +++ b/src/classes/public/Config/GitHubConfig.ps1 @@ -21,7 +21,7 @@ [string] $ApiVersion # The default value for the HTTP protocol version. - [version] $HttpVersion + [string] $HttpVersion # The default value for the 'per_page' API parameter used in 'Get' functions that support paging. [int] $PerPage diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index 8a0def6e6..232c2d21a 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -57,7 +57,7 @@ class GitHubContext { [string] $Repo # The default value for the HTTP protocol version. - [version] $HttpVersion + [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 4c770fd8e..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, + [string] $HttpVersion, # The file path to be used for the API request. This is used for uploading files. [Parameter()] diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index eeee987a3..92ca09f6f 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -186,7 +186,7 @@ ApiBaseUri = [string]$ApiBaseUri ApiVersion = [string]$ApiVersion HostName = [string]$HostName - HttpVersion = [version]$httpVersion + HttpVersion = [string]$httpVersion PerPage = [int]$perPage AuthType = [string]$authType Enterprise = [string]$Enterprise diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index eff588879..2b15b2dab 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -115,7 +115,7 @@ ApiBaseUri = [string]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName - HttpVersion = [version]$Context.HttpVersion + HttpVersion = [string]$Context.HttpVersion PerPage = [int]$Context.PerPage ClientID = [string]$Context.ClientID InstallationID = [string]$installation.id