-
Notifications
You must be signed in to change notification settings - Fork 13
/
powershell.ps1
321 lines (287 loc) · 9.99 KB
/
powershell.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# PSRule
#
# See details at: https://github.com/microsoft/ps-rule
[CmdletBinding()]
param (
# The root path for analysis
[Parameter(Mandatory = $False)]
[String]$Path = $Env:INPUT_PATH,
# The type of input
[Parameter(Mandatory = $False)]
[ValidateSet('repository', 'inputPath')]
[String]$InputType = $Env:INPUT_INPUTTYPE,
# The path to input files
[Parameter(Mandatory = $False)]
[String]$InputPath = $Env:INPUT_INPUTPATH,
# The path to find rules
[Parameter(Mandatory = $False)]
[String]$Source = $Env:INPUT_SOURCE,
# Rule modules to use
[Parameter(Mandatory = $False)]
[String]$Modules = $Env:INPUT_MODULES,
# A baseline to use
[Parameter(Mandatory = $False)]
[String]$Baseline = $env:INPUT_BASELINE,
# The conventions to use
[Parameter(Mandatory = $False)]
[String]$Conventions = $Env:INPUT_CONVENTIONS,
# The path to an options file.
[Parameter(Mandatory = $False)]
[String]$Option = $Env:INPUT_OPTION,
# Filters output to include results with the specified outcome.
[Parameter(Mandatory = $False)]
[String]$Outcome = $Env:INPUT_OUTCOME,
# The output format
[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'NUnit3', 'Csv', 'Markdown', 'Sarif')]
[String]$OutputFormat = $Env:INPUT_OUTPUTFORMAT,
# The path to store formatted output
[Parameter(Mandatory = $False)]
[String]$OutputPath = $Env:INPUT_OUTPUTPATH,
# Determine if a pre-release module version is installed.
[Parameter(Mandatory = $False)]
[String]$PreRelease = $Env:INPUT_PRERELEASE,
# The name of the PowerShell repository where PSRule modules are installed from.
[String]$Repository = $Env:INPUT_REPOSITORY,
# Determines if a job summary is written.
[Parameter(Mandatory = $False)]
[String]$Summary = $Env:INPUT_SUMMARY,
# The specific version of PSRule to use.
[Parameter(Mandatory = $False)]
[String]$Version = $Env:INPUT_VERSION
)
$workspacePath = $Env:GITHUB_WORKSPACE;
$ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue;
}
# Check inputType
if ([String]::IsNullOrEmpty($InputType) -or $InputType -notin 'repository', 'inputPath') {
$InputType = 'repository';
}
# Set workspace
if ([String]::IsNullOrEmpty($workspacePath)) {
$workspacePath = $PWD;
}
# Set Path
if ([String]::IsNullOrEmpty($Path)) {
$Path = $workspacePath;
}
else {
$Path = Join-Path -Path $workspacePath -ChildPath $Path;
}
# Set InputPath
if ([String]::IsNullOrEmpty($InputPath)) {
$InputPath = $Path;
}
else {
$InputPath = Join-Path -Path $Path -ChildPath $InputPath;
}
# Set Source
if ([String]::IsNullOrEmpty($Source)) {
$Source = Join-Path -Path $Path -ChildPath '.ps-rule/';
}
else {
$Source = Join-Path -Path $Path -ChildPath $Source;
}
# Set conventions
if (![String]::IsNullOrEmpty($Conventions)) {
$Conventions = @($Conventions.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
$_.Trim();
});
}
else {
$Conventions = @();
}
# Set repository
if ([String]::IsNullOrEmpty($Repository)) {
$Repository = 'PSGallery'
}
function WriteDebug {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $True)]
[String]$Message
)
process {
if ($Env:SYSTEM_DEBUG -eq 'true' -or $Env:ACTIONS_STEP_DEBUG -eq 'true') {
Write-Host "::debug::$Message";
}
}
}
#
# Check and install modules
#
$dependencyFile = Join-Path -Path $Env:GITHUB_ACTION_PATH -ChildPath 'modules.json';
$latestVersion = (Get-Content -Path $dependencyFile -Raw | ConvertFrom-Json -AsHashtable -Depth 5).dependencies.PSRule.version;
$checkParams = @{
RequiredVersion = $latestVersion
}
if (![String]::IsNullOrEmpty($Version)) {
$checkParams['RequiredVersion'] = $Version;
if ($PreRelease -eq 'true') {
$checkParams['AllowPrerelease'] = $True;
}
}
# Look for existing versions of PSRule
Write-Host "[info] Using repository: $Repository";
$installed = @(Get-InstalledModule -Name PSRule @checkParams -ErrorAction Ignore)
if ($installed.Length -eq 0) {
Write-Host "[info] Installing PSRule: $($checkParams.RequiredVersion)";
$Null = Install-Module -Repository $Repository -Name PSRule @checkParams -Scope CurrentUser -Force;
}
foreach ($m in $installed) {
Write-Host "[info] Using existing module $($m.Name): $($m.Version)";
}
# Look for existing modules
Write-Host '';
$moduleNames = @()
if (![String]::IsNullOrEmpty($Modules)) {
$moduleNames = $Modules.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries);
}
$moduleParams = @{
Scope = 'CurrentUser'
Force = $True
}
if ($PreRelease -eq 'true') {
$moduleParams['AllowPrerelease'] = $True;
}
# Install each module if not already installed
foreach ($m in $moduleNames) {
$m = $m.Trim();
Write-Host "> Checking module: $m";
try {
if ($Null -eq (Get-InstalledModule -Name $m -ErrorAction Ignore)) {
Write-Host ' - Installing module';
$Null = Install-Module -Repository $Repository -Name $m @moduleParams -AllowClobber -ErrorAction Stop;
}
else {
Write-Host ' - Already installed';
}
# Check
if ($Null -eq (Get-InstalledModule -Name $m)) {
Write-Host " - Failed to install";
}
else {
Write-Host " - Using version: $((Get-InstalledModule -Name $m).Version)";
}
}
catch {
Write-Host "::error::An error occurred installing a dependency module '$m'. $($_.Exception.Message)";
$Host.SetShouldExit(1);
}
}
try {
$checkParams = @{ MinimumVersion = $checkParams.RequiredVersion.Split('-')[0] }
$Null = Import-Module PSRule @checkParams -ErrorAction Stop;
$version = (Get-Module PSRule).Version;
}
catch {
Write-Host "::error::An error occurred importing module 'PSRule'. $($_.Exception.Message)";
$Host.SetShouldExit(1);
}
#
# Run assert pipeline
#
Write-Host '';
Write-Host "[info] Using Version: $version";
Write-Host "[info] Using Action: $Env:GITHUB_ACTION";
Write-Host "[info] Using Workspace: $workspacePath";
Write-Host "[info] Using PWD: $PWD";
Write-Host "[info] Using Path: $Path";
Write-Host "[info] Using Source: $Source";
Write-Host "[info] Using Baseline: $Baseline";
Write-Host "[info] Using Conventions: $Conventions";
Write-Host "[info] Using InputType: $InputType";
Write-Host "[info] Using InputPath: $InputPath";
Write-Host "[info] Using Option: $Option";
Write-Host "[info] Using Outcome: $Outcome";
Write-Host "[info] Using OutputFormat: $OutputFormat";
Write-Host "[info] Using OutputPath: $OutputPath";
Write-Host "[info] Using Summary: $Summary";
try {
Push-Location -Path $Path;
$invokeParams = @{
Path = $Source
Style = 'GitHubActions'
ErrorAction = 'Stop'
}
WriteDebug "Preparing command-line:";
WriteDebug ([String]::Concat('-Path ''', $Source, ''''));
if (![String]::IsNullOrEmpty($Baseline)) {
$invokeParams['Baseline'] = $Baseline;
WriteDebug ([String]::Concat('-Baseline ''', $Baseline, ''''));
}
if ($Conventions.Length -gt 0) {
$invokeParams['Convention'] = $Conventions;
WriteDebug ([String]::Concat('-Convention ', [String]::Join(', ', $Conventions)));
}
if (![String]::IsNullOrEmpty($Modules)) {
$moduleNames = $Modules.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries);
$invokeParams['Module'] = $moduleNames;
WriteDebug ([String]::Concat('-Module ', [String]::Join(', ', $moduleNames)));
}
if (![String]::IsNullOrEmpty($Option)) {
$invokeParams['Option'] = $Option;
WriteDebug ([String]::Concat('-Option ', $Option));
}
if (![String]::IsNullOrEmpty($Outcome)) {
$invokeParams['Outcome'] = $Outcome;
WriteDebug ([String]::Concat('-Outcome ', $Outcome));
}
if (![String]::IsNullOrEmpty($OutputFormat) -and ![String]::IsNullOrEmpty($OutputPath) -and $OutputFormat -ne 'None') {
$invokeParams['OutputFormat'] = $OutputFormat;
$invokeParams['OutputPath'] = $OutputPath;
WriteDebug ([String]::Concat('-OutputFormat ', $OutputFormat, ' -OutputPath ''', $OutputPath, ''''));
}
if ($Summary -eq 'true') {
$Env:PSRULE_OUTPUT_JOBSUMMARYPATH = 'reports/ps_rule_summary.md';
$invokeParams['As'] = 'Detail';
}
# repository
if ($InputType -eq 'repository') {
WriteDebug 'Running ''Assert-PSRule'' with repository as input.';
Write-Host '';
Write-Host '---';
Assert-PSRule @invokeParams -InputPath $InputPath -Format File;
}
# inputPath
elseif ($InputType -eq 'inputPath') {
WriteDebug 'Running ''Assert-PSRule'' with input from path.';
Write-Host '';
Write-Host '---';
Assert-PSRule @invokeParams -InputPath $InputPath;
}
}
catch [PSRule.Pipeline.RuleException] {
Write-Host "::error::Rule exception: $($_.Exception.Message)";
Write-Host "$($_.Exception.ScriptStackTrace)";
$Host.SetShouldExit(1);
}
catch [PSRule.Pipeline.FailPipelineException] {
Write-Host "::error::One or more assertions failed. $($_.Exception.Message)";
Write-Host "$($_.Exception.ScriptStackTrace)";
$Host.SetShouldExit(1);
}
catch {
Write-Host "::error::$($_.Exception.Message)";
Write-Host "$($_.Exception.ScriptStackTrace)";
$Host.SetShouldExit(1);
}
finally {
try {
if ($Summary -eq 'true' -and (Test-Path -Path 'reports/ps_rule_summary.md')) {
Get-Content -Path 'reports/ps_rule_summary.md' -Raw > $Env:GITHUB_STEP_SUMMARY;
$Null = Remove-Item -Path 'reports/ps_rule_summary.md' -Force;
}
}
catch {
Write-Host "::warning::Failed to write job summary: $($_.Exception.Message)";
Write-Host "$($_.Exception.ScriptStackTrace)";
}
Pop-Location;
}
Write-Host '---';