Skip to content

Commit 7c856a2

Browse files
Fail closed on workflow parse errors
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent bd7c85f commit 7c856a2

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

.github/scripts/Get-ProcessPSModuleWorkflowInventory.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ function ConvertTo-WorkflowInventoryMarkdown {
770770
if ($TargetReference) {
771771
$matchingTarget = @($parsed | Where-Object MatchesTarget).Count
772772
$lines.Add("- Target reference: $TargetReference")
773-
$lines.Add("- Matching target: $matchingTarget/$($parsed.Count)")
773+
$lines.Add("- Matching target: $matchingTarget/$($Inventory.Count)")
774774
}
775775
$lines.Add('')
776776
$lines.Add('## Reference distribution')
@@ -929,6 +929,8 @@ if (-not $inventory) {
929929
throw "No reusable workflow jobs using [$WorkflowReference] were found in the discovered files."
930930
}
931931

932+
$parseErrors = @($inventory | Where-Object Status -eq 'ParseError')
933+
932934
if ($JsonPath) {
933935
$parent = Split-Path -Path $JsonPath -Parent
934936
if ($parent) {
@@ -950,4 +952,10 @@ if ($MarkdownPath) {
950952
Set-Content -LiteralPath $MarkdownPath -Encoding utf8
951953
}
952954

955+
if ($parseErrors) {
956+
$parseErrorDetails = $parseErrors |
957+
ForEach-Object { "[$($_.Repository)/$($_.WorkflowPath)]: $($_.Error)" }
958+
throw "$($parseErrors.Count) matching workflow file(s) could not be parsed. $($parseErrorDetails -join '; ')"
959+
}
960+
953961
$inventory

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ Describe 'Get-ProcessPSModuleWorkflowInventory' {
143143
Get-Content -LiteralPath $markdownPath -Raw | Should -Match '0 0 \\\* \\\* \\\*'
144144
}
145145

146-
It 'records a parse error for a matching malformed workflow' {
146+
It 'reports and fails closed for a matching malformed workflow' {
147147
$malformedRoot = Join-Path $testRoot 'Malformed'
148148
$malformedWorkflowRoot = Join-Path $malformedRoot '.github/workflows'
149+
$jsonPath = Join-Path $malformedRoot 'output/inventory.json'
150+
$markdownPath = Join-Path $malformedRoot 'output/inventory.md'
149151
& git init --quiet --initial-branch=main $malformedRoot
150152
& git -C $malformedRoot config user.email 'inventory-tests@example.invalid'
151153
& git -C $malformedRoot config user.name 'Inventory Tests'
@@ -160,11 +162,21 @@ jobs:
160162
& git -C $malformedRoot add .
161163
& git -C $malformedRoot commit --quiet -m 'Add malformed workflow'
162164

163-
$result = @(& $scriptPath -Path $malformedRoot)
164-
165+
{
166+
& $scriptPath `
167+
-Path $malformedRoot `
168+
-TargetReference 'v8' `
169+
-JsonPath $jsonPath `
170+
-MarkdownPath $markdownPath |
171+
Out-Null
172+
} | Should -Throw -ExpectedMessage '*1 matching workflow file(s) could not be parsed*'
173+
174+
$result = @(Get-Content -LiteralPath $jsonPath -Raw | ConvertFrom-Json)
165175
$result.Count | Should -Be 1
166176
$result[0].Status | Should -Be 'ParseError'
167177
$result[0].Error | Should -Not -BeNullOrEmpty
178+
Get-Content -LiteralPath $markdownPath -Raw | Should -Match 'Parse errors: 1'
179+
Get-Content -LiteralPath $markdownPath -Raw | Should -Match 'Matching target: 0/1'
168180
}
169181

170182
It 'fails closed when no matching workflow is found' {

0 commit comments

Comments
 (0)