PSRule icon indicating copy to clipboard operation
PSRule copied to clipboard

[DOCS] Add an example for an expansion convention for changed files

Open BernieWhite opened this issue 10 months ago • 0 comments

URL

https://microsoft.github.io/PSRule/v3/creating-your-pipeline

How could the documentation be improved?

Add an example showing how to expand/ explode the change set when the file types are complex like ARM and Bicep.

Example 1:

Export-PSRuleConvention 'AddModuleFiles' -Initialize {
    Write-Host "Initializing AddModuleFiles convention"

    # Get the change file set for any bicep files.
    foreach ($inputFile in $PSRule.Repository.GetChangedFiles().WithExtension('.bicep')) {
        # Calculate the module path, modules are expected to be under modules/<moduleName>/v<version>
        # Tests are under modules/<moduleName>/v<version>/.tests/main.tests.bicep
        $modulePath = $inputFile.AsFileInfo().Directory;
        while (!$modulePath.Name.StartsWith('v')) {
            $modulePath = $modulePath.Parent;
        }
        $moduleVersion = $modulePath.Name;
        $moduleName = $modulePath.Parent.Name;

        # Add tests
        Write-Host "Adding module tests for $moduleName/$moduleVersion";

        if (!(Test-Path "$($modulePath.FullName)/.tests/main.tests.bicep")) {
            Write-Warning "No tests found for $moduleName/$moduleVersion";
        }
        else {
            $PSRule.Input.Add($modulePath.FullName + "/.tests/main.tests.bicep");
        }

        # Add matching docs
        $PSRule.Input.Add("docs/modules/$moduleName-$moduleVersion/**");
    }
}

Example 2:


Export-PSRuleConvention 'AddParams' -Initialize {
    Write-Verbose "Initializing Params convention"

    # If a deployment, test through parameter files.
    foreach ($inputFile in $PSRule.Repository.GetChangedFiles().WithinPath("deployments/").WithExtension('.bicep')) {

        $paramFiles = $inputFile.DirectoryName + "/*.bicepparam"
        Write-Verbose "Adding input file: $paramFiles"

        $PSRule.Input.Add($paramFiles);
    }
}

Additional context

No response

BernieWhite avatar Mar 08 '25 09:03 BernieWhite