Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Validate policy files | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| paths: | |
| - 'windows/**/*.json' | |
| - 'macos/**/*.json' | |
| jobs: | |
| validate-json: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Validate JSON files | |
| id: validate | |
| shell: pwsh | |
| run: | | |
| $failedFiles = @() | |
| # Find all JSON files in windows and macos directories | |
| $jsonFiles = @() | |
| if (Test-Path 'windows') { | |
| $jsonFiles += Get-ChildItem -Path 'windows' -Filter '*.json' -Recurse | |
| } | |
| if (Test-Path 'macos') { | |
| $jsonFiles += Get-ChildItem -Path 'macos' -Filter '*.json' -Recurse | |
| } | |
| foreach ($jsonFile in $jsonFiles) { | |
| $data = $null | |
| $loaded = $false | |
| # Try multiple encodings: UTF-8, UTF-8 with BOM, UTF-16 LE | |
| foreach ($encoding in @([System.Text.Encoding]::UTF8, [System.Text.Encoding]::UTF8, [System.Text.Encoding]::Unicode)) { | |
| try { | |
| $content = Get-Content -Path $jsonFile.FullName -Encoding $encoding.WebName -Raw -ErrorAction Stop | |
| $data = $content | ConvertFrom-Json -ErrorAction Stop | |
| $loaded = $true | |
| break | |
| } | |
| catch { | |
| continue | |
| } | |
| } | |
| if (-not $loaded) { | |
| $failedFiles += [PSCustomObject]@{ | |
| File = $jsonFile.FullName.Replace($PWD, '.').Replace('\', '/') | |
| Reason = 'Unable to decode file (not UTF-8, UTF-8-BOM, or UTF-16 LE)' | |
| } | |
| continue | |
| } | |
| # Check if description property exists | |
| if ($null -eq $data.PSObject.Properties['description']) { | |
| $failedFiles += [PSCustomObject]@{ | |
| File = $jsonFile.FullName.Replace($PWD, '.').Replace('\', '/') | |
| Reason = 'description property is missing' | |
| } | |
| } | |
| else { | |
| $description = $data.description | |
| # Check if description is null | |
| if ($null -eq $description) { | |
| $failedFiles += [PSCustomObject]@{ | |
| File = $jsonFile.FullName.Replace($PWD, '.').Replace('\', '/') | |
| Reason = 'description is null' | |
| } | |
| } | |
| # Check if description contains carriage returns or line feeds | |
| elseif ($description -is [string] -and ($description -match "`r" -or $description -match "`n")) { | |
| $failedFiles += [PSCustomObject]@{ | |
| File = $jsonFile.FullName.Replace($PWD, '.').Replace('\', '/') | |
| Reason = 'description contains CR/LF characters' | |
| } | |
| } | |
| } | |
| } | |
| # Output results | |
| if ($failedFiles.Count -gt 0) { | |
| Write-Host "❌ JSON Validation Failed" | |
| Write-Host "" | |
| Write-Host "The following files failed validation:" | |
| Write-Host "" | |
| foreach ($item in $failedFiles) { | |
| Write-Host " • $($item.File)" | |
| Write-Host " Reason: $($item.Reason)" | |
| Write-Host "" | |
| } | |
| # Write to GitHub job summary | |
| if ($env:GITHUB_STEP_SUMMARY) { | |
| "## ❌ JSON Validation Failed" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "**$($failedFiles.Count) file(s) failed validation:**" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "| File | Reason |" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "|------|--------|" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| foreach ($item in $failedFiles) { | |
| "| ``$($item.File)`` | $($item.Reason) |" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| } | |
| } | |
| exit 1 | |
| } | |
| else { | |
| Write-Host "✅ All JSON files passed validation!" | |
| # Write success to GitHub job summary | |
| if ($env:GITHUB_STEP_SUMMARY) { | |
| "## ✅ JSON Validation Passed" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| "All JSON files in ``windows/`` and ``macos/`` directories passed validation." | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append | |
| } | |
| exit 0 | |
| } |