Skip to content

Commit c0d2cde

Browse files
committed
20220618A
1 parent 4ad1c1f commit c0d2cde

File tree

9 files changed

+116
-66
lines changed

9 files changed

+116
-66
lines changed

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ String
1313
function Format-CommandParameter {
1414
[CmdletBinding()]
1515
[OutputType([String])]
16-
param (
16+
Param (
1717
[Parameter(Mandatory = $true, Position = 0)][AllowEmptyString()][Alias('Input', 'Object')][String]$InputObject
1818
)
1919
return (Format-CommandValue -InputObject $InputObject) -ireplace ',', '%2C' -ireplace ':', '%3A'
@@ -32,7 +32,7 @@ String
3232
function Format-CommandValue {
3333
[CmdletBinding()]
3434
[OutputType([String])]
35-
param (
35+
Param (
3636
[Parameter(Mandatory = $true, Position = 0)][AllowEmptyString()][Alias('Input', 'Object')][String]$InputObject
3737
)
3838
return $InputObject -ireplace '%', '%25' -ireplace '\n', '%0A' -ireplace '\r', '%0D'
@@ -56,7 +56,7 @@ Void
5656
function Write-Command {
5757
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionscommand#Write-GitHubActionsCommand')]
5858
[OutputType([Void])]
59-
param (
59+
Param (
6060
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid command!')][String]$Command,
6161
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)][Alias('Content', 'Message')][String]$Value = '',
6262
[Parameter(Position = 2, ValueFromPipelineByPropertyName = $true)][Alias('Parameters', 'Property', 'Properties')][Hashtable]$Parameter = @{}

hugoalh.GitHubActionsToolkit/module/command-control.psm1

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Import-Module -Name @(
66
[String[]]$GitHubActionsCommands = @(
77
'add-mask',
88
'add-matcher',
9+
'add-path',
910
'debug',
1011
'echo',
1112
'endgroup',
@@ -30,7 +31,7 @@ Void
3031
function Disable-EchoingCommands {
3132
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_disable-githubactionsechoingcommands#Disable-GitHubActionsEchoingCommands')]
3233
[OutputType([Void])]
33-
param ()
34+
Param ()
3435
return Write-GitHubActionsCommand -Command 'echo' -Value 'off'
3536
}
3637
Set-Alias -Name 'Disable-CommandEcho' -Value 'Disable-EchoingCommands' -Option 'ReadOnly' -Scope 'Local'
@@ -61,9 +62,9 @@ String
6162
function Disable-ProcessingCommands {
6263
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_disable-githubactionsprocessingcommands#Disable-GitHubActionsProcessingCommands')]
6364
[OutputType([String])]
64-
param (
65+
Param (
6566
[Parameter(Position = 0)][ValidateScript({
66-
return ($_ -imatch '^.+$' -and $_.Length -ige 4 -and $_ -inotin $GitHubActionsCommands)
67+
return (Test-ProcessingCommandsEndToken -InputObject $_)
6768
}, ErrorMessage = 'Parameter `EndToken` must be in single line string, more than or equal to 4 characters, not match any GitHub Actions commands, and unique!')][Alias('EndKey', 'EndValue', 'Key', 'Token', 'Value')][String]$EndToken = ((New-Guid).Guid -ireplace '-', '')
6869
)
6970
Write-GitHubActionsCommand -Command 'stop-commands' -Value $EndToken
@@ -95,7 +96,7 @@ Void
9596
function Enable-EchoingCommands {
9697
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_enable-githubactionsechoingcommands#Enable-GitHubActionsEchoingCommands')]
9798
[OutputType([Void])]
98-
param ()
99+
Param ()
99100
return Write-GitHubActionsCommand -Command 'echo' -Value 'on'
100101
}
101102
Set-Alias -Name 'Enable-CommandEcho' -Value 'Enable-EchoingCommands' -Option 'ReadOnly' -Scope 'Local'
@@ -126,9 +127,9 @@ Void
126127
function Enable-ProcessingCommands {
127128
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_enable-githubactionsprocessingcommands#Enable-GitHubActionsProcessingCommands')]
128129
[OutputType([Void])]
129-
param (
130+
Param (
130131
[Parameter(Mandatory = $true, Position = 0)][ValidateScript({
131-
return ($_ -imatch '^.+$' -and $_.Length -ige 4 -and $_ -inotin $GitHubActionsCommands)
132+
return (Test-ProcessingCommandsEndToken -InputObject $_)
132133
}, ErrorMessage = 'Parameter `EndToken` must be in single line string, more than or equal to 4 characters, and not match any GitHub Actions commands!')][Alias('EndKey', 'EndValue', 'Key', 'Token', 'Value')][String]$EndToken
133134
)
134135
return Write-GitHubActionsCommand -Command $EndToken
@@ -148,6 +149,24 @@ Set-Alias -Name 'Start-ProcessCommand' -Value 'Enable-ProcessingCommands' -Optio
148149
Set-Alias -Name 'Start-ProcessCommands' -Value 'Enable-ProcessingCommands' -Option 'ReadOnly' -Scope 'Local'
149150
Set-Alias -Name 'Start-ProcessingCommand' -Value 'Enable-ProcessingCommands' -Option 'ReadOnly' -Scope 'Local'
150151
Set-Alias -Name 'Start-ProcessingCommands' -Value 'Enable-ProcessingCommands' -Option 'ReadOnly' -Scope 'Local'
152+
<#
153+
.SYNOPSIS
154+
GitHub Actions (Internal) - Test Processing Commands End Token
155+
.DESCRIPTION
156+
Test processing commands end token whether is valid.
157+
.PARAMETER InputObject
158+
Processing commands end token that need to test.
159+
.OUTPUTS
160+
Boolean
161+
#>
162+
function Test-ProcessingCommandsEndToken {
163+
[CmdletBinding()]
164+
[OutputType([Boolean])]
165+
Param (
166+
[Parameter(Mandatory = $true, Position = 0)][Alias('Input', 'Object')][String]$InputObject
167+
)
168+
return ($InputObject -imatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -and $InputObject.Length -ige 4 -and $InputObject -inotin $GitHubActionsCommands)
169+
}
151170
Export-ModuleMember -Function @(
152171
'Disable-EchoingCommands',
153172
'Disable-ProcessingCommands',

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#Requires -PSEdition Core
22
#Requires -Version 7.2
3+
Import-Module -Name @(
4+
(Join-Path -Path $PSScriptRoot -ChildPath 'command-base.psm1')
5+
) -Prefix 'GitHubActions' -Scope 'Local'
36
[Flags()] enum GitHubActionsEnvironmentVariableScopes {
47
Current = 1
58
Subsequent = 2
@@ -12,7 +15,7 @@ Add PATH to current step and all subsequent steps in the current job.
1215
.PARAMETER Path
1316
Path.
1417
.PARAMETER NoValidator
15-
Disable validator to not check the PATH is valid or not.
18+
Do not check the PATH whether is valid.
1619
.PARAMETER Scope
1720
Scope of PATH.
1821
.OUTPUTS
@@ -21,7 +24,7 @@ Void
2124
function Add-PATH {
2225
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_add-githubactionspath#Add-GitHubActionsPATH')]
2326
[OutputType([Void])]
24-
param (
27+
Param (
2528
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Path` must be in single line string!')][Alias('Paths')][String[]]$Path,
2629
[Alias('NoValidate', 'SkipValidate', 'SkipValidator')][Switch]$NoValidator,
2730
[GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
@@ -47,7 +50,13 @@ function Add-PATH {
4750
[System.Environment]::SetEnvironmentVariable('PATH', ($PATHRaw -join [System.IO.Path]::PathSeparator))
4851
}
4952
{ $_ -icontains 'Subsequent' } {
50-
Add-Content -LiteralPath $env:GITHUB_PATH -Value ($Result -join "`n") -Confirm:$false -Encoding 'UTF8NoBOM'
53+
if ($null -ieq $env:GITHUB_PATH) {
54+
foreach ($Item in $Result) {
55+
Write-GitHubActionsCommand -Command 'add-path' -Value $Item
56+
}
57+
} else {
58+
Add-Content -LiteralPath $env:GITHUB_PATH -Value ($Result -join "`n") -Confirm:$false -Encoding 'UTF8NoBOM'
59+
}
5160
}
5261
}
5362
}
@@ -66,7 +75,7 @@ Environment variable name.
6675
.PARAMETER Value
6776
Environment variable value.
6877
.PARAMETER NoToUpper
69-
Will not format environment variable name to uppercase.
78+
Do not format environment variable name to uppercase.
7079
.PARAMETER Scope
7180
Scope of environment variable.
7281
.OUTPUTS
@@ -75,10 +84,10 @@ Void
7584
function Set-EnvironmentVariable {
7685
[CmdletBinding(DefaultParameterSetName = 'Multiple', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsenvironmentvariable#Set-GitHubActionsEnvironmentVariable')]
7786
[OutputType([Void])]
78-
param (
87+
Param (
7988
[Parameter(Mandatory = $true, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][Hashtable]$InputObject,
8089
[Parameter(Mandatory = $true, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName = $true)][ValidateScript({
81-
return ($_ -imatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -and $_ -inotmatch '^(?:CI|PATH)$' -and $_ -inotmatch '^(?:ACTIONS|GITHUB|RUNNER)_')
90+
return Test-EnvironmentVariableName -InputObject $_
8291
}, ErrorMessage = '`{0}` is not a valid environment variable name!')][Alias('Key')][String]$Name,
8392
[Parameter(Mandatory = $true, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Value` must be in single line string!')][String]$Value,
8493
[Alias('NoToUppercase')][Switch]$NoToUpper,
@@ -95,11 +104,7 @@ function Set-EnvironmentVariable {
95104
Write-Error -Message 'Parameter `Name` must be type of string!' -Category 'InvalidType'
96105
continue
97106
}
98-
if (
99-
$Item.Name -inotmatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -or
100-
$Item.Name -imatch '^(?:CI|PATH)$' -or
101-
$Item.Name -imatch '^(?:ACTIONS|GITHUB|RUNNER)_'
102-
) {
107+
if (!(Test-EnvironmentVariableName -InputObject $Item.Name)) {
103108
Write-Error -Message "``$($Item.Name)`` is not a valid environment variable name!" -Category 'SyntaxError'
104109
continue
105110
}
@@ -129,9 +134,15 @@ function Set-EnvironmentVariable {
129134
}
130135
}
131136
{ $_ -icontains 'Subsequent' } {
132-
Add-Content -LiteralPath $env:GITHUB_ENV -Value (($ResultEnumerator | ForEach-Object -Process {
133-
return "$($_.Name)=$($_.Value)"
134-
}) -join "`n") -Confirm:$false -Encoding 'UTF8NoBOM'
137+
if ($null -ieq $env:GITHUB_ENV) {
138+
foreach ($Item in $ResultEnumerator) {
139+
Write-GitHubActionsCommand -Command 'set-env' -Value $Item.Value -Parameter @{ 'name' = $Item.Name }
140+
}
141+
} else {
142+
Add-Content -LiteralPath $env:GITHUB_ENV -Value (($ResultEnumerator | ForEach-Object -Process {
143+
return "$($_.Name)=$($_.Value)"
144+
}) -join "`n") -Confirm:$false -Encoding 'UTF8NoBOM'
145+
}
135146
}
136147
}
137148
}
@@ -140,6 +151,24 @@ function Set-EnvironmentVariable {
140151
}
141152
Set-Alias -Name 'Set-Env' -Value 'Set-EnvironmentVariable' -Option 'ReadOnly' -Scope 'Local'
142153
Set-Alias -Name 'Set-Environment' -Value 'Set-EnvironmentVariable' -Option 'ReadOnly' -Scope 'Local'
154+
<#
155+
.SYNOPSIS
156+
GitHub Actions (Internal) - Test Environment Variable Name
157+
.DESCRIPTION
158+
Test environment variable name whether is valid.
159+
.PARAMETER InputObject
160+
Environment variable name that need to test.
161+
.OUTPUTS
162+
Boolean
163+
#>
164+
function Test-EnvironmentVariableName {
165+
[CmdletBinding()]
166+
[OutputType([Boolean])]
167+
Param (
168+
[Parameter(Mandatory = $true, Position = 0)][Alias('Input', 'Object')][String]$InputObject
169+
)
170+
return ($InputObject -imatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -and $InputObject -inotmatch '^(?:CI|PATH)$' -and $InputObject -inotmatch '^(?:ACTIONS|GITHUB|RUNNER)_')
171+
}
143172
Export-ModuleMember -Function @(
144173
'Add-PATH',
145174
'Set-EnvironmentVariable'

hugoalh.GitHubActionsToolkit/module/log.psm1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Void
2626
function Enter-LogGroup {
2727
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_enter-githubactionsloggroup#Enter-GitHubActionsLogGroup')]
2828
[OutputType([Void])]
29-
param (
29+
Param (
3030
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header', 'Message')][String]$Title
3131
)
3232
return Write-GitHubActionsCommand -Command 'group' -Value $Title
@@ -43,7 +43,7 @@ Void
4343
function Exit-LogGroup {
4444
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_exit-githubactionsloggroup#Exit-GitHubActionsLogGroup')]
4545
[OutputType([Void])]
46-
param ()
46+
Param ()
4747
return Write-GitHubActionsCommand -Command 'endgroup'
4848
}
4949
Set-Alias -Name 'Exit-Group' -Value 'Exit-LogGroup' -Option 'ReadOnly' -Scope 'Local'
@@ -74,7 +74,7 @@ Void
7474
function Write-Annotation {
7575
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionsannotation#Write-GitHubActionsAnnotation')]
7676
[OutputType([Void])]
77-
param (
77+
Param (
7878
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)][GitHubActionsAnnotationType]$Type,
7979
[Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)][Alias('Content')][String]$Message,
8080
[Parameter(ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File,
@@ -139,7 +139,7 @@ Void
139139
function Write-Debug {
140140
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionsdebug#Write-GitHubActionsDebug')]
141141
[OutputType([Void])]
142-
param (
142+
Param (
143143
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Content')][String]$Message
144144
)
145145
begin {}
@@ -175,7 +175,7 @@ Void
175175
function Write-Error {
176176
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionserror#Write-GitHubActionsError')]
177177
[OutputType([Void])]
178-
param (
178+
Param (
179179
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)][Alias('Content')][String]$Message,
180180
[Parameter(ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File,
181181
[Parameter(ValueFromPipelineByPropertyName = $true)][Alias('LineStart', 'StartLine')][UInt32]$Line,
@@ -217,7 +217,7 @@ Void
217217
function Write-Fail {
218218
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionsfail#Write-GitHubActionsFail')]
219219
[OutputType([Void])]
220-
param (
220+
Param (
221221
[Parameter(Mandatory = $true, Position = 0)][Alias('Content')][String]$Message,
222222
[ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File,
223223
[Alias('LineStart', 'StartLine')][UInt32]$Line,
@@ -254,7 +254,7 @@ Void
254254
function Write-Notice {
255255
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionsnotice#Write-GitHubActionsNotice')]
256256
[OutputType([Void])]
257-
param (
257+
Param (
258258
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)][Alias('Content')][String]$Message,
259259
[Parameter(ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File,
260260
[Parameter(ValueFromPipelineByPropertyName = $true)][Alias('LineStart', 'StartLine')][UInt32]$Line,
@@ -297,7 +297,7 @@ Void
297297
function Write-Warning {
298298
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionswarning#Write-GitHubActionsWarning')]
299299
[OutputType([Void])]
300-
param (
300+
Param (
301301
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)][Alias('Content')][String]$Message,
302302
[Parameter(ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File,
303303
[Parameter(ValueFromPipelineByPropertyName = $true)][Alias('LineStart', 'StartLine')][UInt32]$Line,

hugoalh.GitHubActionsToolkit/module/open-id-connect.psm1

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#Requires -PSEdition Core
22
#Requires -Version 7.2
33
Import-Module -Name @(
4+
(Join-Path -Path $PSScriptRoot -ChildPath 'log.psm1'),
45
(Join-Path -Path $PSScriptRoot -ChildPath 'utility.psm1')
56
) -Prefix 'GitHubActions' -Scope 'Local'
67
<#
@@ -16,28 +17,21 @@ String
1617
function Get-OpenIdConnectToken {
1718
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_get-githubactionsopenidconnecttoken#Get-GitHubActionsOpenIdConnectToken')]
1819
[OutputType([String])]
19-
param (
20+
Param (
2021
[Parameter(Position = 0)][String]$Audience
2122
)
22-
[String]$OidcTokenRequestToken = $env:ACTIONS_ID_TOKEN_REQUEST_TOKEN
23-
[String]$OidcTokenRequestURL = $env:ACTIONS_ID_TOKEN_REQUEST_URL
24-
if ($OidcTokenRequestToken.Length -ieq 0) {
25-
return Write-Error -Message 'Unable to get GitHub Actions OIDC token request token!' -Category 'ResourceUnavailable'
26-
}
27-
Add-GitHubActionsSecretMask -Value $OidcTokenRequestToken
28-
if ($OidcTokenRequestURL.Length -ieq 0) {
29-
return Write-Error -Message 'Unable to get GitHub Actions OIDC token request URL!' -Category 'ResourceUnavailable'
23+
if (!(Test-GitHubActionsEnvironment -OpenIDConnect)) {
24+
return Write-Error -Message 'Unable to get GitHub Actions OpenID Connect (OIDC) resources!' -Category 'ResourceUnavailable'
3025
}
26+
[String]$RequestToken = $env:ACTIONS_ID_TOKEN_REQUEST_TOKEN
27+
[String]$RequestUri = $env:ACTIONS_ID_TOKEN_REQUEST_URL
28+
Add-GitHubActionsSecretMask -Value $RequestToken
3129
if ($Audience.Length -igt 0) {
32-
Add-GitHubActionsSecretMask -Value $Audience
33-
[String]$AudienceEncode = [System.Web.HttpUtility]::UrlEncode($Audience)
34-
Add-GitHubActionsSecretMask -Value $AudienceEncode
35-
$OidcTokenRequestURL += "&audience=$AudienceEncode"
30+
$RequestUri += "&audience=$([System.Web.HttpUtility]::UrlEncode($Audience))"
3631
}
32+
Write-GitHubActionsDebug -Message "OpenID Connect Token Request URI: $RequestUri"
3733
try {
38-
[PSCustomObject]$Response = Invoke-WebRequest -Uri $OidcTokenRequestURL -UseBasicParsing -UserAgent 'actions/oidc-client' -Headers @{
39-
Authorization = "Bearer $OidcTokenRequestToken"
40-
} -MaximumRedirection 1 -MaximumRetryCount 10 -RetryIntervalSec 10 -Method 'Get'
34+
[PSCustomObject]$Response = Invoke-WebRequest -Uri $RequestUri -UseBasicParsing -UserAgent 'actions/oidc-client' -Headers @{ Authorization = "Bearer $RequestToken" } -MaximumRedirection 1 -MaximumRetryCount 10 -RetryIntervalSec 10 -Method 'Get'
4135
[ValidateNotNullOrEmpty()][String]$OidcToken = (ConvertFrom-Json -InputObject $Response.Content -Depth 100).value
4236
Add-GitHubActionsSecretMask -Value $OidcToken
4337
return $OidcToken

0 commit comments

Comments
 (0)