Skip to content

🩹 [Patch]: Obey settings for skips #2

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

Merged
merged 29 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
578fa16
Refactor skip logic in PSScriptAnalyzer tests for improved clarity an…
MariusStorhaug Feb 21, 2025
5c5c7f7
Refactor condition for skipping rules in PSScriptAnalyzer tests for i…
MariusStorhaug Feb 21, 2025
bda02e5
Add custom settings for PSScriptAnalyzer rules and configurations
MariusStorhaug Feb 21, 2025
fed0fcc
Add custom action test configuration and remove unused settings file …
MariusStorhaug Feb 21, 2025
32a63ac
Add custom action test configuration for Src-Default-Custom
MariusStorhaug Feb 21, 2025
573c7a6
Refactor PSScriptAnalyzer tests to simplify skip logic and update cus…
MariusStorhaug Feb 21, 2025
c01d855
Update Custom.Settings.psd1 to disable default rules inclusion
MariusStorhaug Feb 21, 2025
4ad022d
Update documentation and improve rule skipping logic in PSScriptAnaly…
MariusStorhaug Feb 21, 2025
f707bcd
Refactor PSScriptAnalyzer tests to remove commented-out rule details …
MariusStorhaug Feb 21, 2025
5300f7b
Add schema documentation for records used in PSScriptAnalyzer
MariusStorhaug Feb 21, 2025
be00c12
Update README and action.yml for improved clarity and versioning
MariusStorhaug Feb 21, 2025
30efe9f
Change severity level from 'Warning' to 'Information' in Custom.Setti…
MariusStorhaug Feb 21, 2025
4043969
Revert severity level from 'Information' to 'Warning' in Custom.Setti…
MariusStorhaug Feb 21, 2025
38af480
Disable PSUseConsistentIndentation rule and update IncludeRules in Cu…
MariusStorhaug Feb 21, 2025
11b22aa
Add verbose logging for rule evaluation in PSScriptAnalyzer tests
MariusStorhaug Feb 21, 2025
56dabde
Enhance verbose logging in PSScriptAnalyzer tests for better rule eva…
MariusStorhaug Feb 21, 2025
8e4fb2b
Refactor verbose logging in PSScriptAnalyzer tests for consistency
MariusStorhaug Feb 21, 2025
97f7c4f
Fix typo in PSScriptAnalyzer test for rule enabling check
MariusStorhaug Feb 21, 2025
fac919e
Refactor rule skipping messages in PSScriptAnalyzer tests to use Writ…
MariusStorhaug Feb 21, 2025
58fc7f3
Add suppression for Write-Host usage in PSScriptAnalyzer tests for im…
MariusStorhaug Feb 21, 2025
8efe415
Test ANSI link in Github notice
MariusStorhaug Feb 21, 2025
8a5465a
Test ANSI coloring without adding the code.
MariusStorhaug Feb 21, 2025
7f34379
Enhance visibility of rule skipping messages in PSScriptAnalyzer test…
MariusStorhaug Feb 21, 2025
80cc462
Remove explicit setting of PSStyle.OutputRendering to 'Ansi' in PSScr…
MariusStorhaug Feb 21, 2025
a7f544e
Replace explicit PSStyle.OutputRendering setting with Write-Verbose f…
MariusStorhaug Feb 21, 2025
3f00576
Refactor rule skipping messages in PSScriptAnalyzer tests to use ANSI…
MariusStorhaug Feb 21, 2025
496caab
Consolidate rule skipping messages in PSScriptAnalyzer tests for impr…
MariusStorhaug Feb 21, 2025
645ebe4
Cleanup docs
MariusStorhaug Feb 21, 2025
92a4b6c
Remove unused rule and delete deprecated test function for code cleanup
MariusStorhaug Feb 21, 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
24 changes: 24 additions & 0 deletions .github/workflows/Action-Test-Src-Default-Custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Action-Test [Src-Default-Custom]

run-name: "Action-Test [Src-Default-Custom] - [${{ github.event.pull_request.title }} #${{ github.event.pull_request.number }}] by @${{ github.actor }}"

on:
workflow_dispatch:
pull_request:
schedule:
- cron: '0 0 * * *'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions: {}

jobs:
ActionTestCustom:
uses: ./.github/workflows/ActionTestWorkflow.yml
with:
TestType: Src-Default-Custom
Path: tests/srcTestRepo/src
Settings: Custom
SettingsFilePath: tests/srcTestRepo/tests/Custom.Settings.psd1
1 change: 0 additions & 1 deletion .github/workflows/Action-Test-outputs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ jobs:
TestType: outputs
Path: tests/outputTestRepo/outputs/modules/PSModuleTest
Settings: Module
SettingsFilePath:
104 changes: 48 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
# Invoke-ScriptAnalyzer (by PSModule)
# Invoke-ScriptAnalyzer

This repository contains a GitHub Action that runs PSScriptAnalyzer on your code.
This repository contains a GitHub Action that runs [`PSScriptAnalyzer`](https://github.com/PowerShell/PSScriptAnalyzer) on your code.
The action analyzes PowerShell scripts using a hashtable-based settings file to
customize rule selection, severity filtering, and custom rule inclusion.

> **Note:** This repository includes automated tests that run via Pester to ensure
> your settings file is working as expected.
## Dependencies

## Action Details

- **Name:** Invoke-ScriptAnalyzer (by PSModule)
- **Description:** Runs PSScriptAnalyzer on the code.
- **Author:** PSModule
- **Branding:**
Icon: `check-square`
Color: `gray-dark`
- This action.
- [`PSScriptAnalyzer` module](https://github.com/PowerShell/PSScriptAnalyzer).
- [`Invoke-Pester` action](https://github.com/PSModule/Invoke-Pester)
- [`Pester` module](https://github.com/Pester/Pester)
- [`GitHub-Script` action](https://github.com/PSModule/GitHub-Script)
- [`GitHub` module](https://github.com/PSModule/GitHub)

## Inputs

| Input | Description | Required | Default |
|---------------------|-------------------------------------------------------------------|----------|-----------------------------------------------------------------------------|
| **Path** | The path to the code to test. | Yes | `${{ github.workspace }}` |
| **Settings** | The type of tests to run: `Module`, `SourceCode`, or `Custom`. | No | `Custom` |
| **SettingsFilePath**| If `Custom` is selected, the path to the settings file. | No | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` |
| Input | Description | Required | Default |
|---------------------|----------------------------------------------------------------|----------|-----------------------------------------------------------------------------|
| **Path** | The path to the code to test. | Yes | `${{ github.workspace }}` |
| **Settings** | The type of tests to run: `Module`, `SourceCode`, or `Custom`. | No | `Custom` |
| **SettingsFilePath**| If `Custom` is selected, the path to the settings file. | No | `${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1` |

## Outputs

| Output | Description | Value |
|---------|---------------------------------------|--------------------------------------------|
| passed | Indicates if the tests passed. | `${{ steps.test.outputs.Passed }}` |
| Output | Description | Value |
|----------|--------------------------------|------------------------------------|
| `passed` | Indicates if the tests passed. | `${{ steps.test.outputs.Passed }}` |

## Files Overview
## How It Works

- **action.yml**
Describes the action inputs, outputs, and run steps. The action uses a
composite run steps approach with two main steps:
1. **Get test paths:** Uses a script to resolve paths and settings.
2. **Invoke-Pester:** Runs Pester tests against PSScriptAnalyzer.
1. **Set a Path**
Choose a path for your code to test into the `Path` input. This can be a
directory or a file.

- **scripts/main.ps1**
Determines the correct settings file path based on the test type. It
supports testing a module, source code, or using a custom settings file.
2. **Choose settings**
Choose the type of tests to run by setting the `Settings` input. The options
are `Module`, `SourceCode`, or `Custom`. The default is `Custom`.

- **scripts/tests/PSScriptAnalyzer/**
Contains Pester tests that run PSScriptAnalyzer using the provided settings
file. The tests check for issues reported by PSScriptAnalyzer based on rule
configuration.
The predefined settings:
- [`Module`](./scripts/tests/PSScriptAnalyzer/Module.Settings.psd1): Analyzes a module following PSModule standards.
- [`SourceCode`](./scripts/tests/PSScriptAnalyzer/SourceCode.Settings.psd1): Analyzes the source code following PSModule standards.

## How It Works
You can also create a custom settings file to customize the analysis. The
settings file is a hashtable that defines the rules to include, exclude, or
customize. The settings file is in the format of a `.psd1` file.

1. **Path Resolution:**
The action reads inputs and determines the code path, test path, and the
settings file path. For custom settings, it uses the file at:
```powershell
.github/linters/.powershell-psscriptanalyzer.psd1
```
Otherwise, it uses a default settings file from the test folder.
For more info on how to create a settings file, see the [Settings Documentation](./Settings.md) file.

2. **Pester Testing:**
The tests import the settings file and use `Invoke-ScriptAnalyzer` to scan
3. **Run the Action**
The tests import the settings file and use `Invoke-ScriptAnalyzer` to analyze
the code. Each rule is evaluated, and if a rule violation is found, the test
will fail for that rule. Rules that are marked to be skipped (via exclusions
in the settings file) are automatically skipped in the test.

3. **Automation:**
Designed for CI/CD, this action integrates with GitHub Actions, Azure Pipelines,
and other systems. The settings file customizes analysis, letting you control
rule inclusion, severity filtering, and custom rule paths.
To be clear; the action follows the settings file to determine which rules to skip.

4. **View the Results**
The action outputs the results of the tests. If the tests pass, the action
will return a `passed` output with a value of `true`. If the tests fail, the
action will return a `passed` output with a value of `false`.

The action also outputs the results of the tests to the console.

## Example Workflow

Expand All @@ -81,23 +75,21 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v2

- name: Invoke PSScriptAnalyzer
uses: PSModule/Invoke-ScriptAnalyzer@v1
with:
Path: ${{ github.workspace }}
Settings: Custom
SettingsFilePath: ${{ github.workspace }}/.github/linters/.powershell-psscriptanalyzer.psd1
Settings: SourceCode
```

## Appendix: Settings File Documentation

For detailed documentation on the format of the settings file, see the
[Settings File Documentation](./SettingsFileDocumentation.md) file.

## References and Links

- [PSScriptAnalyzer Documentation](https://learn.microsoft.com/powershell/module/psscriptanalyzer/)
- [GitHub Super-Linter](https://github.com/github/super-linter)
- [PSScriptAnalyzer Module Overview](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules)
- [PSScriptAnalyzer Rules](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules)
- [PSScriptAnalyzer GitHub Repository](https://github.com/PowerShell/PSScriptAnalyzer)
- [Custom Rules in PSScriptAnalyzer](https://docs.microsoft.com/powershell/scripting/developer/hosting/psscriptanalyzer-extensibility)
- [GitHub Super-Linter](https://github.com/github/super-linter)
83 changes: 83 additions & 0 deletions Settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# PSScriptAnalyzer Settings File Format Documentation

This document describes the format and usage of the hashtable-based settings file
for PSScriptAnalyzer. The file is used by the GitHub action to customize analysis.

## Basic Setup

The file is a PowerShell data file (.psd1) that returns a hashtable. For example:
```powershell
@{
Severity = @('Error','Warning')
ExcludeRules = @('PSAvoidUsingWriteHost')
}
```
This example sets the severity filter and excludes a specific rule.

## Key Configuration Options

- **IncludeRules**
A list of rules to run. Wildcards (e.g. `PSAvoid*`) are supported.

- **ExcludeRules**
A list of rules to skip. Excludes take precedence over include lists.

- **Severity**
Filters output by severity. Allowed values include `Error`, `Warning`, and `Information`.

- **IncludeDefaultRules**
A Boolean switch to include default rules when using custom rules.

- **CustomRulePath**
One or more paths to custom rule modules or scripts. These extend PSScriptAnalyzer.

- **RecurseCustomRulePath**
Boolean to search subdirectories of the custom rule path(s) for more rule files.

- **Rules**
A nested hashtable for rule-specific settings. Use it to pass parameters to rules.

```powershell
Rules = @{
PSAvoidUsingCmdletAliases = @{
Enabled = $true
Whitelist = @('ls','gc')
}
}
```

## Configuring Custom Rules

Custom rules are implemented in modules (.psm1) or scripts. They must export
functions that return DiagnosticRecord objects. Specify their location using
`CustomRulePath`. Use `IncludeDefaultRules = $true` if you want to run both
default and custom rules.

For example:
```powershell
@{
CustomRulePath = @('.\Modules\MyCustomRules.psm1')
IncludeDefaultRules = $true
IncludeRules = @('PSUseApprovedVerbs', 'Measure-*')
}
```

## Rule Execution and Skip Logic

The action evaluates each rule using several configurable settings to determine whether it should be executed or skipped.
The evaluation is performed in the following order:

1. **Exclude Rules**
- If the rule's name is present in the **`ExcludeRules`** list, the rule is skipped immediately, regardless of other settings.

2. **Include Rules**
- If an **`IncludeRules`** list is provided, the rule must be part of this list. If the rule's name is *not* in the list, it is skipped.

3. **Severity Filtering**
- If a **`Severity`** list is specified, the rule's severity must be included in that list. If the rule's severity is not part of the allowed
values, the rule is skipped.

4. **Rule-Specific Configuration**
- If a specific configuration exists for the rule under the **Rules** key, and its `Enable` property is set to false, the rule is skipped.

To see what rules are skipped and why, check the logs for the action. There is a log group inside the test that contains the rules that were skipped.
116 changes: 0 additions & 116 deletions SettingsFileDocumentation.md

This file was deleted.

2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ runs:
Script: ${{ github.action_path }}/scripts/main.ps1

- name: Invoke-Pester
uses: PSModule/Invoke-Pester@fix
uses: PSModule/Invoke-Pester@v2
id: test
env:
Settings: ${{ fromJson(steps.paths.outputs.result).Settings }}
Expand Down
Loading