From 2f61d10bf2f7afe3e5a9ff44c3dea4ed2d75c385 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 9 Dec 2024 22:42:20 +0100 Subject: [PATCH 1/3] Run tests on Meta --- .../public/Meta/Get-GitHubApiVersion.ps1 | 25 ++++++++++----- src/functions/public/Meta/Get-GitHubMeta.ps1 | 24 ++++++++++---- .../public/Meta/Get-GitHubOctocat.ps1 | 32 ++++++++++++------- src/functions/public/Meta/Get-GitHubRoot.ps1 | 25 ++++++++++----- src/functions/public/Meta/Get-GitHubZen.ps1 | 24 ++++++++++---- tests/GitHub.Tests.ps1 | 29 ++++++++++++++++- 6 files changed, 117 insertions(+), 42 deletions(-) diff --git a/src/functions/public/Meta/Get-GitHubApiVersion.ps1 b/src/functions/public/Meta/Get-GitHubApiVersion.ps1 index 32dc5282c..baedc6a3d 100644 --- a/src/functions/public/Meta/Get-GitHubApiVersion.ps1 +++ b/src/functions/public/Meta/Get-GitHubApiVersion.ps1 @@ -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" + } } diff --git a/src/functions/public/Meta/Get-GitHubMeta.ps1 b/src/functions/public/Meta/Get-GitHubMeta.ps1 index ac8a74745..25ac1ca28 100644 --- a/src/functions/public/Meta/Get-GitHubMeta.ps1 +++ b/src/functions/public/Meta/Get-GitHubMeta.ps1 @@ -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" } } diff --git a/src/functions/public/Meta/Get-GitHubOctocat.ps1 b/src/functions/public/Meta/Get-GitHubOctocat.ps1 index 6b2996839..1f8f3120f 100644 --- a/src/functions/public/Meta/Get-GitHubOctocat.ps1 +++ b/src/functions/public/Meta/Get-GitHubOctocat.ps1 @@ -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" } } diff --git a/src/functions/public/Meta/Get-GitHubRoot.ps1 b/src/functions/public/Meta/Get-GitHubRoot.ps1 index a7dd3fd8b..9a1129a5b 100644 --- a/src/functions/public/Meta/Get-GitHubRoot.ps1 +++ b/src/functions/public/Meta/Get-GitHubRoot.ps1 @@ -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" + } } diff --git a/src/functions/public/Meta/Get-GitHubZen.ps1 b/src/functions/public/Meta/Get-GitHubZen.ps1 index 3a26a2397..54baf46ae 100644 --- a/src/functions/public/Meta/Get-GitHubZen.ps1 +++ b/src/functions/public/Meta/Get-GitHubZen.ps1 @@ -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" } } diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 6917f459a..163d22e5f 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -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 @@ -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 From 3bf7e1039af7f367b4d22e79023f53b944974249 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 9 Dec 2024 23:42:32 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9A=80=20[Feature]:=20Enhance=20GitHu?= =?UTF-8?q?b=20context=20initialization=20and=20response=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Auth/Context/Resolve-GitHubContext.ps1 | 8 ++----- .../Config/Initialize-GitHubConfig.ps1 | 13 +++++++----- src/functions/public/API/Invoke-GitHubAPI.ps1 | 21 ++++++++++++++++++- src/variables/private/Config.ps1 | 1 - 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 index 1c4c5393d..1302d8068 100644 --- a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 @@ -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 { @@ -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) { @@ -59,9 +58,6 @@ # } # } # } - - Write-Verbose 'Resolved Context:' - $Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ } } end { diff --git a/src/functions/private/Config/Initialize-GitHubConfig.ps1 b/src/functions/private/Config/Initialize-GitHubConfig.ps1 index c1aac7f24..4bbc8bab6 100644 --- a/src/functions/private/Config/Initialize-GitHubConfig.ps1 +++ b/src/functions/private/Config/Initialize-GitHubConfig.ps1 @@ -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.' } } diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index fa748b2ae..eb78c7265 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -179,7 +179,26 @@ 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 + } + default { + Write-Warning "Unknown content type: $($headers.'Content-Type')" + Write-Warning 'Please report this issue!' + $results = $response.Content + } + } [pscustomobject]@{ Request = $APICall Response = $results diff --git a/src/variables/private/Config.ps1 b/src/variables/private/Config.ps1 index 05744503a..38b3fdf06 100644 --- a/src/variables/private/Config.ps1 +++ b/src/variables/private/Config.ps1 @@ -1,5 +1,4 @@ $script:GitHub = [pscustomobject]@{ - Initialized = $false TokenPrefixPattern = '(?<=^(ghu|gho|ghs|github_pat|ghp)).*' EnvironmentType = if ($env:GITHUB_ACTIONS -eq 'true') { 'GHA' From 736ed99d4d6db1a396ca264903b2f010ad11f99e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 10 Dec 2024 00:15:30 +0100 Subject: [PATCH 3/3] API fix --- examples/CallingAPIs.ps1 | 1 + src/functions/public/API/Invoke-GitHubAPI.ps1 | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/CallingAPIs.ps1 b/examples/CallingAPIs.ps1 index b1488e439..c38d29dce 100644 --- a/examples/CallingAPIs.ps1 +++ b/examples/CallingAPIs.ps1 @@ -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 diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index eb78c7265..c8733ad93 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -193,10 +193,15 @@ '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!' - $results = $response.Content + [byte[]]$byteArray = $response.Content + $results = [System.Text.Encoding]::UTF8.GetString($byteArray) } } [pscustomobject]@{