microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
hve-core-v2.0.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PSScriptAnalyzer.ps1

156lines Ā· modecode

1#!/usr/bin/env pwsh
2#
3# Invoke-PSScriptAnalyzer.ps1
4#
5# Purpose: Wrapper for PSScriptAnalyzer with GitHub Actions integration
6# Author: HVE Core Team
7# Created: 2025-11-05
8
9[CmdletBinding()]
10param(
11 [Parameter(Mandatory = $false)]
12 [switch]$ChangedFilesOnly,
13
14 [Parameter(Mandatory = $false)]
15 [string]$BaseBranch = "origin/main",
16
17 [Parameter(Mandatory = $false)]
18 [string]$ConfigPath = (Join-Path $PSScriptRoot "PSScriptAnalyzer.psd1"),
19
20 [Parameter(Mandatory = $false)]
21 [string]$OutputPath = "logs/psscriptanalyzer-results.json"
22)
23
24# Import shared helpers
25Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force
26
27Write-Host "šŸ” Running PSScriptAnalyzer..." -ForegroundColor Cyan
28
29# Ensure PSScriptAnalyzer is available
30if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) {
31 Write-Host "Installing PSScriptAnalyzer module..." -ForegroundColor Yellow
32 Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser -Repository PSGallery
33}
34
35Import-Module PSScriptAnalyzer
36
37# Get files to analyze
38$filesToAnalyze = @()
39
40if ($ChangedFilesOnly) {
41 Write-Host "Detecting changed PowerShell files..." -ForegroundColor Cyan
42 $filesToAnalyze = Get-ChangedFilesFromGit -BaseBranch $BaseBranch -FileExtensions @('*.ps1', '*.psm1', '*.psd1')
43}
44else {
45 Write-Host "Analyzing all PowerShell files..." -ForegroundColor Cyan
46 $gitignorePath = Join-Path (git rev-parse --show-toplevel 2>$null) ".gitignore"
47 $filesToAnalyze = Get-FilesRecursive -Path "." -Include @('*.ps1', '*.psm1', '*.psd1') -GitIgnorePath $gitignorePath
48}
49
50if ($filesToAnalyze.Count -eq 0) {
51 Write-Host "āœ… No PowerShell files to analyze" -ForegroundColor Green
52 Set-GitHubOutput -Name "count" -Value "0"
53 Set-GitHubOutput -Name "issues" -Value "0"
54 exit 0
55}
56
57Write-Host "Analyzing $($filesToAnalyze.Count) PowerShell files..." -ForegroundColor Cyan
58Set-GitHubOutput -Name "count" -Value $filesToAnalyze.Count
59
60#region Main Execution
61try {
62 # Run PSScriptAnalyzer
63 $allResults = @()
64 $hasErrors = $false
65
66 foreach ($file in $filesToAnalyze) {
67 $filePath = if ($file -is [System.IO.FileInfo]) { $file.FullName } else { $file }
68 Write-Host "`nšŸ“„ Analyzing: $filePath" -ForegroundColor Cyan
69
70 $results = Invoke-ScriptAnalyzer -Path $filePath -Settings $ConfigPath
71
72 if ($results) {
73 $allResults += $results
74
75 foreach ($result in $results) {
76 # Create GitHub annotation
77 Write-GitHubAnnotation `
78 -Type $result.Severity.ToString().ToLower() `
79 -Message "$($result.RuleName): $($result.Message)" `
80 -File $filePath `
81 -Line $result.Line `
82 -Column $result.Column
83
84 $icon = switch ($result.Severity) {
85 'Error' { 'āŒ'; $hasErrors = $true }
86 'Warning' { 'āš ļø' }
87 default { 'ā„¹ļø' }
88 }
89
90 Write-Host " $icon [$($result.Severity)] $($result.RuleName): $($result.Message) (Line $($result.Line))" -ForegroundColor $(
91 if ($result.Severity -eq 'Error') { 'Red' }
92 elseif ($result.Severity -eq 'Warning') { 'Yellow' }
93 else { 'Cyan' }
94 )
95 }
96 }
97 else {
98 Write-Host " āœ… No issues found" -ForegroundColor Green
99 }
100 }
101
102 # Export results
103 $summary = @{
104 TotalFiles = $filesToAnalyze.Count
105 TotalIssues = $allResults.Count
106 Errors = ($allResults | Where-Object Severity -eq 'Error').Count
107 Warnings = ($allResults | Where-Object Severity -eq 'Warning').Count
108 Information = ($allResults | Where-Object Severity -eq 'Information').Count
109 HasErrors = $hasErrors
110 }
111
112 $allResults | ConvertTo-Json -Depth 5 | Out-File $OutputPath
113 $summary | ConvertTo-Json | Out-File "logs/psscriptanalyzer-summary.json"
114
115 # Set outputs
116 Set-GitHubOutput -Name "issues" -Value $summary.TotalIssues
117 Set-GitHubOutput -Name "errors" -Value $summary.Errors
118 Set-GitHubOutput -Name "warnings" -Value $summary.Warnings
119
120 if ($hasErrors) {
121 Set-GitHubEnv -Name "PSSCRIPTANALYZER_FAILED" -Value "true"
122 }
123
124 # Write summary
125 Write-GitHubStepSummary -Content "## PSScriptAnalyzer Results`n"
126
127 if ($summary.TotalIssues -eq 0) {
128 Write-GitHubStepSummary -Content "āœ… **Status**: Passed`n`nAll $($summary.TotalFiles) PowerShell files passed linting checks."
129 Write-Host "`nāœ… All PowerShell files passed PSScriptAnalyzer checks!" -ForegroundColor Green
130 exit 0
131 }
132 else {
133 Write-GitHubStepSummary -Content @"
134āŒ **Status**: Failed
135
136| Metric | Count |
137|--------|-------|
138| Files Analyzed | $($summary.TotalFiles) |
139| Total Issues | $($summary.TotalIssues) |
140| Errors | $($summary.Errors) |
141| Warnings | $($summary.Warnings) |
142| Information | $($summary.Information) |
143"@
144
145 Write-Host "`nāŒ PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" -ForegroundColor Red
146 exit 1
147 }
148}
149catch {
150 Write-Error "PSScriptAnalyzer failed: $($_.Exception.Message)"
151 if ($env:GITHUB_ACTIONS -eq 'true') {
152 Write-Output "::error::$($_.Exception.Message)"
153 }
154 exit 1
155}
156#endregion