Skip to content

Commit 26372a1

Browse files
Track workflow target reference compliance
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 3cb70c1 commit 26372a1

2 files changed

Lines changed: 54 additions & 10 deletions

File tree

.github/scripts/Get-ProcessPSModuleWorkflowInventory.ps1

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
.EXAMPLE
1616
./.github/scripts/Get-ProcessPSModuleWorkflowInventory.ps1 `
1717
-Organization PSModule `
18+
-TargetReference v8 `
1819
-JsonPath ./output/process-workflows.json `
1920
-MarkdownPath ./output/process-workflows.md
2021
@@ -45,6 +46,10 @@ param(
4546
[ValidateNotNullOrEmpty()]
4647
[string] $WorkflowReference = 'PSModule/Process-PSModule/.github/workflows/workflow.yml',
4748

49+
[Parameter()]
50+
[ValidateNotNullOrEmpty()]
51+
[string] $TargetReference,
52+
4853
[Parameter()]
4954
[string] $JsonPath,
5055

@@ -458,7 +463,10 @@ function Get-WorkflowInventoryItem {
458463
[psobject] $WorkflowFile,
459464

460465
[Parameter(Mandatory)]
461-
[string] $ExpectedReference
466+
[string] $ExpectedReference,
467+
468+
[Parameter()]
469+
[string] $ExpectedTargetReference
462470
)
463471

464472
if ($WorkflowFile.Content -notmatch [regex]::Escape($ExpectedReference)) {
@@ -495,6 +503,11 @@ function Get-WorkflowInventoryItem {
495503
Name = $jobName
496504
Uses = "$uses"
497505
Reference = "$uses".Substring("$ExpectedReference@".Length)
506+
MatchesTarget = if ($ExpectedTargetReference) {
507+
"$uses".Substring("$ExpectedReference@".Length) -eq $ExpectedTargetReference
508+
} else {
509+
$null
510+
}
498511
Inputs = ConvertTo-StringMap -Map (Get-MapValue -Map $job -Name 'with')
499512
SecretMode = $secretMode
500513
SecretMappings = if ($secretMode -eq 'explicit') {
@@ -595,6 +608,12 @@ function Get-WorkflowInventoryItem {
595608
ProcessJobs = @($processJobs)
596609
AdditionalJobs = @($allJobNames | Where-Object { $_ -notin $processJobNames })
597610
VersionComments = $versionComments
611+
TargetReference = $ExpectedTargetReference
612+
MatchesTarget = if ($ExpectedTargetReference) {
613+
@($processJobs | Where-Object { -not $_.MatchesTarget }).Count -eq 0
614+
} else {
615+
$null
616+
}
598617
}
599618
}
600619

@@ -621,7 +640,10 @@ function ConvertTo-WorkflowInventoryMarkdown {
621640

622641
[Parameter(Mandatory)]
623642
[ValidateSet('GitHub', 'Local')]
624-
[string] $Source
643+
[string] $Source,
644+
645+
[Parameter()]
646+
[string] $TargetReference
625647
)
626648

627649
$parsed = @($Inventory | Where-Object Status -eq 'Parsed')
@@ -660,6 +682,11 @@ function ConvertTo-WorkflowInventoryMarkdown {
660682
$lines.Add("- Workflow files: $($Inventory.Count)")
661683
$lines.Add("- Parsed: $($parsed.Count)")
662684
$lines.Add("- Parse errors: $($parseErrors.Count)")
685+
if ($TargetReference) {
686+
$matchingTarget = @($parsed | Where-Object MatchesTarget).Count
687+
$lines.Add("- Target reference: $TargetReference")
688+
$lines.Add("- Matching target: $matchingTarget/$($parsed.Count)")
689+
}
663690
$lines.Add('')
664691
$lines.Add('## Reference distribution')
665692
$lines.Add('')
@@ -688,11 +715,11 @@ function ConvertTo-WorkflowInventoryMarkdown {
688715
$lines.Add('## Workflow files')
689716
$lines.Add('')
690717
$lines.Add(
691-
'| Repository | File | Name | Run name | Events | Reference | Version | PR types | Push branches | Schedule |' +
692-
' Concurrency | Cancel | Permissions | Condition | Secrets | Inputs | Extra jobs |'
718+
'| Repository | File | Name | Run name | Events | Reference | Target | Version | PR types | Push branches |' +
719+
' Schedule | Concurrency | Cancel | Permissions | Condition | Secrets | Inputs | Extra jobs |'
693720
)
694721
$lines.Add(
695-
'| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |'
722+
'| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |'
696723
)
697724

698725
foreach ($item in $Inventory | Sort-Object Repository, WorkflowPath) {
@@ -710,7 +737,7 @@ function ConvertTo-WorkflowInventoryMarkdown {
710737
if ($item.Status -eq 'ParseError') {
711738
$lines.Add(
712739
"| $(ConvertTo-MarkdownCell $repositoryCell) " +
713-
"| $(ConvertTo-MarkdownCell $workflowCell) | parse error | | | | | | | | | | | | | | |"
740+
"| $(ConvertTo-MarkdownCell $workflowCell) | parse error | | | | | | | | | | | | | | | |"
714741
)
715742
continue
716743
}
@@ -749,6 +776,7 @@ function ConvertTo-WorkflowInventoryMarkdown {
749776
"| $(ConvertTo-MarkdownCell $item.RunName) " +
750777
"| $(ConvertTo-MarkdownCell $item.Events) " +
751778
"| $(ConvertTo-MarkdownCell $referencesForItem) " +
779+
"| $(ConvertTo-MarkdownCell $item.MatchesTarget) " +
752780
"| $(ConvertTo-MarkdownCell $versionsForItem) " +
753781
"| $(ConvertTo-MarkdownCell $item.PullRequestTypes) " +
754782
"| $(ConvertTo-MarkdownCell $item.PushBranches) " +
@@ -805,7 +833,10 @@ if (-not $workflowFiles) {
805833
$inventory = @(
806834
$workflowFiles |
807835
ForEach-Object {
808-
Get-WorkflowInventoryItem -WorkflowFile $_ -ExpectedReference $WorkflowReference
836+
Get-WorkflowInventoryItem `
837+
-WorkflowFile $_ `
838+
-ExpectedReference $WorkflowReference `
839+
-ExpectedTargetReference $TargetReference
809840
}
810841
)
811842

@@ -829,7 +860,8 @@ if ($MarkdownPath) {
829860
}
830861
ConvertTo-WorkflowInventoryMarkdown `
831862
-Inventory $inventory `
832-
-Source $PSCmdlet.ParameterSetName |
863+
-Source $PSCmdlet.ParameterSetName `
864+
-TargetReference $TargetReference |
833865
Set-Content -LiteralPath $MarkdownPath -Encoding utf8
834866
}
835867

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ AfterAll {
8282

8383
Describe 'Get-ProcessPSModuleWorkflowInventory' {
8484
It 'inventories matching local workflows and their compatibility dimensions' {
85-
$result = @(& $scriptPath -Path $testRoot)
85+
$result = @(
86+
& $scriptPath `
87+
-Path $testRoot `
88+
-TargetReference '0123456789012345678901234567890123456789'
89+
)
8690

8791
$result.Count | Should -Be 1
8892
$result[0].Repository | Should -Be 'Example'
@@ -93,6 +97,8 @@ Describe 'Get-ProcessPSModuleWorkflowInventory' {
9397
$result[0].ConcurrencyGroup | Should -Be '${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}'
9498
$result[0].CancelInProgress | Should -BeFalse
9599
$result[0].ProcessJobs[0].Reference | Should -Be '0123456789012345678901234567890123456789'
100+
$result[0].ProcessJobs[0].MatchesTarget | Should -BeTrue
101+
$result[0].MatchesTarget | Should -BeTrue
96102
$result[0].ProcessJobs[0].Condition | Should -Match 'head.repo.full_name'
97103
$result[0].ProcessJobs[0].Inputs.Keys | Should -Contain 'Debug'
98104
$result[0].ProcessJobs[0].SecretMappings.Keys | Should -Be @(
@@ -113,13 +119,19 @@ Describe 'Get-ProcessPSModuleWorkflowInventory' {
113119
$jsonPath = Join-Path $testRoot 'inventory.json'
114120
$markdownPath = Join-Path $testRoot 'inventory.md'
115121

116-
& $scriptPath -Path $repositoryRoot -JsonPath $jsonPath -MarkdownPath $markdownPath | Out-Null
122+
& $scriptPath `
123+
-Path $repositoryRoot `
124+
-TargetReference '0123456789012345678901234567890123456789' `
125+
-JsonPath $jsonPath `
126+
-MarkdownPath $markdownPath |
127+
Out-Null
117128

118129
Test-Path -LiteralPath $jsonPath | Should -BeTrue
119130
Test-Path -LiteralPath $markdownPath | Should -BeTrue
120131
(Get-Content -LiteralPath $jsonPath -Raw).TrimStart() | Should -Match '^\['
121132
Get-Content -LiteralPath $markdownPath -Raw | Should -Match 'Example'
122133
Get-Content -LiteralPath $markdownPath -Raw | Should -Match '0123456789012345678901234567890123456789'
134+
Get-Content -LiteralPath $markdownPath -Raw | Should -Match 'Matching target: 1/1'
123135
}
124136

125137
It 'records a parse error for a matching malformed workflow' {

0 commit comments

Comments
 (0)