Skip to content

Commit 0c061e3

Browse files
Update psscriptanalyzer-plus-sarif.yml
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent 888b128 commit 0c061e3

1 file changed

Lines changed: 26 additions & 76 deletions

File tree

.github/workflows/psscriptanalyzer-plus-sarif.yml

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ on:
77
- "**/*.ps1"
88
- "**/*.psm1"
99
- "**/*.psd1"
10-
- ".psscriptanalyzer"
11-
- ".psscriptanalyzer.json"
1210
- ".psscriptanalyzer.psd1"
11+
- ".psscriptanalyzer.json" # optional: keep only if you still maintain it for humans/tools
1312
pull_request:
1413
branches: [main, develop]
1514
paths:
1615
- "**/*.ps1"
1716
- "**/*.psm1"
1817
- "**/*.psd1"
19-
- ".psscriptanalyzer"
20-
- ".psscriptanalyzer.json"
2118
- ".psscriptanalyzer.psd1"
19+
- ".psscriptanalyzer.json" # optional
2220
workflow_dispatch:
2321

2422
concurrency:
@@ -84,7 +82,8 @@ jobs:
8482
run: |
8583
$ErrorActionPreference = 'Stop'
8684
$required = [version]'${{ env.PSA_VERSION }}'
87-
$installed = Get-Module -ListAvailable -Name PSScriptAnalyzer | Sort-Object Version -Descending | Select-Object -First 1
85+
$installed = Get-Module -ListAvailable -Name PSScriptAnalyzer |
86+
Sort-Object Version -Descending | Select-Object -First 1
8887
8988
if (-not $installed -or $installed.Version -ne $required) {
9089
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted | Out-Null
@@ -114,6 +113,17 @@ jobs:
114113
cd "$GITHUB_WORKSPACE"
115114
printf '%s' '{"$schema":"http://json.schemastore.org/sarif-2.1.0","version":"2.1.0","runs":[{"tool":{"driver":{"name":"PSScriptAnalyzer","version":"0"}},"results":[]}]}' > "${{ env.SARIF_FILE }}"
116115
116+
- name: ✅ Assert .psscriptanalyzer.psd1 Exists
117+
if: steps.check_ps.outputs.count != '0'
118+
shell: pwsh
119+
run: |
120+
$ErrorActionPreference = 'Stop'
121+
$settings = Join-Path $env:GITHUB_WORKSPACE ".psscriptanalyzer.psd1"
122+
if (-not (Test-Path $settings)) {
123+
throw "Missing required PSScriptAnalyzer settings file: $settings"
124+
}
125+
Write-Host "Using PSScriptAnalyzer settings: $settings"
126+
117127
# ----------------------------
118128
# SAFE AUTO-FIX (FORMAT FIRST)
119129
# ----------------------------
@@ -124,7 +134,6 @@ jobs:
124134
$ErrorActionPreference = 'Stop'
125135
$root = $env:GITHUB_WORKSPACE
126136
127-
# Format only script/module/data files
128137
$files = Get-ChildItem -Path $root -Recurse -File -Include *.ps1,*.psm1,*.psd1 |
129138
Where-Object {
130139
$_.FullName -notmatch '[\\/]\.git[\\/]' -and
@@ -134,7 +143,6 @@ jobs:
134143
foreach ($f in $files) {
135144
try {
136145
$formatted = Invoke-Formatter -Path $f.FullName
137-
# Keep UTF-8 (no BOM). GitHub runners default to utf8 here.
138146
$formatted | Set-Content -Path $f.FullName -Encoding utf8
139147
} catch {
140148
Write-Warning "Invoke-Formatter failed for: $($f.FullName) :: $($_.Exception.Message)"
@@ -147,44 +155,19 @@ jobs:
147155
run: |
148156
$ErrorActionPreference = 'Stop'
149157
150-
# Prefer repo settings file if present
151-
$settingsPath = @(
152-
Join-Path $env:GITHUB_WORKSPACE ".psscriptanalyzer.psd1"
153-
Join-Path $env:GITHUB_WORKSPACE ".psscriptanalyzer.json"
154-
Join-Path $env:GITHUB_WORKSPACE ".psscriptanalyzer"
155-
) | Where-Object { Test-Path $_ } | Select-Object -First 1
158+
$settingsPath = Join-Path $env:GITHUB_WORKSPACE ".psscriptanalyzer.psd1"
159+
if (-not (Test-Path $settingsPath)) {
160+
throw "Missing required settings file: $settingsPath"
161+
}
156162
157-
$include = @("${{ env.FIX_RULES }}".Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries))
163+
$include = @("${{ env.FIX_RULES }}".Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries))
158164
159165
$htFix = @{
160166
Path = '.'
161167
Recurse = $true
162168
Fix = $true
163169
IncludeRule = $include
164-
}
165-
166-
if ($settingsPath) {
167-
Write-Host "Using settings: $settingsPath"
168-
$htFix.Settings = $settingsPath
169-
} else {
170-
# Deterministic defaults if you don't have a settings file
171-
$htFix.Settings = @{
172-
Rules = @{
173-
PSUseConsistentIndentation = @{
174-
Enable = $true
175-
IndentationSize = 4
176-
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
177-
}
178-
PSUseConsistentWhitespace = @{
179-
Enable = $true
180-
CheckInnerBrace = $true
181-
CheckOpenBrace = $true
182-
CheckOpenParen = $true
183-
CheckOperator = $true
184-
CheckSeparator = $true
185-
}
186-
}
187-
}
170+
Settings = $settingsPath
188171
}
189172
190173
Invoke-ScriptAnalyzer @htFix | Out-Null
@@ -276,49 +259,18 @@ jobs:
276259
$sarifPath = Join-Path $root '${{ env.SARIF_FILE }}'
277260
$jsonPath = Join-Path $root '${{ env.JSON_FILE }}'
278261
279-
$settingsPath = @(
280-
Join-Path $root ".psscriptanalyzer.psd1"
281-
Join-Path $root ".psscriptanalyzer.json"
282-
Join-Path $root ".psscriptanalyzer"
283-
) | Where-Object { Test-Path $_ } | Select-Object -First 1
262+
$settingsPath = Join-Path $root ".psscriptanalyzer.psd1"
263+
if (-not (Test-Path $settingsPath)) {
264+
throw "Missing required settings file: $settingsPath"
265+
}
284266
285267
$psaVersion = (Get-Module -ListAvailable PSScriptAnalyzer | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()
286268
287269
$htPSA = @{
288270
Path = $root
289271
Recurse = $true
290272
Severity = @('Error','Warning')
291-
}
292-
293-
if ($settingsPath) {
294-
Write-Host "Using settings: $settingsPath"
295-
$htPSA.Settings = $settingsPath
296-
} else {
297-
# Minimal defaults if no settings file
298-
$htPSA.IncludeRule = @(
299-
'PSAvoidUsingCmdletAliases',
300-
'PSUseShouldProcessForStateChangingFunctions',
301-
'PSAvoidUsingWriteHost',
302-
'PSUseConsistentIndentation',
303-
'PSUseConsistentWhitespace'
304-
)
305-
$htPSA.Settings = @{
306-
Rules = @{
307-
PSUseConsistentIndentation = @{
308-
Enable = $true
309-
IndentationSize = 4
310-
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
311-
}
312-
PSUseConsistentWhitespace = @{
313-
Enable = $true
314-
CheckInnerBrace = $true
315-
CheckOpenBrace = $true
316-
CheckOpenParen = $true
317-
CheckOperator = $true
318-
CheckSeparator = $true
319-
}
320-
}
321-
}
273+
Settings = $settingsPath
322274
}
323275
324276
$results = Invoke-ScriptAnalyzer @htPSA
@@ -415,15 +367,13 @@ jobs:
415367
$lines.Add("**Detected $($items.Count) issue(s)**")
416368
$lines.Add("")
417369
418-
# Counts by rule
419370
$lines.Add("**Counts by rule (top 15):**")
420371
$topRules = $items | Group-Object RuleName | Sort-Object Count -Descending | Select-Object -First 15
421372
foreach ($r in $topRules) {
422373
$lines.Add("- `$($r.Name)`: $($r.Count)")
423374
}
424375
$lines.Add("")
425376
426-
# Show first 20 findings with deep links
427377
$lines.Add("**First 20 findings:**")
428378
foreach ($i in ($items | Select-Object -First 20)) {
429379
$file = ($i.ScriptPath -replace [regex]::Escape($env:GITHUB_WORKSPACE + [IO.Path]::DirectorySeparatorChar), '').Replace('\','/')

0 commit comments

Comments
 (0)