Skip to content

Commit a9a2138

Browse files
committed
Refactor for new Github actions
1 parent f1dd3b3 commit a9a2138

File tree

6 files changed

+118
-76
lines changed

6 files changed

+118
-76
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [2.0.0] - Unreleased
9+
10+
### Changed
11+
12+
- Refactored to new GitHub Action syntax
13+
- Renamed options to:
14+
- rootPath
15+
- settingsPath
16+
- repoToken
17+
- sendComment
18+
- failOnErrors
19+
- failOnWarnings
20+
- failOnInfos
21+
- Allow failing the GitHub Action on PSSA error, warning, or informational issues
22+
823
## [1.2.1] - 2019-01-15
924

1025
### Fixed

README.md

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,76 @@
11
# github-action-psscriptanalyzer
22

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/).
3+
[GitHub Action](https://github.com/features/actions) to run [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) static code analysis checks on PowerShell for [Pull Requests](https://help.github.com/articles/about-pull-requests/).
44

55
## Success Criteria
66

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.
7+
By default, this action will succeed if **zero** PSScriptAnalyzer **errors** and **warnings** are found.
8+
Failing on errors, warnings, or informational issues can be configured. See [Usage](#Usage) below.
9+
The sending of comments back to the PR if the action fails can be disabled if desired.
910

1011
## Usage
1112

12-
Place the following in your `./github/main.workflow` file to run PSScriptAnalyzer on incoming pull requests.
13-
This action can be triggered from other GitHub events but analyzer results will only be posted as comments to pull requests.
13+
### Basic
1414

15-
> Make sure `secrets = ["GITHUB_TOKEN"]` is present as it is required to post a comment back to the pull request.
15+
Basic configuration that will run PSSA and fail on errors or warnings
1616

17-
```hcl
18-
workflow "psscriptanalysis" {
19-
on = "pull_request"
20-
resolves = "PSScriptAnalyzer"
21-
}
17+
, and send a comment back to the PR with a summary.
18+
Note, that `repoToken` is required for sending comments back.
2219

23-
action "PSScriptAnalyzer" {
24-
# Replace <latest tag> with the latest tag from
25-
# https://github.com/devblackops/github-action-psscriptanalyzer/releases
26-
uses = "devblackops/github-action-psscriptanalyzer@<latest tag>"
20+
```yaml
21+
name: CI
22+
on: [pull_request]
23+
jobs:
24+
lint:
25+
name: Run PSSA
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v1
29+
- name: lint
30+
uses: devblackops/github-action-psscriptanalyzer@master
31+
with:
32+
repoToken: ${{ secrets.GITHUB_TOKEN }}
33+
```
34+
35+
### Advanced
36+
37+
Advanced configuration that will run PSSA only in the `MyModule` directory, with customer PSSA settings, and fail on errors, warnings, or informational issues.
38+
A comment back to the PR with the PSSA summary will also be sent if any issues were detected.
2739

28-
secrets = ["GITHUB_TOKEN"]
2940

30-
# Optional environment variables to control analysis behavior
31-
env = {
32-
PSSCRIPTANALYZER_ROOT = "./MyModule"
33-
PSSCRIPTANALYZER_SETTINGS_PATH = "./settings.psd1
34-
}
35-
}
41+
```yaml
42+
name: CI
43+
on: [pull_request]
44+
jobs:
45+
lint:
46+
name: Run PSSA
47+
runs-on: ubuntu-latest
48+
steps:
49+
- uses: actions/checkout@v1
50+
- name: lint
51+
uses: devblackops/github-action-psscriptanalyzer@master
52+
with:
53+
rootPath: MyModule
54+
settingsPath: pssa_settings.psd1
55+
sendComment: true
56+
repoToken: ${{ secrets.GITHUB_TOKEN }}
57+
failOnErrors: true
58+
failOnWarnings: true
59+
failOnInfos: true
3660
```
3761

38-
## Environment Variables
62+
## Inputs
3963

4064
| Name | Default | Description |
41-
|--------------------------------|------|-------------|
42-
| PSSCRIPTANALYZER_ROOT | . | The root directory to run PSScriptAnalyzer on. By default, this is the root of the repository.
43-
| PSSCRIPTANALYZER_SETTINGS_PATH | none | The path to a PSScriptAnalyser settings file to control rules to execute.
44-
| PSSCRIPTANALYZER_SEND_COMMENT | true | Enable/disable sending comments with PSScriptAnalyzer results back to PR.
65+
|------|---------|-------------|
66+
| rootPath | \<none> | The root directory to run PSScriptAnalyzer on. By default, this is the root of the repository.
67+
| settingsPath | \<none> | The path to a PSScriptAnalyser settings file to control rules to execute.
68+
| sendComment | true | Enable/disable sending comments with PSScriptAnalyzer results back to PR.
69+
| repoToken | \<none> | GitHub token the action will use to send comments back to PR with. Use `${{ secrets.GITHUB_TOKEN }}`.
70+
| failOnErrors | true | Enable/disable failing the action on PSScriptAnalyzer error items.
71+
| failOnWarnings | true | Enable/disable failing the action on PSScriptAnalyzer warning items.
72+
| failOnInfos | false | Enable/disable failing the action on PSScriptAnalyzer informational items.
4573

4674
## Example
4775

48-
![](media/example.jpg)
76+
![](media/example.png)

action.yml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ description: GitHub Action to run PSScriptAnalyzer static code analysis checks o
44
inputs:
55
rootPath:
66
description: The root directory to run PSScriptAnalyzer on. By default, this is the root of the repository.
7-
default: .
87
required: false
98
settingsPath:
109
description: The path to a PSScriptAnalyser settings file to control rules to execute.
1110
required: false
12-
default: .
11+
repoToken:
12+
description: GitHub token the action will use to send comments back to PR with.
13+
required: false
1314
sendComment:
14-
description: Enable/disable sending comments back to PR when PSScriptAnalyzer finds one or more failure checks.
15+
description: Send comment back to PR with PSScriptAnalyzer summary if any issues where found.
1516
required: false
1617
default: true
17-
repoToken:
18-
description: GitHub token the action will use to send comments back to PR with.
18+
failOnErrors:
19+
description: Enable/disable failing the action on PSScriptAnalyzer error items.
20+
required: false
21+
default: true
22+
failOnWarnings:
23+
description: Enable/disable failing the action on PSScriptAnalyzer warning items.
24+
required: false
25+
default: true
26+
failOnInfos:
27+
description: Enable/disable failing the action on PSScriptAnalyzer informational items.
1928
required: false
20-
# sendCommentOnWarnings:
21-
# description: Enable/disable sending comments back to PR when PSScriptAnalyzer finds one or more warning checks.
22-
# required: false
23-
# default: true
24-
# sendCommentOnInfos:
25-
# description: Enable/disable sending comments back to PR when PSScriptAnalyzer finds one or more information checks.
26-
# required: false
27-
# default: false
29+
default: false
2830
outputs:
2931
runs:
3032
using: docker

entrypoint.ps1

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,57 @@ $analyzeParams = @{
1212
# By default, run PSScriptAnalyzer on the whole repository
1313
# but allow overriding this with INPUT_ROOTPATH environment variable
1414
if ($env:INPUT_ROOTPATH) {
15-
$analyzeParams.Path = $env:INPUT_ROOTPATH
15+
$analyzeParams.Path = Join-Path '/github/workspace' $env:INPUT_ROOTPATH
1616
} else {
1717
$analyzeParams.Path = $env:GITHUB_WORKSPACE
1818
}
1919

2020
# Path to custom script analzyer settings
2121
if ($env:INPUT_SETTINGSPATH) {
22-
$analyzeParams.Settings = $env:INPUT_SETTINGSPATH
22+
$analyzeParams.Settings = Join-Path '/github/workspace' $env:INPUT_SETTINGSPATH
2323
}
2424

2525
# Run PSScriptAnalyzer
2626
$issues = Invoke-ScriptAnalyzer @analyzeParams
27-
$errors = ($issues.where({$_.Severity -eq 'Error'})).Count
28-
$warnings = ($issues.where({$_.Severity -eq 'Warning'})).Count
29-
$infos = ($issues.where({$_.Severity -eq 'Information'})).Count
27+
$errors = $issues.Where({$_.Severity -eq 'Error'})
28+
$warnings = $issues.Where({$_.Severity -eq 'Warning'})
29+
$infos = $issues.Where({$_.Severity -eq 'Information'})
3030

31-
$strings = @{
32-
summary = 'PSScriptAnalyzer results:{0}Errors: {1, 6}{2}Warnings: {3, 4}{4}Information: {5}'
33-
errorList = '{0}The following PSScriptAnalyzer errors caused the check to fail:{1}'
34-
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.'
31+
# Create comment string
32+
$comment = '**PSScriptAnalyzer results:**'
33+
$comment += '{0}<details><summary>Errors: [{1}], Warnings: [{2}], Information: [{3}]</summary><p>{4}{5}```' -f $nl, $errors.Count, $warnings.Count, $infos.Count, $nl, $nl
34+
if ($errors.Count -gt 0) {
35+
$comment += $nl + ($errors | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
3536
}
36-
37-
# Create analysis summary
38-
$summary = ($strings.summary -f $nl, $errors, $nl, $warnings, $nl, $infos)
39-
$comment = '```' + $nl + $summary + $nl + '```'
40-
if ($errors -gt 0) {
41-
$comment += $strings.errorList -f $nl, $nl
42-
$errorMsg = ($issues.Where({$_.Severity -eq 'Error'}) |
43-
Format-List -Property RuleName, Severity, ScriptName, Line, Message |
44-
Out-String -Width 80).Trim()
45-
$comment += '```' + $nl + $errorMsg + $nl + '```'
37+
if ($warnings.Count -gt 0) {
38+
$comment += $nl+ $nl + ($warnings | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
4639
}
47-
if (($warnings -gt 0) -or ($infos -gt 0)) {
48-
$comment += $strings.warningMsg -f $nl, $warnings, $infos
40+
if ($infos.Count -gt 0) {
41+
$comment += $nl + $nl + ($infos | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
4942
}
43+
$comment += '{0}{1}```{2}</p></details>' -f $nl, $nl, $nl
5044
Write-Output $comment
5145

52-
$ghEvent = Get-Content -Path $env:GITHUB_EVENT_PATH | ConvertFrom-Json
46+
# Get comment URL
47+
$ghEvent = Get-Content -Path $env:GITHUB_EVENT_PATH | ConvertFrom-Json -Depth 30
5348
$commentsUrl = $ghEvent.pull_request.comments_url
5449

55-
# Send comment back to PR if any errors were found
56-
if ($env:INPUT_SENDCOMMENT -ne 'false' -and $env:INPUT_SENDCOMMENT -ne 0 -and $commentsUrl) {
57-
if ($errors -gt 0) {
58-
$params = @{
59-
Uri = $commentsUrl
60-
Method = 'Post'
61-
Headers = @{
62-
Authorization = "token $env:INPUT_REPOTOKEN"
63-
}
64-
ContentType = 'application/json'
65-
Body = @{body = $comment} | ConvertTo-Json
50+
# Send comment back to PR if any issues were found
51+
if ($commentsUrl -and $env:INPUT_SENDCOMMENT -and ($errors.Count -gt 0 -or $warnings.Count -gt 0 -or $infos.Count -gt 0)) {
52+
$params = @{
53+
Uri = $commentsUrl
54+
Method = 'Post'
55+
Headers = @{
56+
Authorization = "token $env:INPUT_REPOTOKEN"
6657
}
67-
Invoke-RestMethod @params > $null
58+
ContentType = 'application/json'
59+
Body = @{body = $comment} | ConvertTo-Json
6860
}
61+
Invoke-RestMethod @params > $null
6962
}
7063

71-
exit $errors
64+
$exitCode = 0
65+
if ($env:INPUT_FAILONERRORS -eq 'true' -or $env:INPUT_FAILONERRORS -eq 1) { $exitCode += $errors.Count}
66+
if ($env:INPUT_FAILONWARNING -eq 'true' -or $env:INPUT_FAILONWARNING -eq 1) { $exitCode += $warnings.Count}
67+
if ($env:INPUT_FAILONINFOS -eq 'true' -or $env:INPUT_FAILONINFOS -eq 1) { $exitCode += $infos.Count}
68+
exit $exitCode

media/example.jpg

-117 KB
Binary file not shown.

media/example.png

42.9 KB
Loading

0 commit comments

Comments
 (0)