Skip to content

Commit

Permalink
🩹 [Patch]: Add tests for Meta + GitHubAPI fixes (#195)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
MariusStorhaug authored Dec 9, 2024
1 parent 2d3e1bd commit 5de98ee
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 55 deletions.
1 change: 1 addition & 0 deletions examples/CallingAPIs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ Invoke-GitHubAPI -ApiEndpoint /zen
# Most complex example - output is the entire web response
$context = Get-GitHubContext
Invoke-WebRequest -Uri "https://api.$($context.HostName)/zen" -Token ($context.Token) -Authentication Bearer
Invoke-RestMethod -Uri "https://api.$($context.HostName)/octocat" -Token ($context.Token) -Authentication Bearer
#endregion
8 changes: 2 additions & 6 deletions src/functions/private/Auth/Context/Resolve-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Verbose 'Context:'
$Context | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
Initialize-GitHubConfig
}

process {
Expand All @@ -44,7 +43,7 @@
}

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

# switch ($Context.Type) {
Expand All @@ -59,9 +58,6 @@
# }
# }
# }

Write-Verbose 'Resolved Context:'
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
}

end {
Expand Down
13 changes: 8 additions & 5 deletions src/functions/private/Config/Initialize-GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@
}

process {
Write-Verbose "GitHubConfig initialized: [$($script:GitHub.Initialized)]"
Write-Verbose "Force: [$Force]"
if (-not $script:GitHub.Initialized -or $Force) {
Write-Verbose "GitHubConfig ID: [$($script:GitHub.Config.ID)]"
Write-Verbose "Force: [$Force]"
if (-not $script:GitHub.Config.ID -or $Force) {
try {
Write-Verbose 'Attempt to load the stored GitHubConfig from ContextVault'
$context = [GitHubConfig](Get-Context -ID $script:GitHub.Config.ID)
if (-not $context -or $Force) {
Write-Verbose "Loading GitHubConfig from defaults"
Write-Verbose 'No stored config found. Loading GitHubConfig from defaults'
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $script:GitHub.DefaultConfig -PassThru
}
Write-Verbose 'GitHubConfig loaded into memory.'
$script:GitHub.Config = [GitHubConfig]$context
$script:GitHub.Initialized = $true
} catch {
Write-Error $_
throw 'Failed to initialize GitHub config'
}
} else {
Write-Verbose 'GitHubConfig already initialized and available in memory.'
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/functions/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,31 @@
Write-Debug 'Response headers:'
$headers | Out-String -Stream | ForEach-Object { Write-Debug $_ }
Write-Debug '---------------------------'
$results = $response.Content | ConvertFrom-Json
Write-Debug 'Response:'
$response | Out-String -Stream | ForEach-Object { Write-Debug $_ }
Write-Debug '---------------------------'
Write-Debug $headers.'Content-Type'
switch -Regex ($headers.'Content-Type') {
'application/json' {
$results = $response.Content | ConvertFrom-Json
}
'application/vnd.github.v3+json' {
$results = $response.Content | ConvertFrom-Json
}
'text/plain' {
$results = $response.Content
}
'application/octocat-stream' {
[byte[]]$byteArray = $response.Content
$results = [System.Text.Encoding]::UTF8.GetString($byteArray)
}
default {
Write-Warning "Unknown content type: $($headers.'Content-Type')"
Write-Warning 'Please report this issue!'
[byte[]]$byteArray = $response.Content
$results = [System.Text.Encoding]::UTF8.GetString($byteArray)
}
}
[pscustomobject]@{
Request = $APICall
Response = $results
Expand Down
25 changes: 17 additions & 8 deletions src/functions/public/Meta/Get-GitHubApiVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
ApiEndpoint = '/versions'
Method = 'GET'
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
process {
$inputObject = @{
Context = $Context
ApiEndpoint = '/versions'
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

end {
Write-Verbose "[$commandName] - End"
}
}
24 changes: 17 additions & 7 deletions src/functions/public/Meta/Get-GitHubMeta.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,25 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

process {
$inputObject = @{
Context = $Context
ApiEndpoint = '/meta'
Method = 'GET'
}

$inputObject = @{
Context = $Context
ApiEndpoint = '/meta'
Method = 'GET'
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
end {
Write-Verbose "[$commandName] - End"
}
}
32 changes: 21 additions & 11 deletions src/functions/public/Meta/Get-GitHubOctocat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,30 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$body = @{
s = $S
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

$inputObject = @{
Context = $Context
APIEndpoint = '/octocat'
Method = 'GET'
Body = $body
process {
$body = @{
s = $S
}

$inputObject = @{
Context = $Context
APIEndpoint = '/octocat'
Method = 'GET'
Body = $body
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
end {
Write-Verbose "[$commandName] - End"
}
}
25 changes: 17 additions & 8 deletions src/functions/public/Meta/Get-GitHubRoot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
APIEndpoint = '/'
Method = 'GET'
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
process {
$inputObject = @{
Context = $Context
APIEndpoint = '/'
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

end {
Write-Verbose "[$commandName] - End"
}
}
24 changes: 17 additions & 7 deletions src/functions/public/Meta/Get-GitHubZen.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

process {
$inputObject = @{
Context = $Context
APIEndpoint = '/zen'
Method = 'GET'
}

$inputObject = @{
Context = $Context
APIEndpoint = '/zen'
Method = 'GET'
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
end {
Write-Verbose "[$commandName] - End"
}
}
1 change: 0 additions & 1 deletion src/variables/private/Config.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$script:GitHub = [pscustomobject]@{
Initialized = $false
TokenPrefixPattern = '(?<=^(ghu|gho|ghs|github_pat|ghp)).*'
EnvironmentType = if ($env:GITHUB_ACTIONS -eq 'true') {
'GHA'
Expand Down
29 changes: 28 additions & 1 deletion tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Describe 'GitHub' {

It 'Can connect to all GitHub App Installations' {
{ Connect-GitHubApp } | Should -Not -Throw
Write-Verbose "Default context:" -Verbose
Write-Verbose 'Default context:' -Verbose
Write-Verbose (Get-GitHubContext | Out-String) -Verbose
Write-Verbose 'All contexts:' -Verbose
Write-Verbose (Get-GitHubContext -ListAvailable | Out-String) -Verbose
Expand Down Expand Up @@ -111,6 +111,33 @@ Describe 'GitHub' {
$config.ID | Should -Be 'PSModule.GitHub'
}
}
Context 'Meta' {
It 'Get-GitHubRoot' {
$root = Get-GitHubRoot
Write-Verbose ($root | Format-Table | Out-String) -Verbose
$root | Should -Not -BeNullOrEmpty
}
It 'Get-GitHubApiVersion' {
$apiVersion = Get-GitHubApiVersion
Write-Verbose ($apiVersion | Format-Table | Out-String) -Verbose
$apiVersion | Should -Not -BeNullOrEmpty
}
It 'Get-GitHubMeta' {
$meta = Get-GitHubMeta
Write-Verbose ($meta | Format-Table | Out-String) -Verbose
$meta | Should -Not -BeNullOrEmpty
}
It 'Get-GitHubOctocat' {
$octocat = Get-GitHubOctocat
Write-Verbose ($octocat | Format-Table | Out-String) -Verbose
$octocat | Should -Not -BeNullOrEmpty
}
It 'Get-GitHubZen' {
$zen = Get-GitHubZen
Write-Verbose ($zen | Format-Table | Out-String) -Verbose
$zen | Should -Not -BeNullOrEmpty
}
}
Context 'Git' {
It 'Set-GitHubGitConfig sets the Git configuration' {
{ Set-GitHubGitConfig } | Should -Not -Throw
Expand Down

0 comments on commit 5de98ee

Please sign in to comment.