-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathInvoke-PSScriptAnalyzer.ps1
More file actions
207 lines (166 loc) · 7.25 KB
/
Invoke-PSScriptAnalyzer.ps1
File metadata and controls
207 lines (166 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env pwsh
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT
#
# Invoke-PSScriptAnalyzer.ps1
#
# Purpose: Wrapper for PSScriptAnalyzer with GitHub Actions integration
# Author: HVE Core Team
#Requires -Version 7.0
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch]$ChangedFilesOnly,
[Parameter(Mandatory = $false)]
[string]$BaseBranch = "origin/main",
[Parameter(Mandatory = $false)]
[string]$ConfigPath = (Join-Path $PSScriptRoot "PSScriptAnalyzer.psd1"),
[Parameter(Mandatory = $false)]
[string]$OutputPath = "logs/psscriptanalyzer-results.json"
)
$ErrorActionPreference = 'Stop'
# Import shared helpers
Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force
#region Functions
function Invoke-PSScriptAnalyzerCore {
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $false)]
[switch]$ChangedFilesOnly,
[Parameter(Mandatory = $false)]
[string]$BaseBranch = "origin/main",
[Parameter(Mandatory = $false)]
[string]$ConfigPath = (Join-Path $PSScriptRoot "PSScriptAnalyzer.psd1"),
[Parameter(Mandatory = $false)]
[string]$OutputPath = "logs/psscriptanalyzer-results.json"
)
Write-Host "🔍 Running PSScriptAnalyzer..." -ForegroundColor Cyan
# Ensure PSScriptAnalyzer 1.25.0 is available (presence-only check would allow a different installed version to bypass the pin)
if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer | Where-Object { $_.Version -eq [version]'1.25.0' })) {
Write-Host "Installing PSScriptAnalyzer 1.25.0..." -ForegroundColor Yellow
Install-Module -Name PSScriptAnalyzer -RequiredVersion 1.25.0 -Force -Scope CurrentUser -Repository PSGallery
}
Import-Module PSScriptAnalyzer -RequiredVersion 1.25.0
# Get files to analyze
$filesToAnalyze = @()
if ($ChangedFilesOnly) {
Write-Host "Detecting changed PowerShell files..." -ForegroundColor Cyan
$filesToAnalyze = @(Get-ChangedFilesFromGit -BaseBranch $BaseBranch -FileExtensions @('*.ps1', '*.psm1', '*.psd1'))
}
else {
Write-Host "Analyzing all PowerShell files..." -ForegroundColor Cyan
$filesToAnalyze = @(Get-FilesRecursive -Path "." -Include @('*.ps1', '*.psm1', '*.psd1'))
}
if (@($filesToAnalyze).Count -eq 0) {
Write-Host "✅ No PowerShell files to analyze" -ForegroundColor Green
Set-CIOutput -Name "count" -Value "0"
Set-CIOutput -Name "issues" -Value "0"
return
}
Write-Host "Analyzing $($filesToAnalyze.Count) PowerShell files..." -ForegroundColor Cyan
Set-CIOutput -Name "count" -Value $filesToAnalyze.Count
# Run PSScriptAnalyzer
$allResults = @()
$hasErrors = $false
foreach ($file in $filesToAnalyze) {
$filePath = if ($file -is [System.IO.FileInfo]) { $file.FullName } else { $file }
Write-Host "`n📄 Analyzing: $filePath" -ForegroundColor Cyan
$results = Invoke-ScriptAnalyzer -Path $filePath -Settings $ConfigPath
if ($results) {
$allResults += $results
foreach ($result in $results) {
$annotationLevel = switch ($result.Severity) {
'Error' { 'Error' }
'Warning' { 'Warning' }
'Information' { 'Notice' }
default { 'Notice' }
}
Write-CIAnnotation `
-Message "$($result.RuleName): $($result.Message)" `
-Level $annotationLevel `
-File $filePath `
-Line $result.Line `
-Column $result.Column
$icon = switch ($result.Severity) {
'Error' { '❌'; $hasErrors = $true }
'Warning' { '⚠️' }
default { 'ℹ️' }
}
Write-Host " $icon [$($result.Severity)] $($result.RuleName): $($result.Message) (Line $($result.Line))" -ForegroundColor $(
if ($result.Severity -eq 'Error') { 'Red' }
elseif ($result.Severity -eq 'Warning') { 'Yellow' }
else { 'Cyan' }
)
}
}
else {
Write-Host " ✅ No issues found" -ForegroundColor Green
}
}
# Export results
$summary = @{
TotalFiles = @($filesToAnalyze).Count
TotalIssues = @($allResults).Count
Errors = @($allResults | Where-Object Severity -eq 'Error').Count
Warnings = @($allResults | Where-Object Severity -eq 'Warning').Count
Information = @($allResults | Where-Object Severity -eq 'Information').Count
HasErrors = $hasErrors
Timestamp = Get-StandardTimestamp
}
# Ensure logs directory exists
$logsDir = Split-Path $OutputPath -Parent
if (-not (Test-Path $logsDir)) {
New-Item -ItemType Directory -Force -Path $logsDir | Out-Null
}
$allResults | ConvertTo-Json -Depth 5 | Out-File $OutputPath
$summary | ConvertTo-Json | Out-File (Join-Path $logsDir "psscriptanalyzer-summary.json")
# Set outputs
Set-CIOutput -Name "issues" -Value $summary.TotalIssues
Set-CIOutput -Name "errors" -Value $summary.Errors
Set-CIOutput -Name "warnings" -Value $summary.Warnings
if ($hasErrors) {
Set-CIEnv -Name "PSSCRIPTANALYZER_FAILED" -Value "true"
}
# Write summary
Write-CIStepSummary -Content "## PSScriptAnalyzer Results`n"
if ($summary.TotalIssues -eq 0) {
Write-CIStepSummary -Content "✅ **Status**: Passed`n`nAll $($summary.TotalFiles) PowerShell files passed linting checks."
Write-Host "`n✅ All PowerShell files passed PSScriptAnalyzer checks!" -ForegroundColor Green
return
}
else {
Write-CIStepSummary -Content @"
❌ **Status**: Failed
| Metric | Count |
|--------|-------|
| Files Analyzed | $($summary.TotalFiles) |
| Total Issues | $($summary.TotalIssues) |
| Errors | $($summary.Errors) |
| Warnings | $($summary.Warnings) |
| Information | $($summary.Information) |
"@
Write-Host "`n❌ PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" -ForegroundColor Red
throw "PSScriptAnalyzer found $($summary.TotalIssues) issue(s)"
}
}
#endregion Functions
#region Main Execution
if ($MyInvocation.InvocationName -ne '.') {
# Strip /mnt/* paths from PATH to avoid slow 9P cross-filesystem
# lookups in WSL. PSScriptAnalyzer resolves commands by scanning every
# PATH directory per file; Windows mount points add ~40s per file.
$env:PATH = ($env:PATH -split [System.IO.Path]::PathSeparator |
Where-Object { $_ -notlike '/mnt/*' }) -join [System.IO.Path]::PathSeparator
try {
Invoke-PSScriptAnalyzerCore -ChangedFilesOnly:$ChangedFilesOnly -BaseBranch $BaseBranch -ConfigPath $ConfigPath -OutputPath $OutputPath
exit 0
}
catch {
Write-Error -ErrorAction Continue "PSScriptAnalyzer failed: $($_.Exception.Message)"
Write-CIAnnotation -Message $_.Exception.Message -Level Error
exit 1
}
}
#endregion Main Execution