Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running PSScriptAnalyzer as local action. #1388

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cd7a0f8
Initial structure for local PSScriptAnalyzer action
spetersenms Jan 9, 2025
e5b2dd5
Removed unneeded parameters
spetersenms Jan 9, 2025
ddb6d57
Pointing workflow to local PSScriptAnalyzer action
spetersenms Jan 9, 2025
966cc03
Adding verbose flag.
spetersenms Jan 10, 2025
dece92e
Updated readme.
spetersenms Jan 10, 2025
f78399d
Fixed incorrect header label in PSScriptAnalyzer readme
spetersenms Jan 10, 2025
ec704ef
Removed unused parameter from logic.
spetersenms Jan 10, 2025
c86cabc
Corrected PSScriptAnalyzer readme header.
spetersenms Jan 10, 2025
7a2bad9
pre-commit
spetersenms Jan 10, 2025
e6afb89
Adding retry logic
spetersenms Jan 10, 2025
668c3db
Merge branch 'PSScriptAnalyzerAction' of github.com:spetersenms/AL-Go…
spetersenms Jan 10, 2025
e559a9b
Updated excludeRule param description
spetersenms Jan 13, 2025
cc5028b
Writing to GitHub errors instead of using Write-Error.
spetersenms Jan 13, 2025
6180d64
Merge branch 'main' into PSScriptAnalyzerAction
freddydk Jan 16, 2025
9aeb8f4
Merge branch 'main' into PSScriptAnalyzerAction
freddydk Jan 16, 2025
a92b81c
Moving PS code to .ps1 file and renaming to RunPSScriptAnalyzer
spetersenms Jan 16, 2025
5f86817
Merge branch 'PSScriptAnalyzerAction' of github.com:spetersenms/AL-Go…
spetersenms Jan 16, 2025
287833a
Pointing to renamed action
spetersenms Jan 16, 2025
8e07f28
pre-commit
spetersenms Jan 16, 2025
cf930bb
Fixed typo
spetersenms Jan 16, 2025
dbac2df
Switching default shell to pwsh
spetersenms Jan 16, 2025
e3131f7
Changed default value for shell parameter to pwsh
spetersenms Jan 16, 2025
f338c42
pre-commit
spetersenms Jan 16, 2025
bca645d
Parsing ExcludeRule param as list
spetersenms Jan 16, 2025
f57d678
Changing type of excludeRule to string[]
spetersenms Jan 17, 2025
e55aecc
changing recurse parameter to switch type.
spetersenms Jan 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/powershell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Run PSScriptAnalyzer
uses: microsoft/psscriptanalyzer-action@6b2948b1944407914a58661c49941824d149734f # v1.1
uses: ./Actions/PSScriptAnalyzer
with:
path: .\
recurse: true
Expand Down
28 changes: 28 additions & 0 deletions Actions/PSScriptAnalyzer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# PowerShell Script Analyzer

Run the PSScriptAnalyzer tool

## INPUT

### ENV variables

none

### Parameters

| Name | Required | Description | Default value |
| :-- | :-: | :-- | :-- |
| path | Yes | Specifies the path to the scripts or module to be analyzed. Wildcard characters are supported. | powershell |
spetersenms marked this conversation as resolved.
Show resolved Hide resolved
| excludeRule | No | Comma separated list of PSScriptAnalyzer rules to exclude. Wildcard characters are supported. | |
| recurse | No | Runs Script Analyzer on the files in the Path directory and all subdirectories recursively. | |
| output | Yes | Specifies where the path for the sarif file | results.sarif |

## OUTPUT

### ENV variables

none

### OUTPUT variables

none
62 changes: 62 additions & 0 deletions Actions/PSScriptAnalyzer/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Run PSScriptAnalyzer
author: Microsoft Corporation
branding:
icon: "check"
color: "gray-dark"
inputs:
path:
description: 'Specifies the path to the scripts or module to be analyzed. Wildcard characters are supported.'
required: true
default: '.\'
excludeRule:
description: 'Comma separated list of PSScriptAnalyzer rules to exclude. Wildcard characters are supported.'
required: false
recurse:
description: 'Runs Script Analyzer on the files in the Path directory and all subdirectories recursively.'
required: false
output:
description: 'Specifies where the path for the sarif file'
spetersenms marked this conversation as resolved.
Show resolved Hide resolved
required: true
default: 'results.sarif'
runs:
using: "composite"
steps:
- name: Run PSScriptAnalyzer and ConvertToSARIF
shell: pwsh
run: |
$analyzerModule = Get-Module -ListAvailable -Name PSScriptAnalyzer
spetersenms marked this conversation as resolved.
Show resolved Hide resolved
if ($null -eq $analyzerModule) {
Install-Module -Name PSScriptAnalyzer -Force
}

spetersenms marked this conversation as resolved.
Show resolved Hide resolved
$sarifModule = Get-Module -ListAvailable -Name ConvertToSARIF
if ($null -eq $sarifModule) {
Install-Module -Name ConvertToSARIF -Force
}
Import-Module -Name ConvertToSARIF -Force

$htPSA = [ordered]@{ Path = '${{ inputs.path }}'; }
Write-Output "Modules installed, now running tests."
if(![string]::IsNullOrEmpty('${{ inputs.excludeRule }}')) { $htPSA.add('ExcludeRule', @(${{ inputs.excludeRule }})) }
if(![string]::IsNullOrEmpty('${{ inputs.recurse }}')) { $htPSA.add('Recurse', $true) }
$htCTS = [ordered]@{ FilePath = '${{ inputs.output }}'; }

$maxRetries = 3
$retryCount = 0
$success = $false

while (-not $success -and $retryCount -lt $maxRetries) {
Try {
Invoke-ScriptAnalyzer @htPSA -Verbose | ConvertTo-SARIF @htCTS
spetersenms marked this conversation as resolved.
Show resolved Hide resolved
$success = $true
} Catch {
Write-Host "::Error:: $_"
$retryCount++
Write-Output "Retrying... ($retryCount/$maxRetries)"
}
}

if (-not $success) {
Write-Host "::Error:: Failed after $maxRetries attempts."
exit 1
}
Loading