Skip to content

Commit

Permalink
🚀 [Feature]: Enhance GitHub context initialization and response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Dec 9, 2024
1 parent 2f61d10 commit 3bf7e10
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
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
21 changes: 20 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,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
Expand Down
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

0 comments on commit 3bf7e10

Please sign in to comment.