generated from ebekker/pwsh-github-action-base
-
Notifications
You must be signed in to change notification settings - Fork 36
/
action.ps1
373 lines (316 loc) · 13.8 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
#!/usr/bin/env pwsh
## You interface with the Actions/Workflow system by interacting
## with the environment. The `GitHubActions` module makes this
## easier and more natural by wrapping up access to the Workflow
## environment in PowerShell-friendly constructions and idioms
if (-not (Get-Module -ListAvailable GitHubActions)) {
## Make sure the GH Actions module is installed from the Gallery
Install-Module GitHubActions -Force
}
## Load up some common functionality for interacting
## with the GitHub Actions/Workflow environment
Import-Module GitHubActions
. $PSScriptRoot/action_helpers.ps1
$inputs = @{
test_results_path = Get-ActionInput test_results_path
project_path = Get-ActionInput project_path
no_restore = Get-ActionInput no_restore
no_build = Get-ActionInput no_build
msbuild_configuration = Get-ActionInput msbuild_configuration
msbuild_verbosity = Get-ActionInput msbuild_verbosity
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_badge_label = Get-ActionInput gist_badge_label
gist_badge_message = Get-ActionInput gist_badge_message
gist_is_secret = Get-ActionInput gist_is_secret
gist_token = Get-ActionInput gist_token -Required
set_check_status_from_test_outcome = Get-ActionInput set_check_status_from_test_outcome
trx_xsl_path = Get-ActionInput trx_xsl_path
extra_test_parameters = Get-ActionInput extra_test_parameters
fail_build_on_failed_tests = Get-ActionInput fail_build_on_failed_tests
}
$tmpDir = [System.IO.Path]::Combine($PWD, '_TMP')
Write-ActionInfo "Resolved tmpDir as [$tmpDir]"
$test_results_path = $inputs.test_results_path
$test_report_path = Join-Path $tmpDir test-results.md
New-Item -Name $tmpDir -ItemType Directory -Force -ErrorAction Ignore
function Build-MarkdownReport {
$script:report_name = $inputs.report_name
$script:report_title = $inputs.report_title
$script:trx_xsl_path = $inputs.trx_xsl_path
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 $tmpDir test-results.md
$trx2mdParams = @{
trxFile = $script:test_results_path
mdFile = $script:test_report_path
xslParams = @{
reportTitle = $script:report_title
}
}
if ($script:trx_xsl_path) {
$script:trx_xsl_path = "$(Resolve-Path $script:trx_xsl_path)"
Write-ActionInfo "Override TRX XSL Path Provided"
Write-ActionInfo " resolved as: $($script:trx_xsl_path)"
if (Test-Path $script:trx_xsl_path) {
## If XSL path is provided and exists, override the default
$trx2mdParams.xslFile = $script:trx_xsl_path
}
else {
Write-ActionWarning "Could not find TRX XSL at resolved path; IGNORING"
}
}
& "$PSScriptRoot/trx-report/trx2md.ps1" @trx2mdParams -Verbose
}
function Publish-ToCheckRun {
param(
[string]$reportData
)
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"
$conclusion = 'neutral'
# Set check status based on test result outcome.
if ($inputs.set_check_status_from_test_outcome) {
Write-ActionInfo "Mapping check status to test outcome..."
if ($testResult.ResultSummary_outcome -eq "Failed") {
Write-ActionWarning "Found failing tests"
$conclusion = 'failure'
}
elseif ($testResult.ResultSummary_outcome -eq "Completed") {
Write-ActionInfo "All tests passed"
$conclusion = 'success'
}
}
$url = "https://api.github.com/repos/$repoFullName/check-runs"
$hdr = @{
Accept = 'application/vnd.github.antiope-preview+json'
Authorization = "token $ghToken"
}
$bdy = @{
name = $report_name
head_sha = $ref
status = 'completed'
conclusion = $conclusion
output = @{
title = $report_title
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
)
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.v2+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 = '%ResultSummary_outcome%'
}
$gist_badge_label = Resolve-EscapeTokens $gist_badge_label $testResult -UrlEncode
$gist_badge_message = Resolve-EscapeTokens $gist_badge_message $testResult -UrlEncode
$gist_badge_color = switch ($testResult.ResultSummary_outcome) {
'Completed' { '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 (-not $reportGist) {
Write-ActionInfo "Creating initial Tests Report Gist"
$createGistResp = Invoke-WebRequest -Headers $apiHeaders -Uri $gistsApiUrl -Method Post -Body (@{
public = ($inputs.gist_is_secret -ne $true) ## Public or Secret Gist?
files = $gistFiles
} | ConvertTo-Json)
$createGist = $createGistResp.Content | ConvertFrom-Json -AsHashtable
$reportGist = $createGist
Write-ActionInfo "Create Response: $createGistResp"
Set-ActionOutput -Name gist_report_url -Value $createGist.html_url
if ($gist_badge_label) {
Set-ActionOutput -Name gist_badge_url -Value $createGist.files."$($reportGistName)_badge.svg".raw_url
}
}
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)
$updateGist = $updateGistResp.Content | ConvertFrom-Json -AsHashtable
Write-ActionInfo "Update Response: $updateGistResp"
Set-ActionOutput -Name gist_report_url -Value $updateGist.html_url
if ($gist_badge_label) {
Set-ActionOutput -Name gist_badge_url -Value $updateGist.files."$($reportGistName)_badge.svg".raw_url
}
}
}
[bool]$testsfailed = $false
if ($test_results_path) {
Write-ActionInfo "TRX Test Results Path provided as input; skipping test invocation"
}
else {
$dotnet = Get-Command dotnet -ErrorAction SilentlyContinue
if (-not $dotnet) {
Write-ActionError "Unable to resolve the `dotnet` executable; ABORTING!"
exit 1
}
else {
Write-ActionInfo "Resolved `dotnet` executable:"
Write-ActionInfo " * path.......: [$($dotnet.Path)]"
$dotnetVersion = & $dotnet.Path --version
Write-ActionInfo " * version....: [$($dotnetVersion)]"
}
$trxName = 'test-results.trx'
$test_results_path = Join-Path $tmpDir $trxName
$no_restore = $inputs.no_restore
$no_build = $inputs.no_build
$msbuild_configuration = $inputs.msbuild_configuration
$msbuild_verbosity = $inputs.msbuild_verbosity
if (-not $msbuild_verbosity) {
$msbuild_verbosity = 'normal'
}
$dotnetArgs = @(
'test'
'--verbosity',$msbuild_verbosity
'--results-directory',$tmpDir
'--logger',"`"trx;LogFileName=$trxName`""
)
if ($msbuild_configuration) {
$dotnetArgs += '--configuration'
$dotnetArgs += $msbuild_configuration
}
if ($no_restore -eq 'true') {
$dotnetArgs += '--no-restore'
}
if ($no_build -eq 'true') {
$dotnetArgs += '--no-build'
}
if ($inputs.extra_test_parameters) {
# we need to add the extra parameters to the array @dotnetArgs
$dotnetArgs += $inputs.extra_test_parameters -split ' '
}
# The project path has to be after all switches so this needs to be last
if ($inputs.project_path) {
$dotnetArgs += $inputs.project_path
}
Write-ActionInfo "Assembled test invocation arguments:"
Write-ActionInfo " $dotnetArgs"
Write-ActionInfo "Invoking..."
& $dotnet.Path @dotnetArgs
if (-not $?) {
$testsfailed = $true
Write-ActionWarning "Execution of tests returned failure: $LASTEXITCODE"
}
if (-not (Test-Path -PathType Leaf $test_results_path)) {
Write-ActionWarning "Execution of tests DID NOT PRODUCE a tests results file"
}
}
if ($test_results_path) {
Set-ActionOutput -Name test_results_path -Value $test_results_path
Write-ActionInfo "Compiling Test Result object"
$testResultXml = Select-Xml -Path $test_results_path -XPath /
$testResult = [psobject]::new()
$testResultXml.Node.TestRun.Attributes | % { $testResult |
Add-Member -MemberType NoteProperty -Name "TestRun_$($_.Name)" -Value $_.Value }
$testResultXml.Node.TestRun.Times.Attributes | % { $testResult |
Add-Member -MemberType NoteProperty -Name "Times_$($_.Name)" -Value $_.Value }
$testResultXml.Node.TestRun.ResultSummary.Attributes | % { $testResult |
Add-Member -MemberType NoteProperty -Name "ResultSummary_$($_.Name)" -Value $_.Value }
$testResultXml.Node.TestRun.ResultSummary.Counters.Attributes | % { $testResult |
Add-Member -MemberType NoteProperty -Name "Counters_$($_.Name)" -Value $_.Value }
Write-ActionInfo "$($testResult|Out-Default)"
$result_clixml_path = Join-Path $tmpDir dotnet-test-result.clixml
Export-Clixml -InputObject $testResult -Path $result_clixml_path
Set-ActionOutput -Name result_clixml_path -Value $result_clixml_path
Set-ActionOutput -Name result_value -Value ($testResult.ResultSummary_outcome)
Set-ActionOutput -Name total_count -Value ($testResult.Counters_total)
Set-ActionOutput -Name passed_count -Value ($testResult.Counters_passed)
Set-ActionOutput -Name failed_count -Value ($testResult.Counters_failed)
Write-ActionInfo "Generating Markdown Report from TRX file"
Build-MarkdownReport
$reportData = [System.IO.File]::ReadAllText($test_report_path)
if ($inputs.skip_check_run -ne $true) {
Publish-ToCheckRun -ReportData $reportData
}
if ($inputs.gist_name -and $inputs.gist_token) {
Publish-ToGist -ReportData $reportData
}
}
if ($testsfailed -and $inputs.fail_build_on_failed_tests) {
Write-ActionError "Tests failed so failing build..."
exit 1
}