Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 [Patch]: Add tests for Meta + GitHubAPI fixes #195

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading