Skip to content

Commit 34712fe

Browse files
🩹 [Patch]: Source all defaults for Context via Config (#285)
## Description This pull request introduces several enhancements to the GitHub PowerShell module, focusing on default configuration values, context handling, and testing improvements. Default values have been added for GitHub API and HTTP protocol versions, updating context handling to use these default values, and enhancing the test suite to verify the new configurations. ### Enhancements to default configuration values * [`src/classes/public/Config/GitHubConfig.ps1`](diffhunk://#diff-b30ece1b52dbf4e2436f25c9d00b18f0420696da62094aa2b6eb7cfa765aeac9R20-R28): Added default values for `ApiVersion`, `HttpVersion`, and `PerPage` parameters. * [`src/variables/private/GitHub.ps1`](diffhunk://#diff-d48c42727c113c1d1872da6b9e8446dbed357a87545ec0f78de395c9a2553086R17-R19): Set default values for `ApiVersion`, `HttpVersion`, and `PerPage` in the configuration. Updates to context handling: * [`src/classes/public/Context/GitHubContext.ps1`](diffhunk://#diff-a8076f3b68ae776032b69baf8cc6198b2f00f834af814088fa260d0be7b684a8R59-R61): Included default values for `HttpVersion` and `PerPage`. * [`src/functions/public/API/Invoke-GitHubAPI.ps1`](diffhunk://#diff-9285dd3cdd5467d93c8e68c989041171e17993971649b877dce001b1861b2c39L54-R54): Modified to utilize the context's `HttpVersion` and `ApiVersion` values. [[1]](diffhunk://#diff-9285dd3cdd5467d93c8e68c989041171e17993971649b877dce001b1861b2c39L54-R54) [[2]](diffhunk://#diff-9285dd3cdd5467d93c8e68c989041171e17993971649b877dce001b1861b2c39L100-R118) * [`src/functions/public/Auth/Connect-GitHubAccount.ps1`](diffhunk://#diff-12918e90451cdedb78571b9a67ac0313331a25175cebb606b7108b7bf06af092R165-R167): Updated to set context properties `HttpVersion`, `PerPage`, and `ApiVersion` from the configuration. [[1]](diffhunk://#diff-12918e90451cdedb78571b9a67ac0313331a25175cebb606b7108b7bf06af092R165-R167) [[2]](diffhunk://#diff-12918e90451cdedb78571b9a67ac0313331a25175cebb606b7108b7bf06af092R189-L194) Enhancements to the test suite: * [`tests/GitHub.Tests.ps1`](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL26-R30): Improved tests to verify the new default configuration values and context settings. [[1]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL26-R30) [[2]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cR71-R82) ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 3223f04 commit 34712fe

File tree

7 files changed

+58
-23
lines changed

7 files changed

+58
-23
lines changed

src/classes/public/Config/GitHubConfig.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
# The default OAuth app client ID.
1818
[string] $OAuthAppClientID
1919

20+
# The default value for the GitHub API version to use.
21+
[string] $ApiVersion
22+
23+
# The default value for the HTTP protocol version.
24+
[string] $HttpVersion
25+
26+
# The default value for the 'per_page' API parameter used in 'Get' functions that support paging.
27+
[int] $PerPage
28+
2029
# Simple parameterless constructor
2130
GitHubConfig() {}
2231

src/classes/public/Context/GitHubContext.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class GitHubContext {
5656
# The default value for the Repo parameter.
5757
[string] $Repo
5858

59+
# The default value for the HTTP protocol version.
60+
[string] $HttpVersion
61+
5962
# The default value for the 'per_page' API parameter used in 'Get' functions that support paging.
6063
[int] $PerPage
6164

src/functions/public/API/Invoke-GitHubAPI.ps1

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
# Specifies the HTTP version used for the request.
5353
[Parameter()]
54-
[version] $HttpVersion = '2.0',
54+
[string] $HttpVersion,
5555

5656
# The file path to be used for the API request. This is used for uploading files.
5757
[Parameter()]
@@ -97,20 +97,25 @@
9797
$Token = $Context.Token
9898
Write-Debug "Token : [$Token]"
9999

100-
if ([string]::IsNullOrEmpty($TokenType)) {
101-
$TokenType = $Context.TokenType
100+
if ([string]::IsNullOrEmpty($HttpVersion)) {
101+
$HttpVersion = $Context.HttpVersion
102102
}
103-
Write-Debug "TokenType : [$($Context.TokenType)]"
103+
Write-Debug "HttpVersion: [$HttpVersion]"
104104

105105
if ([string]::IsNullOrEmpty($ApiBaseUri)) {
106106
$ApiBaseUri = $Context.ApiBaseUri
107107
}
108-
Write-Debug "ApiBaseUri: [$($Context.ApiBaseUri)]"
108+
Write-Debug "ApiBaseUri: [$ApiBaseUri]"
109109

110110
if ([string]::IsNullOrEmpty($ApiVersion)) {
111111
$ApiVersion = $Context.ApiVersion
112112
}
113-
Write-Debug "ApiVersion: [$($Context.ApiVersion)]"
113+
Write-Debug "ApiVersion: [$ApiVersion]"
114+
115+
if ([string]::IsNullOrEmpty($TokenType)) {
116+
$TokenType = $Context.TokenType
117+
}
118+
Write-Debug "TokenType : [$TokenType]"
114119

115120
switch ($TokenType) {
116121
'ghu' {

src/functions/public/Auth/Connect-GitHubAccount.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@
124124
[Alias('Repository')]
125125
[string] $Repo,
126126

127-
# API version used for API requests.
128-
[Parameter()]
129-
[string] $ApiVersion = '2022-11-28',
130-
131127
# The host to connect to. Can use $env:GITHUB_SERVER_URL to set the host, as the protocol is removed automatically.
132128
# Example: github.com, github.enterprise.com, msx.ghe.com
133129
[Parameter()]
@@ -166,6 +162,9 @@
166162
if (-not $HostName) {
167163
$HostName = $script:GitHub.Config.HostName
168164
}
165+
$httpVersion = $script:GitHub.Config.HttpVersion
166+
$perPage = $script:GitHub.Config.PerPage
167+
$ApiVersion = $script:GitHub.Config.ApiVersion
169168
$HostName = $HostName -replace '^https?://'
170169
$ApiBaseUri = "https://api.$HostName"
171170
$authType = $PSCmdlet.ParameterSetName
@@ -184,14 +183,15 @@
184183
}
185184

186185
$context = @{
187-
ApiBaseUri = [string]$ApiBaseUri
188-
ApiVersion = [string]$ApiVersion
189-
HostName = [string]$HostName
190-
AuthType = [string]$authType
191-
Enterprise = [string]$Enterprise
192-
Owner = [string]$Owner
193-
Repo = [string]$Repo
194-
PerPage = 100
186+
ApiBaseUri = [string]$ApiBaseUri
187+
ApiVersion = [string]$ApiVersion
188+
HostName = [string]$HostName
189+
HttpVersion = [string]$httpVersion
190+
PerPage = [int]$perPage
191+
AuthType = [string]$authType
192+
Enterprise = [string]$Enterprise
193+
Owner = [string]$Owner
194+
Repo = [string]$Repo
195195
}
196196

197197
Write-Verbose ($context | Format-Table | Out-String)

src/functions/public/Auth/Connect-GitHubApp.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
ApiBaseUri = [string]$Context.ApiBaseUri
116116
ApiVersion = [string]$Context.ApiVersion
117117
HostName = [string]$Context.HostName
118-
PerPage = 100
118+
HttpVersion = [string]$Context.HttpVersion
119+
PerPage = [int]$Context.PerPage
119120
ClientID = [string]$Context.ClientID
120121
InstallationID = [string]$installation.id
121122
Permissions = [pscustomobject]$installation.permissions

src/variables/private/GitHub.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
GitHubAppClientID = 'Iv1.f26b61bc99e69405'
1515
OAuthAppClientID = '7204ae9b0580f2cb8288'
1616
DefaultContext = ''
17+
ApiVersion = '2022-11-28'
18+
HttpVersion = '2.0'
19+
PerPage = 100
1720
}
1821
Config = $null
1922
Event = $null

tests/GitHub.Tests.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ param()
1212
BeforeAll {
1313
Get-SecretInfo | Remove-Secret
1414
Get-SecretVault | Unregister-SecretVault
15-
Import-Module -Name Context -Force -RequiredVersion 6.0.0
15+
Get-Module -ListAvailable -Name Context | Sort-Object -Property Version | Select-Object -Last 1 | Import-Module -Force
1616
}
1717

1818
Describe 'GitHub' {
@@ -23,9 +23,11 @@ Describe 'GitHub' {
2323
$config | Should -Not -BeNullOrEmpty
2424
}
2525
It 'Get-GitHubConfig - Gets a configuration item by name' {
26-
$config = Get-GitHubConfig -Name 'HostName'
27-
Write-Verbose ($config | Format-Table | Out-String) -Verbose
28-
$config | Should -Not -BeNullOrEmpty
26+
$config = Get-GitHubConfig
27+
$config.AccessTokenGracePeriodInHours | Should -Be 4
28+
$config.HostName | Should -Be 'github.com'
29+
$config.HttpVersion | Should -Be '2.0'
30+
$config.PerPage | Should -Be 100
2931
}
3032
It 'Set-GitHubConfig - Sets a configuration item' {
3133
Set-GitHubConfig -Name 'HostName' -Value 'msx.ghe.com'
@@ -66,6 +68,18 @@ Describe 'GitHub' {
6668
Write-Verbose (Get-GitHubContext | Out-String) -Verbose
6769
$context | Should -Not -BeNullOrEmpty
6870
}
71+
It 'Connect-GitHubAccount - Connects with default settings' {
72+
$context = Get-GitHubContext
73+
Write-Verbose ($context | Select-Object -Property * | Out-String) -Verbose
74+
$context | Should -Not -BeNullOrEmpty
75+
$context.ApiBaseUri | Should -Be 'https://api.github.com'
76+
$context.ApiVersion | Should -Be '2022-11-28'
77+
$context.AuthType | Should -Be 'IAT'
78+
$context.HostName | Should -Be 'github.com'
79+
$context.HttpVersion | Should -Be '2.0'
80+
$context.TokenType | Should -Be 'ghs'
81+
$context.Name | Should -Be 'github.com/github-actions/Organization/PSModule'
82+
}
6983
It 'Disconnect-GitHubAccount - Disconnects the context from the pipeline' {
7084
$context = Get-GitHubContext
7185
{ $context | Disconnect-GitHubAccount } | Should -Not -Throw

0 commit comments

Comments
 (0)