Skip to content

Commit dcf6c6a

Browse files
committed
Add PSScriptAnalyzer action
1 parent 34c5641 commit dcf6c6a

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# github-action-psscriptanalzyer
2-
GitHub Action to run PSScriptAnalyzer static code analysis
1+
# github-action-psscriptanalyzer
2+
3+
[GitHub Action](https://github.com/features/actions) to run [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) static code analysis checks on [Pull Requests](https://help.github.com/articles/about-pull-requests/).
4+
5+
## Success Criteria
6+
7+
This action will succeed if **zero** PSScriptAnalyzer errors are found.
8+
If any warnings or informational issues are found, a comment will be posted to the pull request but the action will not return a failure.
9+
10+
## Usage
11+
12+
Place the following in your `./github/main.workflow` file to run PSScriptAnalyzer on incoming pull requests.
13+
14+
```
15+
16+
workflow "psscriptanalysis" {
17+
on = "pull_request"
18+
resolves = "PSScriptAnalyzer"
19+
}
20+
21+
action "PSScriptAnalyzer" {
22+
# Replace <latest tag> with the latest tag from
23+
# https://github.com/devblackops/github-action-psscriptanalyzer/releases
24+
uses = "devblackops/github-action-psscriptanalyzer/analyze@<latest tag>"
25+
26+
# Optional environment variables to control analysis behavior
27+
env = [
28+
PSSCRIPTANALYZER_ROOT = "./MyModule"
29+
PSSCRIPTANALYZER_SETTINGS_PATH = "./settings.psd1
30+
]
31+
}
32+
```
33+
34+
## Environment Variables
35+
36+
| Name | Default | Description |
37+
|--------------------------------|---------|-------------|
38+
| PSSCRIPTANALYZER_ROOT | . | The root directory to run PSScriptAnalyzer on. By default, this is the root of the repository.
39+
| PSSCRIPTANALYZER_SETTINGS_PATH | none | The path to a PSScriptAnalyser settings file to control rules to execute.

analyze/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM mcr.microsoft.com/powershell:6.1.0-ubuntu-18.04 as base
2+
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
3+
RUN Set-PSRepository -Name PSGallery -InstallationPolicy Trusted; \
4+
Install-Module PSScriptAnalyzer -RequiredVersion 1.17.1 -Repository PSGallery
5+
6+
FROM base as analyzer
7+
LABEL "com.github.actions.name" = "PSScriptAnalyzer"
8+
LABEL "com.github.actions.description" = "Run PSScriptAnalyzer tests"
9+
LABEL "com.github.actions.icon" = "box"
10+
LABEL "com.github.actions.color" = "blue"
11+
12+
LABEL "repository" = "https://github.com/devblackops/github-action-psscriptanalyzer"
13+
LABEL "homepage" = "https://github.com/PowerShell/PSScriptAnalyzer"
14+
LABEL "maintainer" = "Brandon Olin <[email protected]>"
15+
16+
ADD entrypoint.sh /entrypoint.sh
17+
ADD run.ps1 /run.ps1
18+
19+
RUN chmod +x /entrypoint.sh
20+
21+
ENTRYPOINT ["/entrypoint.sh"]

analyze/entrypoint.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
pwsh -f /run.ps1 $*
6+
7+
exit $?

analyze/run.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#requires -modules PSScriptAnalyzer
2+
3+
[cmdletbinding()]
4+
param()
5+
6+
$ErrorActionPreference = 'Stop'
7+
8+
$nl = [Environment]::NewLine
9+
10+
$analyzeParams = @{
11+
Recurse = $true
12+
}
13+
14+
# By default, run PSScriptAnalyzer on the whole repository
15+
# but allow overriding this with PSSCRIPTANALYZER_ROOT environment variable
16+
if ($env:PSSCRIPTANALYZER_ROOT) {
17+
$analyzeParams.Path = $env:PSSCRIPTANALYZER_ROOT
18+
} else {
19+
$analyzeParams.Path = $env:GITHUB_WORKSPACE
20+
}
21+
22+
# Path to custom script analzyer settings
23+
if ($env:PSSCRIPTANALYZER_SETTINGS_PATH) {
24+
$analyzeParams.Settings = $env:PSSCRIPTANALYZER_SETTINGS_PATH
25+
}
26+
27+
# Run PSScriptAnalyzer
28+
$issues = Invoke-ScriptAnalyzer @analyzeParams
29+
$errors = ($issues.where({$_.Severity -eq 'Error'})).Count
30+
$warnings = ($issues.where({$_.Severity -eq 'Warning'})).Count
31+
$infos = ($issues.where({$_.Severity -eq 'Information'})).Count
32+
33+
$strings = @{
34+
summary = 'PSScriptAnalyzer results:{0}Errors: {1, 6}{2}Warnings: {3, 4}{4}Information: {5}'
35+
errorList = '{0}The following PSScriptAnalyzer errors caused the check to fail:{1}'
36+
warningMsg = '{0} There were **[{1}]** warnings and **[{2}]** informational issues found. These did not cause the check to fail but it is recommended that they be fixed.'
37+
}
38+
39+
# Create analysis summary
40+
$summary = ($strings.summary -f $nl, $errors, $nl, $warnings, $nl, $infos)
41+
$comment = '```' + $nl + $summary + $nl + '```'
42+
if ($errors -gt 0) {
43+
$comment += $scripts.errorList -f $nl, $nl
44+
$errorMsg = ($issues.Where({$_.Severity -eq 'Error'}) |
45+
Format-List -Property RuleName, Severity, ScriptName, Line, Message |
46+
Out-String -Width 80).Trim()
47+
$comment += '```' + $nl + $errorMsg + $nl + '```'
48+
}
49+
if (($warnings -gt 0) -or ($infos -gt 0)) {
50+
$comment += $strings.warningMsg -f $nl, $warnings, $infos
51+
}
52+
Write-Output $comment
53+
54+
# Send comment back to PR if any errors were found
55+
$ghEvent = Get-Content -Path $env:GITHUB_EVENT_PATH | ConvertFrom-Json
56+
if ($errors.Count -gt 0) {
57+
$params = @{
58+
Uri = $ghEvent.pull_request.'_links'.comments.href
59+
Method = 'Post'
60+
Headers = @{
61+
Authorization = "token $env:GITHUB_TOKEN"
62+
}
63+
ContentType = 'application/json'
64+
Body = @{body = $comment} | ConvertTo-Json
65+
}
66+
Invoke-RestMethod @params > $null
67+
}
68+
69+
exit $errors

0 commit comments

Comments
 (0)