Skip to content

Commit 1ffae17

Browse files
committed
20220104C
1 parent 5ce6f7d commit 1ffae17

File tree

3 files changed

+95
-49
lines changed

3 files changed

+95
-49
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Import-Module -Name 'hugoalh.GitHubActionsToolkit' -Scope Local
4343

4444
- `Add-GHActionsEnvironmentVariable`
4545
- `Add-GHActionsPATH`
46+
- `Add-GHActionsProblemMatcher`
4647
- `Add-GHActionsSecretMask`
4748
- `Disable-GHActionsCommandEcho`
4849
- `Disable-GHActionsProcessingCommand`
@@ -54,6 +55,7 @@ Import-Module -Name 'hugoalh.GitHubActionsToolkit' -Scope Local
5455
- `Get-GHActionsIsDebug`
5556
- `Get-GHActionsState`
5657
- `Get-GHActionsWebhookEventPayload`
58+
- `Remove-GHActionsProblemMatcher`
5759
- `Set-GHActionsOutput`
5860
- `Set-GHActionsState`
5961
- `Write-GHActionsAnnotation`

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
FunctionsToExport = @(
6464
'Add-GHActionsEnvironmentVariable',
6565
'Add-GHActionsPATH',
66+
'Add-GHActionsProblemMatcher',
6667
'Add-GHActionsSecretMask',
6768
'Disable-GHActionsCommandEcho',
6869
'Disable-GHActionsProcessingCommand',
@@ -74,6 +75,7 @@
7475
'Get-GHActionsIsDebug',
7576
'Get-GHActionsState',
7677
'Get-GHActionsWebhookEventPayload',
78+
'Remove-GHActionsProblemMatcher',
7779
'Set-GHActionsOutput',
7880
'Set-GHActionsState',
7981
'Write-GHActionsAnnotation',

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psm1

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@
22
#Requires -Version 7.2
33
enum GHActionsAnnotationType {
44
Notice = 0
5+
N = 0
6+
Note = 0
57
Warning = 1
8+
W = 1
69
Warn = 1
710
Error = 2
11+
E = 2
812
}
913
<#
1014
.SYNOPSIS
11-
GitHub Actions - Internal - Escape Characters
15+
GitHub Actions - Internal - Format Command
1216
.DESCRIPTION
13-
An internal function to escape characters that could cause issues.
17+
An internal function to escape command characters that could cause issues.
1418
.PARAMETER InputObject
15-
String that need to escape characters.
16-
.PARAMETER Command
17-
Also escape command properties characters.
19+
String that need to escape command characters.
20+
.PARAMETER Property
21+
Also escape command property characters.
1822
.OUTPUTS
1923
String
2024
#>
21-
function Format-GHActionsEscapeCharacters {
25+
function Format-GHActionsCommand {
2226
[CmdletBinding()][OutputType([string])]
2327
param(
2428
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][AllowEmptyString()][string]$InputObject,
25-
[switch]$Command
29+
[switch]$Property
2630
)
2731
begin {}
2832
process {
2933
[string]$Result = $InputObject -replace '%', '%25' -replace "`n", '%0A' -replace "`r", '%0D'
30-
if ($Command) {
34+
if ($Property) {
3135
$Result = $Result -replace ',', '%2C' -replace ':', '%3A'
3236
}
3337
return $Result
@@ -54,7 +58,7 @@ function Test-GHActionsEnvironmentVariable {
5458
if (($InputObject -match '^[\da-z_]+=.+$') -and (($InputObject -split '=').Count -eq 2)) {
5559
return $true
5660
}
57-
Write-Error -Message "Input `"$InputObject`" is not match the require environment variable pattern." -Category SyntaxError
61+
Write-Error -Message "Input `"$InputObject`" is not match the require environment variable pattern!" -Category SyntaxError
5862
return $false
5963
}
6064
end {}
@@ -76,17 +80,17 @@ Void
7680
function Write-GHActionsCommand {
7781
[CmdletBinding()][OutputType([void])]
7882
param (
79-
[Parameter(Mandatory = $true, Position = 0)][string]$Command,
83+
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Command,
8084
[Parameter(Mandatory = $true, Position = 1)][AllowEmptyString()][string]$Message,
8185
[Parameter(Position = 2)][hashtable]$Properties = @{}
8286
)
8387
[string]$Result = "::$Command"
8488
if ($Properties.Count -gt 0) {
8589
$Result += " $($($Properties.GetEnumerator() | ForEach-Object -Process {
86-
return "$($_.Name)=$(Format-GHActionsEscapeCharacters -InputObject $_.Value -Command)"
90+
return "$($_.Name)=$(Format-GHActionsCommand -InputObject $_.Value -Property)"
8791
}) -join ',')"
8892
}
89-
$Result += "::$(Format-GHActionsEscapeCharacters -InputObject $Message)"
93+
$Result += "::$(Format-GHActionsCommand -InputObject $Message)"
9094
Write-Host -Object $Result
9195
}
9296
<#
@@ -95,7 +99,7 @@ GitHub Actions - Add Environment Variable
9599
.DESCRIPTION
96100
Add environment variable to the system environment variables and automatically makes it available to all subsequent actions in the current job; The currently running action cannot access the updated environment variables.
97101
.PARAMETER InputObject
98-
Environment variables.
102+
Environment variable.
99103
.PARAMETER Name
100104
Environment variable name.
101105
.PARAMETER Value
@@ -104,18 +108,18 @@ Environment variable value.
104108
Void
105109
#>
106110
function Add-GHActionsEnvironmentVariable {
107-
[CmdletBinding(DefaultParameterSetName = 'single')][OutputType([void])]
111+
[CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
108112
param(
109-
[Parameter(Mandatory = $true, ParameterSetName = 'multiple', Position = 0, ValueFromPipeline = $true)]$InputObject,
110-
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 0)][ValidatePattern('^[\da-z_]+$')][string]$Name,
111-
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 1)][ValidatePattern('^.+$')][string]$Value
113+
[Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)]$InputObject,
114+
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^[\da-z_]+$')][string]$Name,
115+
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][ValidatePattern('^.+$')][string]$Value
112116
)
113117
begin {
114118
[hashtable]$Result = @{}
115119
}
116120
process {
117121
switch ($PSCmdlet.ParameterSetName) {
118-
'multiple' {
122+
'1' {
119123
switch ($InputObject.GetType().Name) {
120124
'Hashtable' {
121125
$InputObject.GetEnumerator() | ForEach-Object -Process {
@@ -124,25 +128,18 @@ function Add-GHActionsEnvironmentVariable {
124128
}
125129
}
126130
}
127-
{$_ -in @('PSObject', 'PSCustomObject')} {
128-
$InputObject.PSObject.Properties | ForEach-Object -Process {
129-
if (Test-GHActionsEnvironmentVariable -InputObject "$($_.Name)=$($_.Value)") {
130-
$Result[$_.Name] = $_.Value
131-
}
132-
}
133-
}
134131
'String' {
135132
if (Test-GHActionsEnvironmentVariable -InputObject $InputObject) {
136133
[string[]]$InputObjectSplit = $InputObject.Split('=')
137134
$Result[$InputObjectSplit[0]] = $InputObjectSplit[1]
138135
}
139136
}
140137
default {
141-
Write-Error -Message 'Parameter `InputObject` must be hashtable, object, or string!' -Category InvalidType
138+
Write-Error -Message 'Parameter `InputObject` must be hashtable or string!' -Category InvalidType
142139
}
143140
}
144141
}
145-
'single' {
142+
'2' {
146143
$Result[$Name] = $Value
147144
}
148145
}
@@ -176,14 +173,31 @@ function Add-GHActionsPATH {
176173
if (Test-Path -Path $_ -IsValid) {
177174
$Result += $_
178175
} else {
179-
Write-Error -Message "Input `"$_`" is not match the require path pattern." -Category SyntaxError
176+
Write-Error -Message "Input `"$_`" is not match the require path pattern!" -Category SyntaxError
180177
}
181178
}
182179
}
183180
end {
184181
Add-Content -Path $env:GITHUB_PATH -Value "$($Result -join "`n")" -Encoding utf8NoBOM
185182
}
186183
}
184+
function Add-GHActionsProblemMatcher {
185+
[CmdletBinding()][OutputType([void])]
186+
param (
187+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][SupportsWildcards()][string[]]$Path
188+
)
189+
begin {}
190+
process {
191+
Resolve-Path -Path $Path -Relative | ForEach-Object -Process {
192+
if ((Test-Path -Path $_ -PathType Leaf) -and ((Split-Path -Path $_ -Extension) -eq '.json')) {
193+
Write-GHActionsCommand -Command 'add-matcher' -Message ($_ -replace '^\.\\', '' -replace '\\', '/')
194+
} else {
195+
Write-Error -Message "Path `"$_`" is not exist or match the require path pattern!" -Category SyntaxError
196+
}
197+
}
198+
}
199+
end {}
200+
}
187201
<#
188202
.SYNOPSIS
189203
GitHub Actions - Add Secret Mask
@@ -228,8 +242,9 @@ String
228242
#>
229243
function Disable-GHActionsProcessingCommand {
230244
[CmdletBinding()][OutputType([string])]
231-
param()
232-
[string]$EndToken = (New-Guid).Guid
245+
param(
246+
[Parameter(Position = 0)][ValidatePattern('^.+$')][string]$EndToken = (New-Guid).Guid
247+
)
233248
Write-GHActionsCommand -Command 'stop-commands' -Message $EndToken
234249
return $EndToken
235250
}
@@ -259,7 +274,7 @@ Void
259274
function Enable-GHActionsProcessingCommand {
260275
[CmdletBinding()][OutputType([void])]
261276
param(
262-
[Parameter(Mandatory = $true, Position = 0)][string]$EndToken
277+
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$EndToken
263278
)
264279
Write-GHActionsCommand -Command $EndToken -Message ''
265280
}
@@ -276,7 +291,7 @@ Void
276291
function Enter-GHActionsLogGroup {
277292
[CmdletBinding()][OutputType([void])]
278293
param(
279-
[Parameter(Mandatory = $true, Position = 0)][string]$Title
294+
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Title
280295
)
281296
Write-GHActionsCommand -Command 'group' -Message $Title
282297
}
@@ -320,7 +335,7 @@ function Get-GHActionsInput {
320335
process {
321336
$Name | ForEach-Object -Process {
322337
$InputValue = Get-ChildItem -Path "Env:\INPUT_$($_.ToUpper() -replace '[ \n\r]','_')" -ErrorAction SilentlyContinue
323-
if ($InputValue -eq $null) {
338+
if ($null -eq $InputValue) {
324339
if ($Require) {
325340
throw "Input ``$_`` is not defined!"
326341
}
@@ -381,7 +396,7 @@ function Get-GHActionsState {
381396
process {
382397
$Name | ForEach-Object -Process {
383398
$StateValue = Get-ChildItem -Path "Env:\STATE_$($_.ToUpper() -replace '[ \n\r]','_')" -ErrorAction SilentlyContinue
384-
if ($StateValue -eq $null) {
399+
if ($null -eq $StateValue) {
385400
$Result[$_] = $StateValue
386401
} else {
387402
if ($Trim) {
@@ -404,14 +419,41 @@ function Get-GHActionsState {
404419
GitHub Actions - Get Webhook Event Payload
405420
.DESCRIPTION
406421
Get the complete webhook event payload.
422+
.PARAMETER AsHashTable
423+
Output as hashtable instead of object.
407424
.OUTPUTS
408-
PSCustomObject
425+
Hashtable | PSCustomObject
409426
#>
410427
function Get-GHActionsWebhookEventPayload {
411-
[CmdletBinding()][OutputType([pscustomobject])]
412-
param ()
413-
return (Get-Content -Path $env:GITHUB_EVENT_PATH -Raw -Encoding utf8NoBOM | ConvertFrom-Json)
428+
[CmdletBinding()][OutputType([hashtable], [pscustomobject])]
429+
param (
430+
[switch]$AsHashTable
431+
)
432+
return (Get-Content -Path $env:GITHUB_EVENT_PATH -Raw -Encoding utf8NoBOM | ConvertFrom-Json -AsHashtable:$AsHashTable)
414433
}
434+
function Remove-GHActionsProblemMatcher {
435+
[CmdletBinding()][OutputType([void])]
436+
param (
437+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string[]]$Owner
438+
)
439+
begin {}
440+
process {
441+
$Owner | ForEach-Object -Process {
442+
Write-GHActionsCommand -Command 'remove-matcher' -Message '' -Properties @{ 'owner' = $_ }
443+
}
444+
}
445+
end {}
446+
}
447+
<#
448+
function Save-GHActionsCache {
449+
[CmdletBinding()][OutputType([void])]
450+
param (
451+
[Parameter(Mandatory = $true, Position = 0)][ValidateLength(1,512)][ValidatePattern('^[\da-z._-]+$')][string]$Key,
452+
[Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][SupportsWildcards()][string[]]
453+
$Path
454+
)
455+
}
456+
#>
415457
<#
416458
.SYNOPSIS
417459
GitHub Actions - Set Output
@@ -427,7 +469,7 @@ Void
427469
function Set-GHActionsOutput {
428470
[CmdletBinding()][OutputType([void])]
429471
param(
430-
[Parameter(Mandatory = $true, Position = 0)][string]$Name,
472+
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Name,
431473
[Parameter(Mandatory = $true, Position = 1)][string]$Value
432474
)
433475
Write-GHActionsCommand -Command 'set-output' -Message $Value -Properties @{ 'name' = $Name }
@@ -447,7 +489,7 @@ Void
447489
function Set-GHActionsState {
448490
[CmdletBinding()][OutputType([void])]
449491
param(
450-
[Parameter(Mandatory = $true, Position = 0)][string]$Name,
492+
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Name,
451493
[Parameter(Mandatory = $true, Position = 1)][string]$Value
452494
)
453495
Write-GHActionsCommand -Command 'save-state' -Message $Value -Properties @{ 'name' = $Name }
@@ -481,12 +523,12 @@ function Write-GHActionsAnnotation {
481523
param (
482524
[Parameter(Mandatory = $true, Position = 0)][GHActionsAnnotationType]$Type,
483525
[Parameter(Mandatory = $true, Position = 1)][string]$Message,
484-
[Parameter()][string]$File,
526+
[Parameter()][ValidatePattern('^.*$')][string]$File,
485527
[Parameter()][uint]$Line,
486528
[Parameter()][uint]$Col,
487529
[Parameter()][uint]$EndLine,
488530
[Parameter()][uint]$EndColumn,
489-
[Parameter()][string]$Title
531+
[Parameter()][ValidatePattern('^.*$')][string]$Title
490532
)
491533
[hashtable]$Properties = @{}
492534
if ($File.Length -gt 0) {
@@ -556,12 +598,12 @@ function Write-GHActionsError {
556598
[CmdletBinding()][OutputType([void])]
557599
param (
558600
[Parameter(Mandatory = $true, Position = 0)][string]$Message,
559-
[Parameter()][string]$File,
601+
[Parameter()][ValidatePattern('^.*$')][string]$File,
560602
[Parameter()][uint]$Line,
561603
[Parameter()][uint]$Col,
562604
[Parameter()][uint]$EndLine,
563605
[Parameter()][uint]$EndColumn,
564-
[Parameter()][string]$Title
606+
[Parameter()][ValidatePattern('^.*$')][string]$Title
565607
)
566608
Write-GHActionsAnnotation -Type 'Error' -Message $Message -File $File -Line $Line -Col $Col -EndLine $EndLine -EndColumn $EndColumn -Title $Title
567609
}
@@ -609,12 +651,12 @@ function Write-GHActionsNotice {
609651
[CmdletBinding()][OutputType([void])]
610652
param (
611653
[Parameter(Mandatory = $true, Position = 0)][string]$Message,
612-
[Parameter()][string]$File,
654+
[Parameter()][ValidatePattern('^.*$')][string]$File,
613655
[Parameter()][uint]$Line,
614656
[Parameter()][uint]$Col,
615657
[Parameter()][uint]$EndLine,
616658
[Parameter()][uint]$EndColumn,
617-
[Parameter()][string]$Title
659+
[Parameter()][ValidatePattern('^.*$')][string]$Title
618660
)
619661
Write-GHActionsAnnotation -Type 'Notice' -Message $Message -File $File -Line $Line -Col $Col -EndLine $EndLine -EndColumn $EndColumn -Title $Title
620662
}
@@ -644,13 +686,13 @@ function Write-GHActionsWarning {
644686
[CmdletBinding()][OutputType([void])]
645687
param (
646688
[Parameter(Mandatory = $true, Position = 0)][string]$Message,
647-
[Parameter()][string]$File,
689+
[Parameter()][ValidatePattern('^.*$')][string]$File,
648690
[Parameter()][uint]$Line,
649691
[Parameter()][uint]$Col,
650692
[Parameter()][uint]$EndLine,
651693
[Parameter()][uint]$EndColumn,
652-
[Parameter()][string]$Title
694+
[Parameter()][ValidatePattern('^.*$')][string]$Title
653695
)
654696
Write-GHActionsAnnotation -Type 'Warning' -Message $Message -File $File -Line $Line -Col $Col -EndLine $EndLine -EndColumn $EndColumn -Title $Title
655697
}
656-
Export-ModuleMember -Function Add-GHActionsEnvironmentVariable, Add-GHActionsPATH, Add-GHActionsSecretMask, Disable-GHActionsCommandEcho, Disable-GHActionsProcessingCommand, Enable-GHActionsCommandEcho, Enable-GHActionsProcessingCommand, Enter-GHActionsLogGroup, Exit-GHActionsLogGroup, Get-GHActionsInput, Get-GHActionsIsDebug, Get-GHActionsState, Get-GHActionsWebhookEventPayload, Set-GHActionsOutput, Set-GHActionsState, Write-GHActionsAnnotation, Write-GHActionsDebug, Write-GHActionsError, Write-GHActionsFail, Write-GHActionsNotice, Write-GHActionsWarning
698+
Export-ModuleMember -Function Add-GHActionsEnvironmentVariable, Add-GHActionsPATH, Add-GHActionsProblemMatcher, Add-GHActionsSecretMask, Disable-GHActionsCommandEcho, Disable-GHActionsProcessingCommand, Enable-GHActionsCommandEcho, Enable-GHActionsProcessingCommand, Enter-GHActionsLogGroup, Exit-GHActionsLogGroup, Get-GHActionsInput, Get-GHActionsIsDebug, Get-GHActionsState, Get-GHActionsWebhookEventPayload, Remove-GHActionsProblemMatcher, Set-GHActionsOutput, Set-GHActionsState, Write-GHActionsAnnotation, Write-GHActionsDebug, Write-GHActionsError, Write-GHActionsFail, Write-GHActionsNotice, Write-GHActionsWarning

0 commit comments

Comments
 (0)