Skip to content

Commit d1d05d1

Browse files
Handle inventory API and shorthand edge cases
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 25b8c7e commit d1d05d1

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

.github/scripts/Get-ProcessPSModuleWorkflowInventory.ps1

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,20 @@ function Get-GitHubMatchingWorkflowFile {
151151
'per_page=100'
152152
)
153153
$pages = @($response | ConvertFrom-Json -Depth 100)
154+
if ($pages.incomplete_results -contains $true) {
155+
throw "GitHub code search returned incomplete results for [$query]."
156+
}
157+
154158
$searchResults = @($pages | ForEach-Object { $_.items })
155159
if (-not $searchResults) {
156160
throw "GitHub code search returned no matches for [$query]."
157161
}
158162

163+
$expectedResultCount = @($pages | Select-Object -ExpandProperty total_count -Unique)
164+
if ($expectedResultCount.Count -ne 1 -or $searchResults.Count -ne $expectedResultCount[0]) {
165+
throw "GitHub code search returned [$($searchResults.Count)] of [$($expectedResultCount -join ', ')] results for [$query]."
166+
}
167+
159168
$repositoryByName = @{}
160169
foreach ($item in $RepositoryInfo) {
161170
$repositoryByName[$item.nameWithOwner] = $item
@@ -419,6 +428,29 @@ function ConvertTo-StringArray {
419428
@($Value | ForEach-Object { "$_" })
420429
}
421430

431+
function ConvertTo-PermissionValue {
432+
[CmdletBinding()]
433+
param(
434+
[Parameter()]
435+
[AllowNull()]
436+
[object] $Value
437+
)
438+
439+
if ($null -eq $Value) {
440+
return [ordered]@{}
441+
}
442+
443+
if ($Value -is [string]) {
444+
return $Value
445+
}
446+
447+
if ($Value -is [Collections.IDictionary]) {
448+
return ConvertTo-StringMap -Map $Value
449+
}
450+
451+
throw "Unsupported workflow permissions type [$($Value.GetType().FullName)]."
452+
}
453+
422454
function Get-WorkflowInventoryItem {
423455
[CmdletBinding()]
424456
param(
@@ -494,10 +526,34 @@ function Get-WorkflowInventoryItem {
494526
Error = $_.Exception.Message
495527
}
496528
}
497-
$pullRequest = Get-MapValue -Map $trigger -Name 'pull_request'
498-
$push = Get-MapValue -Map $trigger -Name 'push'
499-
$schedule = Get-MapValue -Map $trigger -Name 'schedule'
500-
$concurrency = Get-MapValue -Map $workflow -Name 'concurrency'
529+
try {
530+
$pullRequest = Get-MapValue -Map $trigger -Name 'pull_request'
531+
$push = Get-MapValue -Map $trigger -Name 'push'
532+
$schedule = Get-MapValue -Map $trigger -Name 'schedule'
533+
$concurrency = Get-MapValue -Map $workflow -Name 'concurrency'
534+
if ($concurrency -is [string]) {
535+
$concurrencyGroup = $concurrency
536+
$cancelInProgress = $null
537+
} elseif ($null -eq $concurrency -or $concurrency -is [Collections.IDictionary]) {
538+
$concurrencyGroup = Get-MapValue -Map $concurrency -Name 'group'
539+
$cancelInProgress = Get-MapValue -Map $concurrency -Name 'cancel-in-progress'
540+
} else {
541+
throw "Unsupported workflow concurrency type [$($concurrency.GetType().FullName)]."
542+
}
543+
$permissions = ConvertTo-PermissionValue -Value (Get-MapValue -Map $workflow -Name 'permissions')
544+
} catch {
545+
return [pscustomobject]@{
546+
Repository = $WorkflowFile.Repository
547+
DefaultBranch = $WorkflowFile.DefaultBranch
548+
Archived = $WorkflowFile.Archived
549+
RepositoryUrl = $WorkflowFile.RepositoryUrl
550+
WorkflowPath = $WorkflowFile.WorkflowPath
551+
WorkflowUrl = $WorkflowFile.WorkflowUrl
552+
SearchQuery = $WorkflowFile.SearchQuery
553+
Status = 'ParseError'
554+
Error = $_.Exception.Message
555+
}
556+
}
501557
$allJobNames = Get-MapKey -Map $jobs
502558
$processJobNames = @($processJobs.Name)
503559

@@ -533,9 +589,9 @@ function Get-WorkflowInventoryItem {
533589
PushPathsIgnore = ConvertTo-StringArray -Value (Get-MapValue -Map $push -Name 'paths-ignore')
534590
PullRequestBranches = ConvertTo-StringArray -Value (Get-MapValue -Map $pullRequest -Name 'branches')
535591
PullRequestTypes = ConvertTo-StringArray -Value (Get-MapValue -Map $pullRequest -Name 'types')
536-
ConcurrencyGroup = Get-MapValue -Map $concurrency -Name 'group'
537-
CancelInProgress = Get-MapValue -Map $concurrency -Name 'cancel-in-progress'
538-
Permissions = ConvertTo-StringMap -Map (Get-MapValue -Map $workflow -Name 'permissions')
592+
ConcurrencyGroup = $concurrencyGroup
593+
CancelInProgress = $cancelInProgress
594+
Permissions = $permissions
539595
ProcessJobs = @($processJobs)
540596
AdditionalJobs = @($allJobNames | Where-Object { $_ -notin $processJobNames })
541597
VersionComments = $versionComments
@@ -676,11 +732,15 @@ function ConvertTo-WorkflowInventoryMarkdown {
676732
Sort-Object -Unique
677733
)
678734
$conditionSummary = @($item.ProcessJobs.Condition | Where-Object { $_ } | Sort-Object -Unique)
679-
$permissionSummary = @(
680-
$item.Permissions.GetEnumerator() |
681-
Sort-Object Key |
682-
ForEach-Object { "$($_.Key)=$($_.Value)" }
683-
)
735+
$permissionSummary = if ($item.Permissions -is [string]) {
736+
@($item.Permissions)
737+
} else {
738+
@(
739+
$item.Permissions.GetEnumerator() |
740+
Sort-Object Key |
741+
ForEach-Object { "$($_.Key)=$($_.Value)" }
742+
)
743+
}
684744

685745
$lines.Add(
686746
"| $(ConvertTo-MarkdownCell $repositoryCell) " +
@@ -758,7 +818,8 @@ if ($JsonPath) {
758818
if ($parent) {
759819
New-Item -ItemType Directory -Path $parent -Force | Out-Null
760820
}
761-
$inventory | ConvertTo-Json -Depth 100 | Set-Content -LiteralPath $JsonPath -Encoding utf8
821+
ConvertTo-Json -InputObject @($inventory) -Depth 100 |
822+
Set-Content -LiteralPath $JsonPath -Encoding utf8
762823
}
763824

764825
if ($MarkdownPath) {

.github/scripts/tests/Get-ProcessPSModuleWorkflowInventory.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Describe 'Get-ProcessPSModuleWorkflowInventory' {
117117

118118
Test-Path -LiteralPath $jsonPath | Should -BeTrue
119119
Test-Path -LiteralPath $markdownPath | Should -BeTrue
120+
(Get-Content -LiteralPath $jsonPath -Raw).TrimStart() | Should -Match '^\['
120121
Get-Content -LiteralPath $markdownPath -Raw | Should -Match 'Example'
121122
Get-Content -LiteralPath $markdownPath -Raw | Should -Match '0123456789012345678901234567890123456789'
122123
}
@@ -178,6 +179,8 @@ jobs:
178179
Set-Content -LiteralPath (Join-Path $shorthandWorkflowRoot 'Process.yml') -Value @'
179180
name: Shorthand
180181
on: [push, workflow_dispatch]
182+
permissions: read-all
183+
concurrency: process-${{ github.ref }}
181184
jobs:
182185
Process:
183186
uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v8
@@ -188,5 +191,8 @@ jobs:
188191
$result = @(& $scriptPath -Path $shorthandRoot)
189192

190193
$result[0].Events | Should -Be @('push', 'workflow_dispatch')
194+
$result[0].Permissions | Should -Be 'read-all'
195+
$result[0].ConcurrencyGroup | Should -Be 'process-${{ github.ref }}'
196+
$result[0].CancelInProgress | Should -BeNullOrEmpty
191197
}
192198
}

0 commit comments

Comments
 (0)