Skip to content

Commit 07138a2

Browse files
Harden GitHub API helper for local/module execution
Fix module-qualified API invocation, unwrap GitHub module response envelopes, correct workflow URI interpolation, and fall back to anonymous mode when no context exists. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 4ec9d95 commit 07138a2

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

.github/actions/update-index/src/Helper.psm1

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,60 @@ function Invoke-GitHubApi {
168168
)
169169

170170
try {
171-
Invoke-GitHubAPI -Method GET -Uri $Uri -ErrorAction Stop
171+
$apiParameters = @{
172+
Method = 'GET'
173+
Uri = $Uri
174+
ErrorAction = 'Stop'
175+
}
176+
177+
$availableContexts = Get-GitHubContext -ListAvailable -ErrorAction SilentlyContinue
178+
if ($null -eq $availableContexts -or $availableContexts.Count -eq 0) {
179+
$apiParameters.Anonymous = $true
180+
}
181+
182+
$rawResponse = GitHub\Invoke-GitHubAPI @apiParameters
183+
if ($null -eq $rawResponse) {
184+
return $null
185+
}
186+
187+
if ($rawResponse -is [array] -and $rawResponse.Count -gt 0 -and $rawResponse[0].PSObject.Properties['Response']) {
188+
$payloadItems = @()
189+
foreach ($item in $rawResponse) {
190+
if ($null -eq $item.Response) {
191+
continue
192+
}
193+
194+
if ($item.Response -is [array]) {
195+
$payloadItems += $item.Response
196+
}
197+
else {
198+
$payloadItems += , $item.Response
199+
}
200+
}
201+
202+
if ($payloadItems.Count -eq 0) {
203+
return $null
204+
}
205+
if ($payloadItems.Count -eq 1) {
206+
return $payloadItems[0]
207+
}
208+
209+
return $payloadItems
210+
}
211+
212+
if ($rawResponse.PSObject.Properties['Response']) {
213+
return $rawResponse.Response
214+
}
215+
216+
return $rawResponse
172217
}
173218
catch {
174219
$statusCode = $null
175220
if ($_.Exception.Response) {
176221
$statusCode = $_.Exception.Response.StatusCode.value__
177222
}
178-
if ($statusCode -eq 404) {
223+
$errorText = $_ | Out-String
224+
if ($statusCode -eq 404 -or $errorText -match 'StatusCode\s*:\s*404' -or $errorText -match '\(404\)') {
179225
return $null
180226
}
181227

@@ -331,7 +377,7 @@ function Get-WorkflowReference {
331377

332378
foreach ($workflowPath in @('.github/workflows/workflow.yml', '.github/workflows/workflow.yaml')) {
333379
$encodedRef = [uri]::EscapeDataString($DefaultBranch)
334-
$uri = "https://api.github.com/repos/$Owner/$Name/contents/$workflowPath?ref=$encodedRef"
380+
$uri = "https://api.github.com/repos/$Owner/$Name/contents/${workflowPath}?ref=$encodedRef"
335381
$response = Invoke-GitHubApi -Uri $uri
336382
if ($null -eq $response) {
337383
continue

0 commit comments

Comments
 (0)