generated from ebekker/pwsh-github-action-base
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaction.ps1
483 lines (417 loc) · 17.7 KB
/
action.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
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
#!/usr/bin/env pwsh
$ErrorActionPreference = 'Stop'
## Make sure any modules we depend on are installed
$modulesToInstall = @(
'GitHubActions'
'Pester'
)
$modulesToInstall | ForEach-Object {
if (-not (Get-Module -ListAvailable -All $_)) {
Write-Output "Module [$_] not found, INSTALLING..."
Install-Module $_ -Force
}
}
## Import dependencies
Import-Module GitHubActions -Force
Import-Module Pester -Force
Write-ActionInfo "Running from [$($PSScriptRoot)]"
function splitListInput { $args[0] -split ',' | % { $_.Trim() } }
function writeListInput { $args[0] | % { Write-ActionInfo " - $_" } }
$inputs = @{
test_results_path = Get-ActionInput test_results_path
full_names_filters = Get-ActionInput full_names_filters
include_paths = Get-ActionInput include_paths
exclude_paths = Get-ActionInput exclude_paths
include_tags = Get-ActionInput include_tags
exclude_tags = Get-ActionInput exclude_tags
output_level = Get-ActionInput output_level
report_name = Get-ActionInput report_name
report_title = Get-ActionInput report_title
github_token = Get-ActionInput github_token -Required
skip_check_run = Get-ActionInput skip_check_run
gist_name = Get-ActionInput gist_name
gist_token = Get-ActionInput gist_token
gist_badge_label = Get-ActionInput gist_badge_label
gist_badge_message = Get-ActionInput gist_badge_message
coverage_paths = Get-ActionInput coverage_paths
coverage_report_name = Get-ActionInput coverage_report_name
coverage_report_title = Get-ActionInput coverage_report_title
coverage_gist = Get-ActionInput coverage_gist
coverage_gist_badge_label = Get-ActionInput coverage_gist_badge_label
tests_fail_step = Get-ActionInput tests_fail_step
}
$test_results_dir = Join-Path $PWD _TMP
Write-ActionInfo "Creating test results space"
if (-not (Test-Path -Path $test_results_dir -PathType Container)) {
mkdir $test_results_dir
}
$test_results_path = $inputs.test_results_path
if ($test_results_path) {
Write-ActionInfo "Test Results Path provided as input; skipping Pester tests"
$result_clixml_path = Get-ActionInput result_clixml_path
if ($result_clixml_path) {
$script:pesterResult = Import-Clixml $result_clixml_path
Write-ActionInfo "Pester Result CLIXML provided as input; loaded"
}
}
else {
$full_names_filters = splitListInput $inputs.full_names_filters
$include_paths = splitListInput $inputs.include_paths
$exclude_paths = splitListInput $inputs.exclude_paths
$include_tags = splitListInput $inputs.include_tags
$exclude_tags = splitListInput $inputs.exclude_tags
$coverage_paths = splitListInput $inputs.coverage_paths
$output_level = splitListInput $inputs.output_level
Write-ActionInfo "Running Pester tests with following:"
Write-ActionInfo " * realtive to PWD: $PWD"
$pesterConfig = [PesterConfiguration]::new()
if ($full_names_filters) {
Write-ActionInfo " * full_names_filters:"
writeListInput $full_names_filters
$pesterConfig.Filter.FullName = $full_names_filters
}
else { Write-ActionInfo " * Default full_names_filters"}
if ($include_paths) {
Write-ActionInfo " * include_paths:"
writeListInput $include_paths
$pesterConfig.Run.Path = $include_paths
}
else { Write-ActionInfo " * Default include_paths"}
if ($exclude_paths) {
Write-ActionInfo " * exclude_paths:"
writeListInput $exclude_paths
$pesterConfig.Run.ExcludePath = $exclude_paths
}
else { Write-ActionInfo " * Default exclude_paths"}
if ($include_tags) {
Write-ActionInfo " * include_tags:"
writeListInput $include_tags
$pesterConfig.Filter.Tag = $include_tags
}
else { Write-ActionInfo " * Default include_tags"}
if ($exclude_tags) {
Write-ActionInfo " * exclude_tags:"
writeListInput $exclude_tags
$pesterConfig.Filter.ExcludeTag = $exclude_tags
}
else { Write-ActionInfo " * Default exclude_tags"}
if ($output_level) {
Write-ActionInfo " * output_level: $output_level"
$pesterConfig.Output.Verbosity = $output_level
}
if ($coverage_paths) {
Write-ActionInfo " * coverage_paths:"
writeListInput $coverage_paths
$coverageFiles = @()
foreach ($path in $coverage_paths) {
$coverageFiles += Get-ChildItem $Path -Recurse -Include @("*.ps1","*.psm1") -Exclude "*.Tests.ps1"
}
$pesterConfig.CodeCoverage.Enabled = $true
$pesterConfig.CodeCoverage.Path = $coverageFiles
$coverage_results_path = Join-Path $test_results_dir coverage.xml
$pesterConfig.CodeCoverage.OutputPath = $coverage_results_path
}
if ($inputs.tests_fail_step) {
Write-ActionInfo " * tests_fail_step: true"
}
$test_results_path = Join-Path $test_results_dir test-results.nunit.xml
## TODO: For now, only NUnit is supported in Pester 5.x
##$pesterConfig.TestResult.OutputFormat = ''
$pesterConfig.Run.PassThru = $true
$pesterConfig.TestResult.Enabled = $true
$pesterConfig.TestResult.OutputPath = $test_results_path
$error_message = ''
$error_clixml_path = ''
$result_clixml_path = Join-Path $test_results_dir pester-result.xml
$script:pesterResult = Invoke-Pester -Configuration $pesterConfig -ErrorVariable $pesterError
if ($pesterError) {
Write-ActionWarning "Pester invocation produced error:"
Write-ActionWarning $pesterError
$error_message = "$pesterError"
$error_clixml_path = Join-Path $test_results_dir pester-error.xml
Export-Clixml -InputObject $pesterError -Path $error_clixml_path
}
Export-Clixml -InputObject $pesterResult -Path $result_clixml_path
if ($error_message) {
Set-ActionOutput -Name error_message -Value $error_message
}
if ($error_clixml_path) {
Set-ActionOutput -Name error_clixml_path -Value $error_clixml_path
}
if ($inputs.tests_fail_step -and ($pesterResult.FailedCount -gt 0)) {
$script:stepShouldFail = $true
}
Set-ActionOutput -Name result_clixml_path -Value $result_clixml_path
Set-ActionOutput -Name result_value -Value ($pesterResult.Result)
Set-ActionOutput -Name total_count -Value ($pesterResult.TotalCount)
Set-ActionOutput -Name passed_count -Value ($pesterResult.PassedCount)
Set-ActionOutput -Name failed_count -Value ($pesterResult.FailedCount)
}
function Resolve-EscapeTokens {
param(
[object]$Message,
[object]$Context,
[switch]$UrlEncode
)
$m = ''
$Message = $Message.ToString()
$p2 = -1
$p1 = $Message.IndexOf('%')
while ($p1 -gt -1) {
$m += $Message.Substring($p2 + 1, $p1 - $p2 - 1)
$p2 = $Message.IndexOf('%', $p1 + 1)
if ($p2 -lt 0) {
$m += $Message.Substring($p1)
break
}
$etName = $Message.Substring($p1 + 1, $p2 - $p1 - 1)
if ($etName -eq '') {
$etValue = '%'
}
else {
$etValue = $Context.$etName
}
$m += $etValue
$p1 = $Message.IndexOf('%', $p2 + 1)
}
$m += $Message.Substring($p2 + 1)
if ($UrlEncode) {
$m = [System.Web.HTTPUtility]::UrlEncode($m).Replace('+', '%20')
}
$m
}
function Build-MarkdownReport {
$script:report_name = $inputs.report_name
$script:report_title = $inputs.report_title
if (-not $script:report_name) {
$script:report_name = "TEST_RESULTS_$([datetime]::Now.ToString('yyyyMMdd_hhmmss'))"
}
if (-not $report_title) {
$script:report_title = $report_name
}
$script:test_report_path = Join-Path $test_results_dir test-results.md
& "$PSScriptRoot/nunit-report/nunitxml2md.ps1" -Verbose `
-xmlFile $script:test_results_path `
-mdFile $script:test_report_path -xslParams @{
reportTitle = $script:report_title
}
}
function Build-CoverageReport {
Write-ActionInfo "Building human-readable code-coverage report"
$script:coverage_report_name = $inputs.coverage_report_name
$script:coverage_report_title = $inputs.coverage_report_title
if (-not $script:coverage_report_name) {
$script:coverage_report_name = "COVERAGE_RESULTS_$([datetime]::Now.ToString('yyyyMMdd_hhmmss'))"
}
if (-not $coverage_report_title) {
$script:coverage_report_title = $report_name
}
$script:coverage_report_path = Join-Path $test_results_dir coverage-results.md
& "$PSScriptRoot/jacoco-report/jacocoxml2md.ps1" -Verbose `
-xmlFile $script:coverage_results_path `
-mdFile $script:coverage_report_path -xslParams @{
reportTitle = $script:coverage_report_title
}
& "$PSScriptRoot/jacoco-report/embedmissedlines.ps1" -mdFile $script:coverage_report_path
}
function Publish-ToCheckRun {
param(
[string]$reportData,
[string]$reportName,
[string]$reportTitle
)
if ($env:GITHUB_EVENT_NAME -eq "workflow_dispatch") {
Write-Host "::notice title=Check Run Publishing Skipped::Check run publishing has been skipped as it is not possible to attach check runs to workflows triggered with 'workflow_dispatch'."
} else {
Write-ActionInfo "Publishing Report to GH Workflow"
$ghToken = $inputs.github_token
$ctx = Get-ActionContext
$repo = Get-ActionRepo
$repoFullName = "$($repo.Owner)/$($repo.Repo)"
Write-ActionInfo "Resolving REF"
$ref = $ctx.Sha
if ($ctx.EventName -eq 'pull_request') {
Write-ActionInfo "Resolving PR REF"
$ref = $ctx.Payload.pull_request.head.sha
if (-not $ref) {
Write-ActionInfo "Resolving PR REF as AFTER"
$ref = $ctx.Payload.after
}
}
if (-not $ref) {
Write-ActionError "Failed to resolve REF"
exit 1
}
Write-ActionInfo "Resolved REF as $ref"
Write-ActionInfo "Resolve Repo Full Name as $repoFullName"
Write-ActionInfo "Adding Check Run"
$url = "https://api.github.com/repos/$repoFullName/check-runs"
$hdr = @{
Accept = 'application/vnd.github.antiope-preview+json'
Authorization = "token $ghToken"
}
$bdy = @{
name = $reportName
head_sha = $ref
status = 'completed'
conclusion = 'neutral'
output = @{
title = $reportTitle
summary = "This run completed at ``$([datetime]::Now)``"
text = $reportData
}
}
Invoke-WebRequest -Headers $hdr $url -Method Post -Body ($bdy | ConvertTo-Json)
}
}
function Publish-ToGist {
param(
[string]$reportData,
[string]$coverageData
)
Write-ActionInfo "Publishing Report to GH Workflow"
$reportGistName = $inputs.gist_name
$gist_token = $inputs.gist_token
Write-ActionInfo "Resolved Report Gist Name.....: [$reportGistName]"
$gistsApiUrl = "https://api.github.com/gists"
$apiHeaders = @{
Accept = "application/vnd.github.v3+json"
Authorization = "token $gist_token"
}
## Request all Gists for the current user
$listGistsResp = Invoke-WebRequest -Headers $apiHeaders -Uri $gistsApiUrl
## Parse response content as JSON
$listGists = $listGistsResp.Content | ConvertFrom-Json -AsHashtable
Write-ActionInfo "Got [$($listGists.Count)] Gists for current account"
## Isolate the first Gist with a file matching the expected metadata name
$reportGist = $listGists | Where-Object { $_.files.$reportGistName } | Select-Object -First 1
if ($reportGist) {
Write-ActionInfo "Found the Tests Report Gist!"
## Debugging:
#$reportDataRawUrl = $reportGist.files.$reportGistName.raw_url
#Write-ActionInfo "Fetching Tests Report content from Raw Url"
#$reportDataRawResp = Invoke-WebRequest -Headers $apiHeaders -Uri $reportDataRawUrl
#$reportDataContent = $reportDataRawResp.Content
#if (-not $reportData) {
# Write-ActionWarning "Tests Report content seems to be missing"
# Write-ActionWarning "[$($reportGist.files.$reportGistName)]"
# Write-ActionWarning "[$reportDataContent]"
#}
#else {
# Write-Information "Got existing Tests Report"
#}
}
$gistFiles = @{
$reportGistName = @{
content = $reportData
}
}
if ($inputs.gist_badge_label) {
$gist_badge_label = $inputs.gist_badge_label
$gist_badge_message = $inputs.gist_badge_message
if (-not $gist_badge_message) {
$gist_badge_message = '%Result%'
}
$gist_badge_label = Resolve-EscapeTokens $gist_badge_label $pesterResult -UrlEncode
$gist_badge_message = Resolve-EscapeTokens $gist_badge_message $pesterResult -UrlEncode
$gist_badge_color = switch ($pesterResult.Result) {
'Passed' { 'green' }
'Failed' { 'red' }
default { 'yellow' }
}
$gist_badge_url = "https://img.shields.io/badge/$gist_badge_label-$gist_badge_message-$gist_badge_color"
Write-ActionInfo "Computed Badge URL: $gist_badge_url"
$gistBadgeResult = Invoke-WebRequest $gist_badge_url -ErrorVariable $gistBadgeError
if ($gistBadgeError) {
$gistFiles."$($reportGistName)_badge.txt" = @{ content = $gistBadgeError.Message }
}
else {
$gistFiles."$($reportGistName)_badge.svg" = @{ content = $gistBadgeResult.Content }
}
}
if ($coverageData) {
$gistFiles."$([io.path]::GetFileNameWithoutExtension($reportGistName))_Coverage.md" = @{ content = $coverageData }
}
if ($inputs.coverage_gist_badge_label) {
$coverage_gist_badge_label = $inputs.coverage_gist_badge_label
$coverage_gist_badge_label = Resolve-EscapeTokens $coverage_gist_badge_label $pesterResult -UrlEncode
$coverageXmlData = Select-Xml -Path $coverage_results_path -XPath "/report/counter[@type='LINE']"
$coveredLines = $coverageXmlData.Node.covered
Write-Host "Covered Lines: $coveredLines"
$missedLines = $coverageXmlData.Node.missed
Write-Host "Missed Lines: $missedLines"
if ($missedLines -eq 0) {
$coveragePercentage = 100
} else {
$coveragePercentage = [math]::Round(100 - (($missedLines / $coveredLines) * 100))
}
$coveragePercentageString = "$coveragePercentage%"
if ($coveragePercentage -eq 100) {
$coverage_gist_badge_color = 'brightgreen'
} elseif ($coveragePercentage -ge 80) {
$coverage_gist_badge_color = 'green'
} elseif ($coveragePercentage -ge 60) {
$coverage_gist_badge_color = 'yellowgreen'
} elseif ($coveragePercentage -ge 40) {
$coverage_gist_badge_color = 'yellow'
} elseif ($coveragePercentage -ge 20) {
$coverage_gist_badge_color = 'orange'
} else {
$coverage_gist_badge_color = 'red'
}
$coverage_gist_badge_url = "https://img.shields.io/badge/$coverage_gist_badge_label-$coveragePercentageString-$coverage_gist_badge_color"
Write-ActionInfo "Computed Coverage Badge URL: $coverage_gist_badge_url"
$coverageGistBadgeResult = Invoke-WebRequest $coverage_gist_badge_url -ErrorVariable $coverageGistBadgeError
if ($coverageGistBadgeError) {
$gistFiles."$($reportGistName)_coverage_badge.txt" = @{ content = $coverageGistBadgeError.Message }
}
else {
$gistFiles."$($reportGistName)_coverage_badge.svg" = @{ content = $coverageGistBadgeResult.Content }
}
}
if (-not $reportGist) {
Write-ActionInfo "Creating initial Tests Report Gist"
$createGistResp = Invoke-WebRequest -Headers $apiHeaders -Uri $gistsApiUrl -Method Post -Body (@{
public = $true ## Set thit to false to make it a Secret Gist
files = $gistFiles
} | ConvertTo-Json)
$createGist = $createGistResp.Content | ConvertFrom-Json -AsHashtable
$reportGist = $createGist
Write-ActionInfo "Create Response: $createGistResp"
}
else {
Write-ActionInfo "Updating Tests Report Gist"
$updateGistUrl = "$gistsApiUrl/$($reportGist.id)"
$updateGistResp = Invoke-WebRequest -Headers $apiHeaders -Uri $updateGistUrl -Method Patch -Body (@{
files = $gistFiles
} | ConvertTo-Json)
Write-ActionInfo "Update Response: $updateGistResp"
}
}
if ($test_results_path) {
Set-ActionOutput -Name test_results_path -Value $test_results_path
Build-MarkdownReport
$reportData = [System.IO.File]::ReadAllText($test_report_path)
if ($coverage_results_path) {
Set-ActionOutput -Name coverage_results_path -Value $coverage_results_path
Build-CoverageReport
$coverageSummaryData = [System.IO.File]::ReadAllText($coverage_report_path)
}
if ($inputs.skip_check_run -ne $true) {
Publish-ToCheckRun -ReportData $reportData -ReportName $report_name -ReportTitle $report_title
if ($coverage_results_path) {
Publish-ToCheckRun -ReportData $coverageSummaryData -ReportName $coverage_report_name -ReportTitle $coverage_report_title
}
}
if ($inputs.gist_name -and $inputs.gist_token) {
if ($inputs.coverage_gist) {
Publish-ToGist -ReportData $reportData -CoverageData $coverageSummaryData
} else {
Publish-ToGist -ReportData $reportData
}
}
}
if ($stepShouldFail) {
Write-ActionInfo "Thowing error as ne or more tests failed and 'tests_fail_step' was true."
throw "One or more tests failed."
}