microsoft/hve-core

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-185

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PSScriptAnalyzer.ps1

145lines Ā· 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# Run PSScriptAnalyzer
61$allResults = @()
62$hasErrors = $false
63
64foreach ($file in $filesToAnalyze) {
65 $filePath = if ($file -is [System.IO.FileInfo]) { $file.FullName } else { $file }
66 Write-Host "`nšŸ“„ Analyzing: $filePath" -ForegroundColor Cyan
67
68 $results = Invoke-ScriptAnalyzer -Path $filePath -Settings $ConfigPath
69
70 if ($results) {
71 $allResults += $results
72
73 foreach ($result in $results) {
74 # Create GitHub annotation
75 Write-GitHubAnnotation `
76 -Type $result.Severity.ToString().ToLower() `
77 -Message "$($result.RuleName): $($result.Message)" `
78 -File $filePath `
79 -Line $result.Line `
80 -Column $result.Column
81
82 $icon = switch ($result.Severity) {
83 'Error' { 'āŒ'; $hasErrors = $true }
84 'Warning' { 'āš ļø' }
85 default { 'ā„¹ļø' }
86 }
87
88 Write-Host " $icon [$($result.Severity)] $($result.RuleName): $($result.Message) (Line $($result.Line))" -ForegroundColor $(
89 if ($result.Severity -eq 'Error') { 'Red' }
90 elseif ($result.Severity -eq 'Warning') { 'Yellow' }
91 else { 'Cyan' }
92 )
93 }
94 }
95 else {
96 Write-Host " āœ… No issues found" -ForegroundColor Green
97 }
98}
99
100# Export results
101$summary = @{
102 TotalFiles = $filesToAnalyze.Count
103 TotalIssues = $allResults.Count
104 Errors = ($allResults | Where-Object Severity -eq 'Error').Count
105 Warnings = ($allResults | Where-Object Severity -eq 'Warning').Count
106 Information = ($allResults | Where-Object Severity -eq 'Information').Count
107 HasErrors = $hasErrors
108}
109
110$allResults | ConvertTo-Json -Depth 5 | Out-File $OutputPath
111$summary | ConvertTo-Json | Out-File "logs/psscriptanalyzer-summary.json"
112
113# Set outputs
114Set-GitHubOutput -Name "issues" -Value $summary.TotalIssues
115Set-GitHubOutput -Name "errors" -Value $summary.Errors
116Set-GitHubOutput -Name "warnings" -Value $summary.Warnings
117
118if ($hasErrors) {
119 Set-GitHubEnv -Name "PSSCRIPTANALYZER_FAILED" -Value "true"
120}
121
122# Write summary
123Write-GitHubStepSummary -Content "## PSScriptAnalyzer Results`n"
124
125if ($summary.TotalIssues -eq 0) {
126 Write-GitHubStepSummary -Content "āœ… **Status**: Passed`n`nAll $($summary.TotalFiles) PowerShell files passed linting checks."
127 Write-Host "`nāœ… All PowerShell files passed PSScriptAnalyzer checks!" -ForegroundColor Green
128 exit 0
129}
130else {
131 Write-GitHubStepSummary -Content @"
132āŒ **Status**: Failed
133
134| Metric | Count |
135|--------|-------|
136| Files Analyzed | $($summary.TotalFiles) |
137| Total Issues | $($summary.TotalIssues) |
138| Errors | $($summary.Errors) |
139| Warnings | $($summary.Warnings) |
140| Information | $($summary.Information) |
141"@
142
143 Write-Host "`nāŒ PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" -ForegroundColor Red
144 exit 1
145}
146