module/utility.psm1

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
#Requires -PSEdition Core -Version 7.2
Import-Module -Name (
    @(
        'command-base'
    ) |
        ForEach-Object -Process { Join-Path -Path $PSScriptRoot -ChildPath "$_.psm1" }
) -Prefix 'GitHubActions' -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Add Secret Mask
.DESCRIPTION
Make a secret get masked from the log.
.PARAMETER Value
A secret that need to get masked from the log.
.PARAMETER WithChunks
Whether to split a secret into chunks to well make a secret get masked from the log.
.OUTPUTS
[Void]
#>

Function Add-SecretMask {
    [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_addgithubactionssecretmask')]
    [OutputType([Void])]
    Param (
        [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][AllowEmptyString()][AllowNull()][Alias('Key', 'Secret', 'Token')][String]$Value,
        [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Advance', 'Advanced', 'Chunk', 'Chunks', 'WithChunk')][Switch]$WithChunks
    )
    Process {
        If ($Value.Length -igt 0) {
            Write-GitHubActionsStdOutCommand -StdOutCommand 'add-mask' -Value $Value
            If ($WithChunks.IsPresent) {
                $Value -isplit '[\b\n\r\s\t -/:-@\[-`{-~]+' |
                    Where-Object -FilterScript { $_.Length -ige 4 -and $_ -ine $Value } |
                    ForEach-Object -Process { Write-GitHubActionsStdOutCommand -StdOutCommand 'add-mask' -Value $_ }
            }
        }
    }
}
Set-Alias -Name 'Add-Mask' -Value 'Add-SecretMask' -Option 'ReadOnly' -Scope 'Local'
Set-Alias -Name 'Add-Secret' -Value 'Add-SecretMask' -Option 'ReadOnly' -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Get Debug Status
.DESCRIPTION
Get the debug status of the runner.
.OUTPUTS
[Boolean] Debug status.
#>

Function Get-IsDebug {
    [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_getgithubactionsisdebug')]
    [OutputType([Boolean])]
    Param ()
    (
        $Env:RUNNER_DEBUG -ieq '1' -or
        $Env:RUNNER_DEBUG -ieq 'true'
    ) |
        Write-Output
}
<#
.SYNOPSIS
GitHub Actions - Get Webhook Event Payload
.DESCRIPTION
Get the complete webhook event payload.
.PARAMETER AsHashtable
Whether to output as hashtable instead of object.
.OUTPUTS
[Hashtable] Webhook event payload as hashtable.
[PSCustomObject] Webhook event payload as object.
#>

Function Get-WebhookEventPayload {
    [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_getgithubactionswebhookeventpayload')]
    [OutputType(([Hashtable], [PSCustomObject]))]
    Param (
        [Alias('ToHashtable')][Switch]$AsHashtable
    )
    If (!([System.IO.Path]::IsPathFullyQualified($Env:GITHUB_EVENT_PATH) -and (Test-Path -LiteralPath $Env:GITHUB_EVENT_PATH -PathType 'Leaf'))) {
        Write-Error -Message 'Unable to get the GitHub Actions webhook event payload: Environment path `GITHUB_EVENT_PATH` is not defined or not contain a valid file path, or file is not exist!' -Category 'ResourceUnavailable'
        Return
    }
    Get-Content -LiteralPath $Env:GITHUB_EVENT_PATH -Raw -Encoding 'UTF8NoBOM' |
        ConvertFrom-Json -AsHashtable:$AsHashtable.IsPresent -Depth 100 -NoEnumerate |
        Write-Output
}
Set-Alias -Name 'Get-Event' -Value 'Get-WebhookEventPayload' -Option 'ReadOnly' -Scope 'Local'
Set-Alias -Name 'Get-Payload' -Value 'Get-WebhookEventPayload' -Option 'ReadOnly' -Scope 'Local'
Set-Alias -Name 'Get-WebhookEvent' -Value 'Get-WebhookEventPayload' -Option 'ReadOnly' -Scope 'Local'
Set-Alias -Name 'Get-WebhookPayload' -Value 'Get-WebhookEventPayload' -Option 'ReadOnly' -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Get Workflow Run URI
.DESCRIPTION
Get the URI of the workflow run.
.OUTPUTS
[String] URI of the workflow run.
#>

Function Get-WorkflowRunUri {
    [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_getgithubactionsworkflowrunuri')]
    [OutputType([String])]
    Param ()
    ForEach ($EnvironmentPath In @('GITHUB_SERVER_URL', 'GITHUB_REPOSITORY', 'GITHUB_RUN_ID')) {
        If ([String]::IsNullOrEmpty((Get-Content -LiteralPath "Env:\$EnvironmentPath" -ErrorAction 'SilentlyContinue'))) {
            Write-Error -Message "Unable to get the GitHub Actions workflow run URI: Environment path ``$EnvironmentPath`` is not defined!" -Category 'ResourceUnavailable'
            Return
        }
    }
    Write-Output -InputObject "$Env:GITHUB_SERVER_URL/$Env:GITHUB_REPOSITORY/actions/runs/$Env:GITHUB_RUN_ID"
}
Set-Alias -Name 'Get-WorkflowRunUrl' -Value 'Get-WorkflowRunUri' -Option 'ReadOnly' -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Test Environment
.DESCRIPTION
Test the current process whether is executing inside the GitHub Actions environment.
.PARAMETER Artifact
Also test whether has artifact resources.
.PARAMETER Cache
Also test whether has cache resources.
.PARAMETER OpenIdConnect
Also test whether has OpenID Connect (OIDC) resources.
.PARAMETER ToolCache
Also test whether has tool cache resources.
.PARAMETER Mandatory
Whether the requirement is mandatory; If mandatory but not fulfill, will throw an error.
.PARAMETER MandatoryMessage
Message when the requirement is mandatory but not fulfill.
.OUTPUTS
[Boolean] Test result when the requirement is not mandatory.
[Void] Nothing when the requirement is mandatory.
#>

Function Test-Environment {
    [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_testgithubactionsenvironment')]
    [OutputType(([Boolean], [Void]))]
    Param (
        [Switch]$Artifact,
        [Switch]$Cache,
        [Alias('Oidc')][Switch]$OpenIdConnect,
        [Switch]$ToolCache,
        [Alias('Require', 'Required')][Switch]$Mandatory,
        [Alias('RequiredMessage', 'RequireMessage')][String]$MandatoryMessage = 'This process requires to invoke inside the GitHub Actions environment!',
        [Switch]$StepSummary# Deprecated, keep as legacy.
    )
    [Hashtable[]]$Conditions = @(
        @{ NeedTest = $True; Name = 'CI'; ExpectValue = 'true' },
        @{ NeedTest = $True; Name = 'GITHUB_ACTION'; },
        @{ NeedTest = $True; Name = 'GITHUB_ACTIONS'; ExpectValue = 'true' },
        @{ NeedTest = $True; Name = 'GITHUB_ACTOR'; },
        @{ NeedTest = $True; Name = 'GITHUB_ACTOR_ID'; },
        @{ NeedTest = $True; Name = 'GITHUB_API_URL'; },
        @{ NeedTest = $True; Name = 'GITHUB_ENV'; },
        @{ NeedTest = $True; Name = 'GITHUB_EVENT_NAME'; },
        @{ NeedTest = $True; Name = 'GITHUB_EVENT_PATH'; },
        @{ NeedTest = $True; Name = 'GITHUB_GRAPHQL_URL'; },
        @{ NeedTest = $True; Name = 'GITHUB_JOB'; },
        @{ NeedTest = $True; Name = 'GITHUB_PATH'; },
        @{ NeedTest = $True; Name = 'GITHUB_REF_NAME'; },
        @{ NeedTest = $True; Name = 'GITHUB_REF_PROTECTED'; },
        @{ NeedTest = $True; Name = 'GITHUB_REF_TYPE'; },
        @{ NeedTest = $True; Name = 'GITHUB_REPOSITORY'; },
        @{ NeedTest = $True; Name = 'GITHUB_REPOSITORY_ID'; },
        @{ NeedTest = $True; Name = 'GITHUB_REPOSITORY_OWNER'; },
        @{ NeedTest = $True; Name = 'GITHUB_REPOSITORY_OWNER_ID'; },
        @{ NeedTest = $True; Name = 'GITHUB_RETENTION_DAYS'; },
        @{ NeedTest = $True; Name = 'GITHUB_RUN_ATTEMPT'; },
        @{ NeedTest = $True; Name = 'GITHUB_RUN_ID'; },
        @{ NeedTest = $True; Name = 'GITHUB_RUN_NUMBER'; },
        @{ NeedTest = $True; Name = 'GITHUB_SERVER_URL'; },
        @{ NeedTest = $True; Name = 'GITHUB_SHA'; },
        @{ NeedTest = $True; Name = 'GITHUB_STEP_SUMMARY'; },
        @{ NeedTest = $True; Name = 'GITHUB_WORKFLOW'; },
        @{ NeedTest = $True; Name = 'GITHUB_WORKFLOW_REF'; },
        @{ NeedTest = $True; Name = 'GITHUB_WORKFLOW_SHA'; },
        @{ NeedTest = $True; Name = 'GITHUB_WORKSPACE'; },
        @{ NeedTest = $True; Name = 'RUNNER_ARCH'; },
        @{ NeedTest = $True; Name = 'RUNNER_NAME'; },
        @{ NeedTest = $True; Name = 'RUNNER_OS'; },
        @{ NeedTest = $True; Name = 'RUNNER_TEMP'; },
        @{ NeedTest = $True; Name = 'RUNNER_TOOL_CACHE'; },
        @{ NeedTest = $Artifact.IsPresent -or $Cache.IsPresent; Name = 'ACTIONS_RUNTIME_TOKEN'; },
        @{ NeedTest = $Artifact.IsPresent; Name = 'ACTIONS_RUNTIME_URL'; },
        @{ NeedTest = $Cache.IsPresent; Name = 'ACTIONS_CACHE_URL'; },
        @{ NeedTest = $OpenIdConnect.IsPresent; Name = 'ACTIONS_ID_TOKEN_REQUEST_TOKEN'; },
        @{ NeedTest = $OpenIdConnect.IsPresent; Name = 'ACTIONS_ID_TOKEN_REQUEST_URL'; }
    )
    [Boolean]$Failed = $False
    ForEach ($Condition In $Conditions) {
        If ($Condition.NeedTest) {
            If ($Null -ieq $Condition.ExpectValue) {
                If ([String]::IsNullOrEmpty((Get-Content -LiteralPath "Env:\$($Condition.Name)" -ErrorAction 'SilentlyContinue'))) {
                    $Failed = $True
                    Write-Warning -Message "Unable to get the GitHub Actions resources: Environment path ``$($Condition.Name)`` is not defined!"
                }
            }
            Else {
                If ((Get-Content -LiteralPath "Env:\$($Condition.Name)" -ErrorAction 'SilentlyContinue') -ine $Condition.ExpectValue) {
                    $Failed = $True
                    Write-Warning -Message "Unable to get the GitHub Actions resources: Environment path ``$($Condition.Name)`` is not defined or not equal to expect value!"
                }
            }
        }
    }
    If ($Failed) {
        If ($Mandatory.IsPresent) {
            Write-Error -Message $MandatoryMessage -Category 'InvalidOperation' -ErrorAction 'Stop'
        }
        Write-Output -InputObject $False
        Return
    }
    If (!$Mandatory.IsPresent) {
        Write-Output -InputObject $True
    }
}
Export-ModuleMember -Function @(
    'Add-SecretMask',
    'Get-IsDebug',
    'Get-WebhookEventPayload',
    'Get-WorkflowRunUri',
    'Test-Environment'
) -Alias @(
    'Add-Mask',
    'Add-Secret',
    'Get-Event',
    'Get-Payload',
    'Get-WebhookEvent',
    'Get-WebhookPayload',
    'Get-WorkflowRunUrl'
)