Skip to content

Commit d6193e8

Browse files
Add workflow resolution diagnostics
Log discovery strategy, listing shape, canonical fallback hits, and final resolution outcome for Process-PSModule ref detection. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 4fa296f commit d6193e8

1 file changed

Lines changed: 71 additions & 21 deletions

File tree

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

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ function Invoke-GitHubApi {
209209
[CmdletBinding()]
210210
param(
211211
[Parameter(Mandatory)]
212-
[string] $Uri
212+
[string] $Uri,
213+
[Parameter()]
214+
[switch] $Anonymous
213215
)
214216

215217
try {
@@ -219,9 +221,13 @@ function Invoke-GitHubApi {
219221
ErrorAction = 'Stop'
220222
}
221223

222-
$availableContexts = Get-GitHubContext -ListAvailable -ErrorAction SilentlyContinue
223-
if ($null -eq $availableContexts -or $availableContexts.Count -eq 0) {
224+
if ($Anonymous) {
224225
$apiParameters.Anonymous = $true
226+
} else {
227+
$availableContexts = Get-GitHubContext -ListAvailable -ErrorAction SilentlyContinue
228+
if ($null -eq $availableContexts -or $availableContexts.Count -eq 0) {
229+
$apiParameters.Anonymous = $true
230+
}
225231
}
226232

227233
$rawResponse = GitHub\Invoke-GitHubAPI @apiParameters
@@ -477,24 +483,49 @@ function Get-WorkflowReference {
477483
$workflowFolderPath = '.github/workflows'
478484
$workflowFolderUri = "https://api.github.com/repos/$Owner/$Name/contents/$workflowFolderPath?ref=$encodedRef"
479485
Write-Host "Discovering workflow files under [$workflowFolderPath] for [$Owner/$Name] on [$DefaultBranch]"
486+
$workflowDiscoveryStrategy = 'folder-listing'
480487
$workflowEntries = Invoke-GitHubApi -Uri $workflowFolderUri
481488
if ($null -eq $workflowEntries) {
482-
Write-Host "Workflow folder not found: [$workflowFolderPath]"
483-
return 'N/A'
489+
Write-Host "Workflow folder lookup failed with current auth; retrying anonymously for [$workflowFolderPath]"
490+
$workflowEntries = Invoke-GitHubApi -Uri $workflowFolderUri -Anonymous
491+
}
492+
493+
$workflowFiles = @()
494+
$prefetchedWorkflowResponses = @{}
495+
if ($null -ne $workflowEntries) {
496+
$workflowEntryItems = @($workflowEntries)
497+
Write-Host "Workflow folder listing returned [$($workflowEntryItems.Count)] item(s)"
498+
$workflowFiles = @(
499+
$workflowEntryItems |
500+
Where-Object {
501+
(Get-PropertyValue -InputObject $_ -Names @('type') -Default '') -eq 'file' -and
502+
[string](Get-PropertyValue -InputObject $_ -Names @('name') -Default '') -match '\.ya?ml$'
503+
} |
504+
Sort-Object { [string](Get-PropertyValue -InputObject $_ -Names @('name') -Default '') }
505+
)
484506
}
485507

486-
$workflowFiles = @(
487-
$workflowEntries |
488-
Where-Object {
489-
(Get-PropertyValue -InputObject $_ -Names @('type') -Default '') -eq 'file' -and
490-
[string](Get-PropertyValue -InputObject $_ -Names @('name') -Default '') -match '\.ya?ml$'
491-
} |
492-
Sort-Object { [string](Get-PropertyValue -InputObject $_ -Names @('name') -Default '') }
493-
)
494-
495508
if ($workflowFiles.Count -eq 0) {
496-
Write-Host 'No workflow .yml/.yaml files found'
497-
return 'N/A'
509+
$workflowDiscoveryStrategy = 'canonical-fallback'
510+
Write-Host "Workflow folder listing unavailable or empty; trying canonical workflow files directly"
511+
foreach ($canonicalWorkflowPath in @('.github/workflows/Process-PSModule.yml', '.github/workflows/Process-PSModule.yaml')) {
512+
$canonicalWorkflowUri = "https://api.github.com/repos/$Owner/$Name/contents/${canonicalWorkflowPath}?ref=$encodedRef"
513+
$canonicalWorkflowResponse = Invoke-GitHubApi -Uri $canonicalWorkflowUri
514+
if ($null -eq $canonicalWorkflowResponse) {
515+
$canonicalWorkflowResponse = Invoke-GitHubApi -Uri $canonicalWorkflowUri -Anonymous
516+
}
517+
if ($null -eq $canonicalWorkflowResponse) {
518+
continue
519+
}
520+
521+
Write-Host "Canonical workflow candidate found: [$canonicalWorkflowPath]"
522+
$prefetchedWorkflowResponses[$canonicalWorkflowPath] = $canonicalWorkflowResponse
523+
$workflowFiles += [pscustomobject]@{
524+
name = [IO.Path]::GetFileName($canonicalWorkflowPath)
525+
path = $canonicalWorkflowPath
526+
type = 'file'
527+
}
528+
}
498529
}
499530

500531
if ($workflowFiles.Count -eq 1) {
@@ -506,6 +537,7 @@ function Get-WorkflowReference {
506537
Write-Host " - $workflowName"
507538
}
508539
}
540+
Write-Host "Workflow discovery strategy used: [$workflowDiscoveryStrategy]"
509541

510542
$candidateWorkflowFiles = $workflowFiles
511543
if ($workflowFiles.Count -gt 1) {
@@ -522,19 +554,29 @@ function Get-WorkflowReference {
522554
}
523555

524556
$resolvedRefs = @()
557+
$foundWorkflowFile = $false
525558
foreach ($workflowFile in $candidateWorkflowFiles) {
526559
$workflowPath = [string](Get-PropertyValue -InputObject $workflowFile -Names @('path') -Default '')
527560
if ([string]::IsNullOrWhiteSpace($workflowPath)) {
528561
continue
529562
}
530563

531564
Write-Host "Checking workflow path [$workflowPath] for [$Owner/$Name] on [$DefaultBranch]"
532-
$uri = "https://api.github.com/repos/$Owner/$Name/contents/${workflowPath}?ref=$encodedRef"
533-
$response = Invoke-GitHubApi -Uri $uri
534-
if ($null -eq $response) {
535-
Write-Host "Workflow path not found: [$workflowPath]"
536-
continue
565+
if ($prefetchedWorkflowResponses.ContainsKey($workflowPath)) {
566+
$response = $prefetchedWorkflowResponses[$workflowPath]
567+
} else {
568+
$uri = "https://api.github.com/repos/$Owner/$Name/contents/${workflowPath}?ref=$encodedRef"
569+
$response = Invoke-GitHubApi -Uri $uri
570+
if ($null -eq $response) {
571+
Write-Host "Workflow path lookup failed with current auth; retrying anonymously for [$workflowPath]"
572+
$response = Invoke-GitHubApi -Uri $uri -Anonymous
573+
if ($null -eq $response) {
574+
Write-Host "Workflow path not found: [$workflowPath]"
575+
continue
576+
}
577+
}
537578
}
579+
$foundWorkflowFile = $true
538580

539581
$content = Get-PropertyValue -InputObject $response -Names @('content')
540582
if ([string]::IsNullOrWhiteSpace([string]$content)) {
@@ -564,16 +606,24 @@ function Get-WorkflowReference {
564606
}
565607
}
566608

609+
if (-not $foundWorkflowFile) {
610+
Write-Host "No workflow files could be fetched for [$Owner/$Name]"
611+
return 'N/A'
612+
}
613+
567614
$uniqueResolvedRefs = @($resolvedRefs | Select-Object -Unique)
568615
if ($uniqueResolvedRefs.Count -eq 1) {
616+
Write-Host "Workflow reference resolution succeeded using strategy [$workflowDiscoveryStrategy]"
569617
return [string]$uniqueResolvedRefs[0]
570618
}
571619
if ($uniqueResolvedRefs.Count -gt 1) {
572620
Write-Host "Multiple Process-PSModule refs resolved for [$Owner/$Name]: $($uniqueResolvedRefs -join ', ')"
621+
Write-Host "Workflow reference resolution failed using strategy [$workflowDiscoveryStrategy]"
573622
return 'N/A'
574623
}
575624

576625
Write-Host "No Process-PSModule workflow reference found for [$Owner/$Name]"
626+
Write-Host "Workflow reference resolution failed using strategy [$workflowDiscoveryStrategy]"
577627
'N/A'
578628
}
579629

0 commit comments

Comments
 (0)