generated from PSModule/Template-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ps1
More file actions
492 lines (445 loc) · 20.6 KB
/
main.ps1
File metadata and controls
492 lines (445 loc) · 20.6 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'powershell-yaml', 'Hashtable' | Install-PSResource -Repository PSGallery -TrustRepository
$name = $env:PSMODULE_GET_SETTINGS_INPUT_Name
$settingsPath = $env:PSMODULE_GET_SETTINGS_INPUT_SettingsPath
$debug = $env:PSMODULE_GET_SETTINGS_INPUT_Debug
$verbose = $env:PSMODULE_GET_SETTINGS_INPUT_Verbose
$version = $env:PSMODULE_GET_SETTINGS_INPUT_Version
$prerelease = $env:PSMODULE_GET_SETTINGS_INPUT_Prerelease
$workingDirectory = $env:PSMODULE_GET_SETTINGS_INPUT_WorkingDirectory
LogGroup 'Inputs' {
[pscustomobject]@{
PWD = (Get-Location).Path
Name = $name
SettingsPath = $settingsPath
} | Format-List | Out-String
}
if (![string]::IsNullOrEmpty($settingsPath) -and (Test-Path -Path $settingsPath)) {
LogGroup 'Import settings' {
$settingsFile = Get-Item -Path $settingsPath
$relativeSettingsPath = $settingsFile | Resolve-Path -Relative
Write-Host "Importing settings from [$relativeSettingsPath]"
$content = $settingsFile | Get-Content -Raw
switch -Regex ($settingsFile.Extension) {
'.json' {
$settings = $content | ConvertFrom-Json
Write-Host ($settings | ConvertTo-Json -Depth 5 | Out-String)
}
'.yaml|.yml' {
$settings = $content | ConvertFrom-Yaml
Write-Host ($settings | ConvertTo-Yaml | Out-String)
}
'.psd1' {
$settings = $content | ConvertFrom-Hashtable
Write-Host ($settings | ConvertTo-Hashtable | Format-Hashtable | Out-String)
}
default {
throw "Unsupported settings file format: [$settingsPath]. Supported formats are json, yaml/yml and psd1."
}
}
}
LogGroup 'Validate settings against schema' {
$schemaPath = Join-Path $PSScriptRoot 'Settings.schema.json'
if (Test-Path -Path $schemaPath) {
Write-Host 'Validating settings against schema...'
$schema = Get-Content $schemaPath -Raw
# Convert settings to JSON for validation
$settingsJson = $settings | ConvertTo-Json -Depth 10
try {
$isValid = Test-Json -Json $settingsJson -Schema $schema -ErrorAction Stop
if ($isValid) {
Write-Host '✓ Settings conform to schema'
} else {
throw 'Settings do not conform to the schema'
}
} catch {
Write-Error "Schema validation failed: $_"
Write-Error 'Your settings file does not match the expected schema structure.'
throw
}
} else {
Write-Warning "Schema file not found at [$schemaPath]. Skipping validation."
}
}
} else {
Write-Host 'No settings file present.'
$settings = @{}
}
LogGroup 'Name' {
[pscustomobject]@{
InputName = $name
SettingsName = $settings.Name
RepositoryName = $env:GITHUB_REPOSITORY_NAME
} | Format-List | Out-String
if (![string]::IsNullOrEmpty($name)) {
Write-Host "Using name from input parameter: [$name]"
} elseif (![string]::IsNullOrEmpty($settings.Name)) {
$name = $settings.Name
Write-Host "Using name from settings file: [$name]"
} else {
$name = $env:GITHUB_REPOSITORY_NAME
Write-Host "Using repository name: [$name]"
}
}
$settings = [pscustomobject]@{
Name = $name
Test = [pscustomobject]@{
Skip = $settings.Test.Skip ?? $false
Linux = [pscustomobject]@{
Skip = $settings.Test.Linux.Skip ?? $false
}
MacOS = [pscustomobject]@{
Skip = $settings.Test.MacOS.Skip ?? $false
}
Windows = [pscustomobject]@{
Skip = $settings.Test.Windows.Skip ?? $false
}
SourceCode = [pscustomobject]@{
Skip = $settings.Test.SourceCode.Skip ?? $false
Linux = [pscustomobject]@{
Skip = $settings.Test.SourceCode.Linux.Skip ?? $false
}
MacOS = [pscustomobject]@{
Skip = $settings.Test.SourceCode.MacOS.Skip ?? $false
}
Windows = [pscustomobject]@{
Skip = $settings.Test.SourceCode.Windows.Skip ?? $false
}
}
PSModule = [pscustomobject]@{
Skip = $settings.Test.PSModule.Skip ?? $false
Linux = [pscustomobject]@{
Skip = $settings.Test.PSModule.Linux.Skip ?? $false
}
MacOS = [pscustomobject]@{
Skip = $settings.Test.PSModule.MacOS.Skip ?? $false
}
Windows = [pscustomobject]@{
Skip = $settings.Test.PSModule.Windows.Skip ?? $false
}
}
Module = [pscustomobject]@{
Skip = $settings.Test.Module.Skip ?? $false
Linux = [pscustomobject]@{
Skip = $settings.Test.Module.Linux.Skip ?? $false
}
MacOS = [pscustomobject]@{
Skip = $settings.Test.Module.MacOS.Skip ?? $false
}
Windows = [pscustomobject]@{
Skip = $settings.Test.Module.Windows.Skip ?? $false
}
}
TestResults = [pscustomobject]@{
Skip = $settings.Test.TestResults.Skip ?? $false
}
CodeCoverage = [pscustomobject]@{
Skip = $settings.Test.CodeCoverage.Skip ?? $false
PercentTarget = $settings.Test.CodeCoverage.PercentTarget ?? 0
StepSummaryMode = $settings.Test.CodeCoverage.StepSummaryMode ?? 'Missed, Files'
}
}
Build = [pscustomobject]@{
Skip = $settings.Build.Skip ?? $false
Module = [pscustomobject]@{
Skip = $settings.Build.Module.Skip ?? $false
}
Docs = [pscustomobject]@{
Skip = $settings.Build.Docs.Skip ?? $false
ShowSummaryOnSuccess = $settings.Build.Docs.ShowSummaryOnSuccess ?? $false
}
Site = [pscustomobject]@{
Skip = $settings.Build.Site.Skip ?? $false
}
}
Publish = [pscustomobject]@{
Module = [pscustomobject]@{
Skip = $settings.Publish.Module.Skip ?? $false
AutoCleanup = $settings.Publish.Module.AutoCleanup ?? $true
AutoPatching = $settings.Publish.Module.AutoPatching ?? $true
IncrementalPrerelease = $settings.Publish.Module.IncrementalPrerelease ?? $true
DatePrereleaseFormat = $settings.Publish.Module.DatePrereleaseFormat ?? ''
VersionPrefix = $settings.Publish.Module.VersionPrefix ?? 'v'
MajorLabels = $settings.Publish.Module.MajorLabels ?? 'major, breaking'
MinorLabels = $settings.Publish.Module.MinorLabels ?? 'minor, feature'
PatchLabels = $settings.Publish.Module.PatchLabels ?? 'patch, fix'
IgnoreLabels = $settings.Publish.Module.IgnoreLabels ?? 'NoRelease'
UsePRTitleAsReleaseName = $settings.Publish.Module.UsePRTitleAsReleaseName ?? $false
UsePRBodyAsReleaseNotes = $settings.Publish.Module.UsePRBodyAsReleaseNotes ?? $true
UsePRTitleAsNotesHeading = $settings.Publish.Module.UsePRTitleAsNotesHeading ?? $true
}
}
Linter = [pscustomobject]@{
Skip = $settings.Linter.Skip ?? $false
ShowSummaryOnSuccess = $settings.Linter.ShowSummaryOnSuccess ?? $false
env = $settings.Linter.env ?? @{}
}
}
# Add input properties to settings
$settings | Add-Member -MemberType NoteProperty -Name SettingsPath -Value $settingsPath
$settings | Add-Member -MemberType NoteProperty -Name Debug -Value $debug
$settings | Add-Member -MemberType NoteProperty -Name Verbose -Value $verbose
$settings | Add-Member -MemberType NoteProperty -Name Version -Value $version
$settings | Add-Member -MemberType NoteProperty -Name Prerelease -Value $prerelease
$settings | Add-Member -MemberType NoteProperty -Name WorkingDirectory -Value $workingDirectory
# Calculate job run conditions
LogGroup 'Calculate Job Run Conditions:' {
# Common conditions
$eventData = $null
try {
$eventData = Get-GitHubEventData -ErrorAction Stop
} catch {
if (-not [string]::IsNullOrEmpty($env:GITHUB_EVENT_PATH) -and (Test-Path -Path $env:GITHUB_EVENT_PATH)) {
$eventData = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
}
}
LogGroup 'GitHub Event Data' {
if ($null -ne $eventData) {
Write-Host ($eventData | ConvertTo-Json -Depth 10 | Out-String)
} else {
Write-Host 'No event data available.'
}
}
$pullRequestAction = if ($null -ne $eventData.Action) {
$eventData.Action
} else {
$env:GITHUB_EVENT_ACTION
}
$pullRequest = if ($null -ne $eventData.PullRequest) {
$eventData.PullRequest
} else {
$null
}
$pullRequestIsMerged = if ($null -ne $pullRequest -and $null -ne $pullRequest.Merged) {
[bool]$pullRequest.Merged
} else {
$false
}
Write-Host 'GitHub event inputs:'
[pscustomobject]@{
GITHUB_EVENT_NAME = $env:GITHUB_EVENT_NAME
GITHUB_EVENT_ACTION = $pullRequestAction
GITHUB_EVENT_PULL_REQUEST_MERGED = $pullRequestIsMerged
} | Format-List | Out-String
$isPR = $env:GITHUB_EVENT_NAME -eq 'pull_request'
$isOpenOrUpdatedPR = $isPR -and $pullRequestAction -in @('opened', 'reopened', 'synchronize')
$isAbandonedPR = $isPR -and $pullRequestAction -eq 'closed' -and $pullRequestIsMerged -ne $true
$isMergedPR = $isPR -and $pullRequestAction -eq 'closed' -and $pullRequestIsMerged -eq $true
$isNotAbandonedPR = -not $isAbandonedPR
[pscustomobject]@{
isPR = $isPR
isOpenOrUpdatedPR = $isOpenOrUpdatedPR
isAbandonedPR = $isAbandonedPR
isMergedPR = $isMergedPR
isNotAbandonedPR = $isNotAbandonedPR
} | Format-List | Out-String
}
# Get-TestSuites
if ($settings.Test.Skip) {
Write-Host 'Skipping all tests.'
$sourceCodeTestSuites = $null
$psModuleTestSuites = $null
$moduleTestSuites = $null
# Add TestSuites to settings
$settings | Add-Member -MemberType NoteProperty -Name TestSuites -Value ([pscustomobject]@{
SourceCode = $null
PSModule = $null
Module = $null
})
} else {
# Define test configurations as an array of hashtables.
$linux = [PSCustomObject]@{ RunsOn = 'ubuntu-latest'; OSName = 'Linux' }
$macOS = [PSCustomObject]@{ RunsOn = 'macos-latest'; OSName = 'macOS' }
$windows = [PSCustomObject]@{ RunsOn = 'windows-latest'; OSName = 'Windows' }
LogGroup 'Source Code Test Suites:' {
$sourceCodeTestSuites = if ($settings.Test.SourceCode.Skip) {
Write-Host 'Skipping all source code tests.'
$null
} else {
$result = @()
if (-not $settings.Test.Linux.Skip -and -not $settings.Test.SourceCode.Linux.Skip) { $result += $linux }
if (-not $settings.Test.MacOS.Skip -and -not $settings.Test.SourceCode.MacOS.Skip) { $result += $macOS }
if (-not $settings.Test.Windows.Skip -and -not $settings.Test.SourceCode.Windows.Skip) { $result += $windows }
if ($result.Count -gt 0) { $result } else { $null }
}
$sourceCodeTestSuites | Format-Table -AutoSize | Out-String
}
LogGroup 'PSModule Test Suites:' {
$psModuleTestSuites = if ($settings.Test.PSModule.Skip) {
Write-Host 'Skipping all PSModule tests.'
$null
} else {
$result = @()
if (-not $settings.Test.Linux.Skip -and -not $settings.Test.PSModule.Linux.Skip) { $result += $linux }
if (-not $settings.Test.MacOS.Skip -and -not $settings.Test.PSModule.MacOS.Skip) { $result += $macOS }
if (-not $settings.Test.Windows.Skip -and -not $settings.Test.PSModule.Windows.Skip) { $result += $windows }
if ($result.Count -gt 0) { $result } else { $null }
}
$psModuleTestSuites | Format-Table -AutoSize | Out-String
}
LogGroup 'Module Local Test Suites:' {
$moduleTestSuites = if ($settings.Test.Module.Skip) {
Write-Host 'Skipping all module tests.'
$null
} else {
# Locate the tests directory.
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
if (-not $testsPath) {
Write-Warning 'No tests found'
return $null
}
Write-Host "Tests found at [$testsPath]"
function Get-TestItemsFromFolder {
<#
.SYNOPSIS
Retrieves test items from a specified folder.
.DESCRIPTION
This function searches for test-related files in the specified folder.
It looks for configuration files (*.Configuration.ps1), container files (*.Container.ps1),
and test files (*.Tests.ps1) in that order of precedence.
.PARAMETER FolderPath
The path to the folder to search for test items.
.OUTPUTS
System.IO.FileInfo[]
Returns an array of test-related files found in the folder.
#>
param ([string]$FolderPath)
$configFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Configuration.ps1'
if ($configFiles.Count -eq 1) {
return @($configFiles)
} elseif ($configFiles.Count -gt 1) {
throw "Multiple configuration files found in [$FolderPath]. Please separate configurations into different folders."
}
$containerFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Container.ps1'
if ($containerFiles.Count -ge 1) {
return $containerFiles
}
$testFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Tests.ps1'
return $testFiles
}
function Find-TestDirectory {
<#
.SYNOPSIS
Finds test directories recursively.
.DESCRIPTION
This function recursively searches for all directories starting from the specified path.
It returns a flat array of all directory paths found.
.PARAMETER Path
The root path to start searching for directories.
.OUTPUTS
System.String[]
Returns an array of directory paths.
#>
param ([string]$Path)
$directories = @()
$childDirs = Get-ChildItem -Path $Path -Directory
foreach ($dir in $childDirs) {
$directories += $dir.FullName
$directories += Find-TestDirectory -Path $dir.FullName
}
return $directories
}
$allTestFolders = @($testsPath) + (Find-TestDirectory -Path $testsPath)
foreach ($folder in $allTestFolders) {
$testItems = Get-TestItemsFromFolder -FolderPath $folder
foreach ($item in $testItems) {
if (-not $settings.Test.Linux.Skip -and -not $settings.Test.Module.Linux.Skip) {
[pscustomobject]@{
RunsOn = $linux.RunsOn
OSName = $linux.OSName
TestPath = Resolve-Path -Path $item.FullName -Relative
TestName = ($item.BaseName).Split('.')[0]
}
}
if (-not $settings.Test.MacOS.Skip -and -not $settings.Test.Module.MacOS.Skip) {
[pscustomobject]@{
RunsOn = $macOS.RunsOn
OSName = $macOS.OSName
TestPath = Resolve-Path -Path $item.FullName -Relative
TestName = ($item.BaseName).Split('.')[0]
}
}
if (-not $settings.Test.Windows.Skip -and -not $settings.Test.Module.Windows.Skip) {
[pscustomobject]@{
RunsOn = $windows.RunsOn
OSName = $windows.OSName
TestPath = Resolve-Path -Path $item.FullName -Relative
TestName = ($item.BaseName).Split('.')[0]
}
}
}
}
}
$moduleTestSuites | Format-Table -AutoSize | Out-String
}
# Add TestSuites to settings
$settings | Add-Member -MemberType NoteProperty -Name TestSuites -Value ([pscustomobject]@{
SourceCode = $sourceCodeTestSuites
PSModule = $psModuleTestSuites
Module = $moduleTestSuites
})
}
# Calculate job-specific conditions and add to settings
LogGroup 'Calculate Job Run Conditions:' {
# Create Run object with all job-specific conditions
$run = [pscustomobject]@{
LintRepository = $isOpenOrUpdatedPR -and (-not $settings.Linter.Skip)
BuildModule = $isNotAbandonedPR -and (-not $settings.Build.Module.Skip)
TestSourceCode = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.SourceCode)
LintSourceCode = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.SourceCode)
TestModule = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.PSModule)
BeforeAllModuleLocal = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.Module)
TestModuleLocal = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.Module)
AfterAllModuleLocal = $true # Always runs if Test-ModuleLocal was not skipped
GetTestResults = $isNotAbandonedPR -and (-not $settings.Test.TestResults.Skip) -and (
($null -ne $settings.TestSuites.SourceCode) -or ($null -ne $settings.TestSuites.PSModule) -or ($null -ne $settings.TestSuites.Module)
)
GetCodeCoverage = $isNotAbandonedPR -and (-not $settings.Test.CodeCoverage.Skip) -and (
($null -ne $settings.TestSuites.PSModule) -or ($null -ne $settings.TestSuites.Module)
)
PublishModule = $isPR -and ($isAbandonedPR -or ($isOpenOrUpdatedPR -or $isMergedPR))
BuildDocs = $isNotAbandonedPR -and (-not $settings.Build.Docs.Skip)
BuildSite = $isNotAbandonedPR -and (-not $settings.Build.Site.Skip)
PublishSite = $isMergedPR
}
$settings | Add-Member -MemberType NoteProperty -Name Run -Value $run
Write-Host 'Job Run Conditions:'
$run | Format-List | Out-String
}
LogGroup 'Final settings' {
switch -Regex ($settingsFile.Extension) {
'.yaml|.yml' {
Write-Host ($settings | ConvertTo-Yaml | Out-String)
}
'.psd1' {
Write-Host ($settings | ConvertTo-Hashtable | Format-Hashtable | Out-String)
}
default {
Write-Host ($settings | ConvertTo-Json -Depth 5 | Out-String)
}
}
Set-GitHubOutput -Name Settings -Value ($settings | ConvertTo-Json -Depth 10)
}
LogGroup 'Validate output settings against schema' {
$schemaPath = Join-Path $PSScriptRoot 'Settings.schema.json'
if (Test-Path -Path $schemaPath) {
Write-Host 'Validating output settings against schema...'
$schema = Get-Content $schemaPath -Raw
# Convert output settings to JSON for validation
$outputJson = $settings | ConvertTo-Json -Depth 10
try {
$isValid = Test-Json -Json $outputJson -Schema $schema -ErrorAction Stop
if ($isValid) {
Write-Host '✓ Output settings conform to schema'
} else {
throw 'Output settings do not conform to the schema'
}
} catch {
Write-Error "Output schema validation failed: $_"
Write-Error 'The generated settings object does not match the expected schema structure.'
Write-Error 'This indicates a bug in the action. Please report this issue.'
throw
}
} else {
Write-Warning "Schema file not found at [$schemaPath]. Skipping output validation."
}
}