@@ -116,3 +116,76 @@ jobs:
116116 git pull --rebase origin "$BRANCH"
117117 git push origin HEAD:"$BRANCH"
118118 fi
119+
120+ pr-powershell-analysis :
121+ if : github.event_name == 'pull_request'
122+ runs-on : ubuntu-latest
123+ permissions :
124+ contents : read
125+ pull-requests : write
126+ steps :
127+ - name : Checkout code
128+ uses : actions/checkout@v4
129+
130+ - name : Install PSScriptAnalyzer
131+ run : pwsh -Command "Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser"
132+
133+ - name : List changed .ps1 files in PR
134+ id : files
135+ run : |
136+ git fetch origin ${{ github.base_ref }}
137+ CHANGED=$(git diff --name-only origin/${{ github.base_ref }} ${{ github.sha }} | grep -E '\.ps1$' || true)
138+ echo "changed_ps1<<EOF" >> $GITHUB_OUTPUT
139+ echo "$CHANGED" >> $GITHUB_OUTPUT
140+ echo "EOF" >> $GITHUB_OUTPUT
141+
142+ - name : Run PSScriptAnalyzer on changed files
143+ id : psa
144+ run : |
145+ FILES="${{ steps.files.outputs.changed_ps1 }}"
146+ if [ -z "$FILES" ]; then
147+ echo "No changed PowerShell files to analyze."
148+ echo "psa_md<<EOF" >> $GITHUB_OUTPUT
149+ echo "## PSScriptAnalyzer Results" >> $GITHUB_OUTPUT
150+ echo "" >> $GITHUB_OUTPUT
151+ echo "No PowerShell files changed in this PR." >> $GITHUB_OUTPUT
152+ echo "EOF" >> $GITHUB_OUTPUT
153+ exit 0
154+ fi
155+ echo "$FILES" | xargs pwsh -Command "Invoke-ScriptAnalyzer -Path \$args -Recurse | ConvertTo-Json -Depth 5" > psa_result.json
156+
157+ cat <<'EOF' > format_psa.ps1
158+ $results = Get-Content psa_result.json | ConvertFrom-Json
159+ $md = @()
160+ $md += '## PSScriptAnalyzer Results'
161+ $md += ''
162+ if ($results.Count -eq 0) {
163+ $md += 'No issues detected.'
164+ } else {
165+ $md += '| File | Line | Rule | Severity | Message |'
166+ $md += '| ---- | ---- | ---- | -------- | ------- |'
167+ foreach ($item in $results) {
168+ $file = if ($item.ScriptName) { $item.ScriptName } else { '' }
169+ $line = if ($item.Line) { $item.Line } else { '' }
170+ $rule = if ($item.RuleName) { $item.RuleName } else { '' }
171+ $severity = switch ($item.Severity) { 0 {'Info'}; 1 {'Warning'}; 2 {'Error'}; default { $item.Severity } }
172+ $msg = ($item.Message -replace '\|','\\|') -replace '\r?\n',' '
173+ if ($msg.Length -gt 120) { $msg = $msg.Substring(0,117) + '...' }
174+ $md += "| $file | $line | $rule | $severity | $msg |"
175+ }
176+ }
177+ Set-Content -Path psa_result.md -Value ($md -join "`n")
178+ EOF
179+
180+ pwsh -File format_psa.ps1
181+
182+ echo "psa_md<<EOF" >> $GITHUB_OUTPUT
183+ cat psa_result.md >> $GITHUB_OUTPUT
184+ echo "EOF" >> $GITHUB_OUTPUT
185+
186+ - name : Comment PR with results
187+ uses : peter-evans/create-or-update-comment@v4
188+ with :
189+ token : ${{ secrets.GITHUB_TOKEN }}
190+ issue-number : ${{ github.event.pull_request.number }}
191+ body : ${{ steps.psa.outputs.psa_md }}
0 commit comments