Skip to content

Commit 5de98ee

Browse files
🩹 [Patch]: Add tests for Meta + GitHubAPI fixes (#195)
## Description This pull request includes several changes to improve the handling of GitHub API responses, enhance logging, and refine error messages. The most important changes involve adding detailed logging, handling different content types in API responses, and refactoring context resolution. ### Improvements to API response handling: * [`src/functions/public/API/Invoke-GitHubAPI.ps1`](diffhunk://#diff-9285dd3cdd5467d93c8e68c989041171e17993971649b877dce001b1861b2c39R182-R206): Added detailed logging for API responses and handling of various content types, including JSON, plain text, and custom streams. ### Enhancements to logging: * `src/functions/public/Meta/Get-GitHubApiVersion.ps1`, `src/functions/public/Meta/Get-GitHubMeta.ps1`, `src/functions/public/Meta/Get-GitHubOctocat.ps1`, `src/functions/public/Meta/Get-GitHubRoot.ps1`, `src/functions/public/Meta/Get-GitHubZen.ps1`: Added `begin`, `process`, and `end` blocks with verbose logging for better traceability. [[1]](diffhunk://#diff-79713af1fd3e68c4553acbe86eb87773288d3018711828a0f5392f92771a01a8R26-R32) [[2]](diffhunk://#diff-79713af1fd3e68c4553acbe86eb87773288d3018711828a0f5392f92771a01a8R42-R46) [[3]](diffhunk://#diff-bd717cea01612bd72bed0f327a6962871ace61b88d24887ea3b8576c24fa3485R34-R40) [[4]](diffhunk://#diff-bd717cea01612bd72bed0f327a6962871ace61b88d24887ea3b8576c24fa3485R51-R55) [[5]](diffhunk://#diff-ed90ed77fe68bef3c2ff2f7982a20112cb30413d92e889f12a5673ca294c6955R37-R43) [[6]](diffhunk://#diff-ed90ed77fe68bef3c2ff2f7982a20112cb30413d92e889f12a5673ca294c6955R59-R63) [[7]](diffhunk://#diff-69f2760d130ff5f36917becadeef5b836cee481db5b38f464fdfdbb161949c77R25-R31) [[8]](diffhunk://#diff-69f2760d130ff5f36917becadeef5b836cee481db5b38f464fdfdbb161949c77R41-R45) [[9]](diffhunk://#diff-75c2a6ba6de9154ed68703398de86936973b6a1f02978992e53b1957926e00fbR25-R31) [[10]](diffhunk://#diff-75c2a6ba6de9154ed68703398de86936973b6a1f02978992e53b1957926e00fbR42-R46) ### Refactoring context resolution: * [`src/functions/private/Auth/Context/Resolve-GitHubContext.ps1`](diffhunk://#diff-096a5462baf72d04ba840145e46b05d4651afb1cce7c439631f4442c239f63e1L35-R35): Refactored context resolution by removing verbose logging of the context and initializing GitHub configuration. [[1]](diffhunk://#diff-096a5462baf72d04ba840145e46b05d4651afb1cce7c439631f4442c239f63e1L35-R35) [[2]](diffhunk://#diff-096a5462baf72d04ba840145e46b05d4651afb1cce7c439631f4442c239f63e1L47-R46) [[3]](diffhunk://#diff-096a5462baf72d04ba840145e46b05d4651afb1cce7c439631f4442c239f63e1L62-L64) ### Additional changes: * [`src/functions/private/Config/Initialize-GitHubConfig.ps1`](diffhunk://#diff-0bd6f61981cdae153b550552b394da11c794e8ad8ae819fbb7bb702022a02e5dL32-R49): Updated verbose messages and logic for initializing GitHub configuration. * [`tests/GitHub.Tests.ps1`](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cR114-R140): Added tests for various meta commands to ensure they return non-empty responses. ## 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 2d3e1bd commit 5de98ee

File tree

11 files changed

+153
-55
lines changed

11 files changed

+153
-55
lines changed

examples/CallingAPIs.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ Invoke-GitHubAPI -ApiEndpoint /zen
4343
# Most complex example - output is the entire web response
4444
$context = Get-GitHubContext
4545
Invoke-WebRequest -Uri "https://api.$($context.HostName)/zen" -Token ($context.Token) -Authentication Bearer
46+
Invoke-RestMethod -Uri "https://api.$($context.HostName)/octocat" -Token ($context.Token) -Authentication Bearer
4647
#endregion

src/functions/private/Auth/Context/Resolve-GitHubContext.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
begin {
3333
$commandName = $MyInvocation.MyCommand.Name
3434
Write-Verbose "[$commandName] - Start"
35-
Write-Verbose 'Context:'
36-
$Context | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
35+
Initialize-GitHubConfig
3736
}
3837

3938
process {
@@ -44,7 +43,7 @@
4443
}
4544

4645
if (-not $Context) {
47-
throw "Context [$contextName] not found. Please provide a valid context or log in using 'Connect-GitHub'."
46+
throw "Please provide a valid context or log in using 'Connect-GitHub'."
4847
}
4948

5049
# switch ($Context.Type) {
@@ -59,9 +58,6 @@
5958
# }
6059
# }
6160
# }
62-
63-
Write-Verbose 'Resolved Context:'
64-
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
6561
}
6662

6763
end {

src/functions/private/Config/Initialize-GitHubConfig.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,24 @@
2929
}
3030

3131
process {
32-
Write-Verbose "GitHubConfig initialized: [$($script:GitHub.Initialized)]"
33-
Write-Verbose "Force: [$Force]"
34-
if (-not $script:GitHub.Initialized -or $Force) {
32+
Write-Verbose "GitHubConfig ID: [$($script:GitHub.Config.ID)]"
33+
Write-Verbose "Force: [$Force]"
34+
if (-not $script:GitHub.Config.ID -or $Force) {
3535
try {
36+
Write-Verbose 'Attempt to load the stored GitHubConfig from ContextVault'
3637
$context = [GitHubConfig](Get-Context -ID $script:GitHub.Config.ID)
3738
if (-not $context -or $Force) {
38-
Write-Verbose "Loading GitHubConfig from defaults"
39+
Write-Verbose 'No stored config found. Loading GitHubConfig from defaults'
3940
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $script:GitHub.DefaultConfig -PassThru
4041
}
42+
Write-Verbose 'GitHubConfig loaded into memory.'
4143
$script:GitHub.Config = [GitHubConfig]$context
42-
$script:GitHub.Initialized = $true
4344
} catch {
4445
Write-Error $_
4546
throw 'Failed to initialize GitHub config'
4647
}
48+
} else {
49+
Write-Verbose 'GitHubConfig already initialized and available in memory.'
4750
}
4851
}
4952

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,31 @@
179179
Write-Debug 'Response headers:'
180180
$headers | Out-String -Stream | ForEach-Object { Write-Debug $_ }
181181
Write-Debug '---------------------------'
182-
$results = $response.Content | ConvertFrom-Json
182+
Write-Debug 'Response:'
183+
$response | Out-String -Stream | ForEach-Object { Write-Debug $_ }
184+
Write-Debug '---------------------------'
185+
Write-Debug $headers.'Content-Type'
186+
switch -Regex ($headers.'Content-Type') {
187+
'application/json' {
188+
$results = $response.Content | ConvertFrom-Json
189+
}
190+
'application/vnd.github.v3+json' {
191+
$results = $response.Content | ConvertFrom-Json
192+
}
193+
'text/plain' {
194+
$results = $response.Content
195+
}
196+
'application/octocat-stream' {
197+
[byte[]]$byteArray = $response.Content
198+
$results = [System.Text.Encoding]::UTF8.GetString($byteArray)
199+
}
200+
default {
201+
Write-Warning "Unknown content type: $($headers.'Content-Type')"
202+
Write-Warning 'Please report this issue!'
203+
[byte[]]$byteArray = $response.Content
204+
$results = [System.Text.Encoding]::UTF8.GetString($byteArray)
205+
}
206+
}
183207
[pscustomobject]@{
184208
Request = $APICall
185209
Response = $results

src/functions/public/Meta/Get-GitHubApiVersion.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@
2323
[object] $Context = (Get-GitHubContext)
2424
)
2525

26-
$Context = Resolve-GitHubContext -Context $Context
27-
28-
$inputObject = @{
29-
Context = $Context
30-
ApiEndpoint = '/versions'
31-
Method = 'GET'
26+
begin {
27+
$commandName = $MyInvocation.MyCommand.Name
28+
Write-Verbose "[$commandName] - Start"
29+
$Context = Resolve-GitHubContext -Context $Context
3230
}
3331

34-
Invoke-GitHubAPI @inputObject | ForEach-Object {
35-
Write-Output $_.Response
32+
process {
33+
$inputObject = @{
34+
Context = $Context
35+
ApiEndpoint = '/versions'
36+
Method = 'GET'
37+
}
38+
39+
Invoke-GitHubAPI @inputObject | ForEach-Object {
40+
Write-Output $_.Response
41+
}
3642
}
3743

44+
end {
45+
Write-Verbose "[$commandName] - End"
46+
}
3847
}

src/functions/public/Meta/Get-GitHubMeta.ps1

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,25 @@
3131
[object] $Context = (Get-GitHubContext)
3232
)
3333

34-
$Context = Resolve-GitHubContext -Context $Context
34+
begin {
35+
$commandName = $MyInvocation.MyCommand.Name
36+
Write-Verbose "[$commandName] - Start"
37+
$Context = Resolve-GitHubContext -Context $Context
38+
}
39+
40+
process {
41+
$inputObject = @{
42+
Context = $Context
43+
ApiEndpoint = '/meta'
44+
Method = 'GET'
45+
}
3546

36-
$inputObject = @{
37-
Context = $Context
38-
ApiEndpoint = '/meta'
39-
Method = 'GET'
47+
Invoke-GitHubAPI @inputObject | ForEach-Object {
48+
Write-Output $_.Response
49+
}
4050
}
4151

42-
Invoke-GitHubAPI @inputObject | ForEach-Object {
43-
Write-Output $_.Response
52+
end {
53+
Write-Verbose "[$commandName] - End"
4454
}
4555
}

src/functions/public/Meta/Get-GitHubOctocat.ps1

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,30 @@
3434
[object] $Context = (Get-GitHubContext)
3535
)
3636

37-
$Context = Resolve-GitHubContext -Context $Context
38-
39-
$body = @{
40-
s = $S
37+
begin {
38+
$commandName = $MyInvocation.MyCommand.Name
39+
Write-Verbose "[$commandName] - Start"
40+
$Context = Resolve-GitHubContext -Context $Context
4141
}
4242

43-
$inputObject = @{
44-
Context = $Context
45-
APIEndpoint = '/octocat'
46-
Method = 'GET'
47-
Body = $body
43+
process {
44+
$body = @{
45+
s = $S
46+
}
47+
48+
$inputObject = @{
49+
Context = $Context
50+
APIEndpoint = '/octocat'
51+
Method = 'GET'
52+
Body = $body
53+
}
54+
55+
Invoke-GitHubAPI @inputObject | ForEach-Object {
56+
Write-Output $_.Response
57+
}
4858
}
4959

50-
Invoke-GitHubAPI @inputObject | ForEach-Object {
51-
Write-Output $_.Response
60+
end {
61+
Write-Verbose "[$commandName] - End"
5262
}
5363
}

src/functions/public/Meta/Get-GitHubRoot.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,25 @@
2222
[object] $Context = (Get-GitHubContext)
2323
)
2424

25-
$Context = Resolve-GitHubContext -Context $Context
26-
27-
$inputObject = @{
28-
Context = $Context
29-
APIEndpoint = '/'
30-
Method = 'GET'
25+
begin {
26+
$commandName = $MyInvocation.MyCommand.Name
27+
Write-Verbose "[$commandName] - Start"
28+
$Context = Resolve-GitHubContext -Context $Context
3129
}
3230

33-
Invoke-GitHubAPI @inputObject | ForEach-Object {
34-
Write-Output $_.Response
31+
process {
32+
$inputObject = @{
33+
Context = $Context
34+
APIEndpoint = '/'
35+
Method = 'GET'
36+
}
37+
38+
Invoke-GitHubAPI @inputObject | ForEach-Object {
39+
Write-Output $_.Response
40+
}
3541
}
3642

43+
end {
44+
Write-Verbose "[$commandName] - End"
45+
}
3746
}

src/functions/public/Meta/Get-GitHubZen.ps1

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,25 @@
2222
[object] $Context = (Get-GitHubContext)
2323
)
2424

25-
$Context = Resolve-GitHubContext -Context $Context
25+
begin {
26+
$commandName = $MyInvocation.MyCommand.Name
27+
Write-Verbose "[$commandName] - Start"
28+
$Context = Resolve-GitHubContext -Context $Context
29+
}
30+
31+
process {
32+
$inputObject = @{
33+
Context = $Context
34+
APIEndpoint = '/zen'
35+
Method = 'GET'
36+
}
2637

27-
$inputObject = @{
28-
Context = $Context
29-
APIEndpoint = '/zen'
30-
Method = 'GET'
38+
Invoke-GitHubAPI @inputObject | ForEach-Object {
39+
Write-Output $_.Response
40+
}
3141
}
3242

33-
Invoke-GitHubAPI @inputObject | ForEach-Object {
34-
Write-Output $_.Response
43+
end {
44+
Write-Verbose "[$commandName] - End"
3545
}
3646
}

src/variables/private/Config.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
$script:GitHub = [pscustomobject]@{
2-
Initialized = $false
32
TokenPrefixPattern = '(?<=^(ghu|gho|ghs|github_pat|ghp)).*'
43
EnvironmentType = if ($env:GITHUB_ACTIONS -eq 'true') {
54
'GHA'

0 commit comments

Comments
 (0)