hugoalh.GitHubActionsToolkit.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#Requires -PSEdition Core
#Requires -Version 7.2
enum GHActionsAnnotationType {
    Notice = 0
    N = 0
    Note = 0
    Warning = 1
    W = 1
    Warn = 1
    Error = 2
    E = 2
}
<#
.SYNOPSIS
GitHub Actions - Internal - Format Command
.DESCRIPTION
An internal function to escape command characters that could cause issues.
.PARAMETER InputObject
String that need to escape command characters.
.PARAMETER Property
Also escape command property characters.
.OUTPUTS
String
#>

function Format-GHActionsCommand {
    [CmdletBinding()][OutputType([string])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][AllowEmptyString()][Alias('Input', 'Object')][string]$InputObject,
        [switch]$Property
    )
    begin {}
    process {
        [string]$Result = $InputObject -replace '%', '%25' -replace "\n", '%0A' -replace "\r", '%0D'
        if ($Property) {
            $Result = $Result -replace ',', '%2C' -replace ':', '%3A'
        }
        return $Result
    }
    end {}
}
<#
.SYNOPSIS
GitHub Actions - Internal - Write Workflow Command
.DESCRIPTION
An internal function to write workflow command.
.PARAMETER Command
Workflow command.
.PARAMETER Message
Message.
.PARAMETER Property
Workflow command property.
.OUTPUTS
Void
#>

function Write-GHActionsCommand {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Command,
        [Parameter(Mandatory = $true, Position = 1)][AllowEmptyString()][string]$Message,
        [Parameter(Position = 2)][Alias('Properties')][hashtable]$Property = @{}
    )
    [string]$Result = "::$Command"
    if ($Property.Count -gt 0) {
        $Result += " $($($Property.GetEnumerator() | ForEach-Object -Process {
            return "$($_.Name)=$(Format-GHActionsCommand -InputObject $_.Value -Property)"
        }) -join ',')"

    }
    $Result += "::$(Format-GHActionsCommand -InputObject $Message)"
    Write-Host -Object $Result
}
<#
.SYNOPSIS
GitHub Actions - Add Environment Variable
.DESCRIPTION
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.
.PARAMETER InputObject
Environment variable.
.PARAMETER Name
Environment variable name.
.PARAMETER Value
Environment variable value.
.OUTPUTS
Void
#>

function Add-GHActionsEnvironmentVariable {
    [CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^[\da-z_]+$')][Alias('Key')][string]$Name,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][ValidatePattern('^.+$')][string]$Value
    )
    begin {
        [hashtable]$Result = @{}
    }
    process {
        switch ($PSCmdlet.ParameterSetName) {
            '1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
                if ($_.Name.GetType().Name -ne 'string') {
                    Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
                } elseif ($_.Name -notmatch '^[\da-z_]+$') {
                    Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
                } elseif ($_.Value.GetType().Name -ne 'string') {
                    Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
                } elseif ($_.Value -notmatch '^.+$') {
                    Write-Error -Message "Input value `"$($_.Value)`" is not match the require pattern!" -Category SyntaxError
                } else {
                    $Result[$_.Name] = $_.Value
                }
            }; break }
            '2' { $Result[$Name] = $Value; break }
        }
    }
    end {
        Add-Content -Path $env:GITHUB_ENV -Value "$($($Result.GetEnumerator() | ForEach-Object -Process {
            return "$($_.Name)=$($_.Value)"
        }) -join "`n")"
 -Encoding utf8NoBOM
    }
}
Set-Alias -Name 'Add-GHActionsEnv' -Value 'Add-GHActionsEnvironmentVariable' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Add PATH
.DESCRIPTION
Add directory to the system `PATH` variable and automatically makes it available to all subsequent actions in the current job; The currently running action cannot access the updated path variable.
.PARAMETER Path
System path.
.OUTPUTS
Void
#>

function Add-GHActionsPATH {
    [CmdletBinding()][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Alias('Paths')][string[]]$Path
    )
    begin {
        [string[]]$Result = @()
    }
    process {
        $Path | ForEach-Object -Process {
            if (Test-Path -Path $_ -IsValid) {
                $Result += $_
            } else {
                Write-Error -Message "Input `"$_`" is not match the require path pattern!" -Category SyntaxError
            }
        }
    }
    end {
        Add-Content -Path $env:GITHUB_PATH -Value "$($Result -join "`n")" -Encoding utf8NoBOM
    }
}
<#
.SYNOPSIS
GitHub Actions - Add Problem Matcher
.DESCRIPTION
Problem matchers are a way to scan the output of actions for a specified regular expression pattern and automatically surface that information prominently in the user interface, both annotations and log file decorations are created when a match is detected. For more information, please visit https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md.
.PARAMETER Path
Relative path to the JSON file problem matcher.
.OUTPUTS
Void
#>

function Add-GHActionsProblemMatcher {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][SupportsWildcards()][Alias('File', 'Files', 'Paths', 'PSPath', 'PSPaths')][string[]]$Path
    )
    begin {}
    process {
        Resolve-Path -Path $Path -Relative | ForEach-Object -Process {
            if ((Test-Path -Path $_ -PathType Leaf) -and ((Split-Path -Path $_ -Extension) -eq '.json')) {
                Write-GHActionsCommand -Command 'add-matcher' -Message ($_ -replace '^\.\\', '' -replace '\\', '/')
            } else {
                Write-Error -Message "Path `"$_`" is not exist or match the require path pattern!" -Category SyntaxError
            }
        }
    }
    end {}
}
<#
.SYNOPSIS
GitHub Actions - Add Secret Mask
.DESCRIPTION
Make a secret will get masked from the log.
.PARAMETER Value
The secret.
.PARAMETER Smart
Use improved method to well make a secret will get masked from the log.
.OUTPUTS
Void
#>

function Add-GHActionsSecretMask {
    [CmdletBinding()][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Key', 'Token')][string]$Value,
        [switch]$Smart
    )
    begin {}
    process {
        Write-GHActionsCommand -Command 'add-mask' -Message $Value
        if ($Smart) {
            [string[]]$Bin = $Value -split "[\n\r\s\t]+"
            $Bin | ForEach-Object -Process {
                if (($_ -ne $Value) -and ($_.Length -ge 2)) {
                    Write-GHActionsCommand -Command 'add-mask' -Message $_
                }
            }
        }
    }
    end {}
}
Set-Alias -Name 'Add-GHActionsMask' -Value 'Add-GHActionsSecretMask' -Option ReadOnly -Scope 'Local'
Set-Alias -Name 'Add-GHActionsSecret' -Value 'Add-GHActionsSecretMask' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Disable Echo Command
.DESCRIPTION
Disable echoing of workflow commands, the workflow run's log will not show the command itself; A workflow command is echoed if there are any errors processing the command; Secret `ACTIONS_STEP_DEBUG` will ignore this.
.OUTPUTS
Void
#>

function Disable-GHActionsEchoCommand {
    [CmdletBinding()][OutputType([void])]
    param()
    Write-GHActionsCommand -Command 'echo' -Message 'off'
}
Set-Alias -Name 'Disable-GHActionsCommandEcho' -Value 'Disable-GHActionsEchoCommand' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Disable Processing Command
.DESCRIPTION
Stop processing any workflow commands to allow log anything without accidentally running workflow commands.
.PARAMETER EndToken
An end token for function `Enable-GHActionsProcessingCommand`.
.OUTPUTS
String
#>

function Disable-GHActionsProcessingCommand {
    [CmdletBinding()][OutputType([string])]
    param(
        [Parameter(Position = 0)][ValidatePattern('^.+$')][Alias('Key', 'Token', 'Value')][string]$EndToken = (New-Guid).Guid
    )
    Write-GHActionsCommand -Command 'stop-commands' -Message $EndToken
    return $EndToken
}
Set-Alias -Name 'Disable-GHActionsCommandProcessing' -Value 'Disable-GHActionsProcessingCommand' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Enable Echo Command
.DESCRIPTION
Enable echoing of workflow commands, the workflow run's log will show the command itself; The `add-mask`, `debug`, `warning`, and `error` commands do not support echoing because their outputs are already echoed to the log; Secret `ACTIONS_STEP_DEBUG` will ignore this.
.OUTPUTS
Void
#>

function Enable-GHActionsEchoCommand {
    [CmdletBinding()][OutputType([void])]
    param()
    Write-GHActionsCommand -Command 'echo' -Message 'on'
}
Set-Alias -Name 'Enable-GHActionsCommandEcho' -Value 'Enable-GHActionsEchoCommand' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Enable Processing Command
.DESCRIPTION
Resume processing any workflow commands to allow running workflow commands.
.PARAMETER EndToken
An end token from function `Disable-GHActionsProcessingCommand`.
.OUTPUTS
Void
#>

function Enable-GHActionsProcessingCommand {
    [CmdletBinding()][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][Alias('Key', 'Token', 'Value')][string]$EndToken
    )
    Write-GHActionsCommand -Command $EndToken -Message ''
}
Set-Alias -Name 'Enable-GHActionsCommandProcessing' -Value 'Enable-GHActionsProcessingCommand' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Enter Log Group
.DESCRIPTION
Create an expandable group in the log; Anything write to the log between `Enter-GHActionsLogGroup` and `Exit-GHActionsLogGroup` commands are inside an expandable group in the log.
.PARAMETER Title
Title of the log group.
.OUTPUTS
Void
#>

function Enter-GHActionsLogGroup {
    [CmdletBinding()][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][Alias('Header', 'Message')][string]$Title
    )
    Write-GHActionsCommand -Command 'group' -Message $Title
}
Set-Alias -Name 'Enter-GHActionsGroup' -Value 'Enter-GHActionsLogGroup' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Exit Log Group
.DESCRIPTION
End an expandable group in the log.
.OUTPUTS
Void
#>

function Exit-GHActionsLogGroup {
    [CmdletBinding()][OutputType([void])]
    param ()
    Write-GHActionsCommand -Command 'endgroup' -Message ''
}
Set-Alias -Name 'Exit-GHActionsGroup' -Value 'Exit-GHActionsLogGroup' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Get Input
.DESCRIPTION
Get input.
.PARAMETER Name
Name of the input.
.PARAMETER Require
Whether the input is require. If required and not present, will throw an error.
.PARAMETER Trim
Trim the input's value.
.OUTPUTS
Hashtable | String
#>

function Get-GHActionsInput {
    [CmdletBinding()][OutputType([hashtable], [string])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Key', 'Keys', 'Names')][string[]]$Name,
        [Alias('Required')][switch]$Require,
        [switch]$Trim
    )
    begin {
        [hashtable]$Result = @{}
    }
    process {
        $Name | ForEach-Object -Process {
            $InputValue = Get-ChildItem -Path "Env:\INPUT_$($_.ToUpper() -replace '[ \n\r\s\t]+','_')" -ErrorAction SilentlyContinue
            if ($null -eq $InputValue) {
                if ($Require) {
                    throw "Input ``$_`` is not defined!"
                }
                $Result[$_] = $InputValue
            } else {
                if ($Trim) {
                    $Result[$_] = $InputValue.Value.Trim()
                } else {
                    $Result[$_] = $InputValue.Value
                }
            }
        }
    }
    end {
        if ($Result.Count -eq 1) {
            return $Result.Values[0]
        }
        return $Result
    }
}
<#
.SYNOPSIS
GitHub Actions - Get Debug Status
.DESCRIPTION
Get debug status.
.OUTPUTS
Boolean
#>

function Get-GHActionsIsDebug {
    [CmdletBinding()][OutputType([bool])]
    param ()
    if ($env:RUNNER_DEBUG -eq 'true') {
        return $true
    }
    return $false
}
<#
.SYNOPSIS
GitHub Actions - Get State
.DESCRIPTION
Get state.
.PARAMETER Name
Name of the state.
.PARAMETER Trim
Trim the state's value.
.OUTPUTS
Hashtable | String
#>

function Get-GHActionsState {
    [CmdletBinding()][OutputType([hashtable], [string])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Key', 'Keys', 'Names')][string[]]$Name,
        [switch]$Trim
    )
    begin {
        [hashtable]$Result = @{}
    }
    process {
        $Name | ForEach-Object -Process {
            $StateValue = Get-ChildItem -Path "Env:\STATE_$($_.ToUpper() -replace '[ \n\r\s\t]+','_')" -ErrorAction SilentlyContinue
            if ($null -eq $StateValue) {
                $Result[$_] = $StateValue
            } else {
                if ($Trim) {
                    $Result[$_] = $StateValue.Value.Trim()
                } else {
                    $Result[$_] = $StateValue.Value
                }
            }
        }
    }
    end {
        if ($Result.Count -eq 1) {
            return $Result.Values[0]
        }
        return $Result
    }
}
Set-Alias -Name 'Restore-GHActionsState' -Value 'Get-GHActionsState' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Get Webhook Event Payload
.DESCRIPTION
Get the complete webhook event payload.
.PARAMETER AsHashTable
Output as hashtable instead of object.
.OUTPUTS
Hashtable | PSCustomObject
#>

function Get-GHActionsWebhookEventPayload {
    [CmdletBinding()][OutputType([hashtable], [pscustomobject])]
    param (
        [Alias('ToHashTable')][switch]$AsHashTable
    )
    return (Get-Content -Path $env:GITHUB_EVENT_PATH -Raw -Encoding utf8NoBOM | ConvertFrom-Json -AsHashtable:$AsHashTable)
}
Set-Alias -Name 'Get-GHActionsEvent' -Value 'Get-GHActionsWebhookEventPayload' -Option ReadOnly -Scope 'Local'
Set-Alias -Name 'Get-GHActionsPayload' -Value 'Get-GHActionsWebhookEventPayload' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Remove Problem Matcher
.DESCRIPTION
Remove problem matcher that previously added from function `Add-GHActionsProblemMatcher`.
.PARAMETER Owner
Owner of the problem matcher that previously added from function `Add-GHActionsProblemMatcher`.
.OUTPUTS
Void
#>

function Remove-GHActionsProblemMatcher {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Identifies', 'Identify', 'Identifier', 'Identifiers', 'Key', 'Keys', 'Name', 'Names', 'Owners')][string[]]$Owner
    )
    begin {}
    process {
        $Owner | ForEach-Object -Process {
            Write-GHActionsCommand -Command 'remove-matcher' -Message '' -Property @{ 'owner' = $_ }
        }
    }
    end {}
}
<#
.SYNOPSIS
GitHub Actions - Set Output
.DESCRIPTION
Set output.
.PARAMETER Name
Name of the output.
.PARAMETER Value
Value of the output.
.OUTPUTS
Void
#>

function Set-GHActionsOutput {
    [CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][string]$Value
    )
    begin {}
    process {
        switch ($PSCmdlet.ParameterSetName) {
            '1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
                if ($_.Name.GetType().Name -ne 'string') {
                    Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
                } elseif ($_.Name -notmatch '^.+$') {
                    Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
                } elseif ($_.Value.GetType().Name -ne 'string') {
                    Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
                } else {
                    Write-GHActionsCommand -Command 'set-output' -Message $_.Value -Property @{ 'name' = $_.Name }
                }
            }; break }
            '2' { Write-GHActionsCommand -Command 'set-output' -Message $Value -Property @{ 'name' = $Name }; break }
        }
    }
    end {}
}
<#
.SYNOPSIS
GitHub Actions - Set State
.DESCRIPTION
Set state.
.PARAMETER Name
Name of the state.
.PARAMETER Value
Value of the state.
.OUTPUTS
Void
#>

function Set-GHActionsState {
    [CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
        [Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][string]$Value
    )
    begin {}
    process {
        switch ($PSCmdlet.ParameterSetName) {
            '1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
                if ($_.Name.GetType().Name -ne 'string') {
                    Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
                } elseif ($_.Name -notmatch '^.+$') {
                    Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
                } elseif ($_.Value.GetType().Name -ne 'string') {
                    Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
                } else {
                    Write-GHActionsCommand -Command 'save-state' -Message $_.Value -Property @{ 'name' = $_.Name }
                }
            }; break }
            '2' { Write-GHActionsCommand -Command 'save-state' -Message $Value -Property @{ 'name' = $Name }; break }
        }
    }
    end {}
}
Set-Alias -Name 'Save-GHActionsState' -Value 'Set-GHActionsState' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Write Annotation
.DESCRIPTION
Prints an annotation message to the log.
.PARAMETER Type
Annotation type.
.PARAMETER Message
Message that need to log at annotation.
.PARAMETER File
Issue file path.
.PARAMETER Line
Issue file line start.
.PARAMETER Column
Issue file column start.
.PARAMETER EndLine
Issue file line end.
.PARAMETER EndColumn
Issue file column end.
.PARAMETER Title
Issue title.
.OUTPUTS
Void
#>

function Write-GHActionsAnnotation {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0)][GHActionsAnnotationType]$Type,
        [Parameter(Mandatory = $true, Position = 1)][string]$Message,
        [ValidatePattern('^.*$')][Alias('Path')][string]$File,
        [uint]$Line,
        [Alias('Col')][uint]$Column,
        [uint]$EndLine,
        [uint]$EndColumn,
        [ValidatePattern('^.*$')][Alias('Header')][string]$Title
    )
    [string]$TypeRaw = ""
    switch ($Type.GetHashCode()) {
        0 { $TypeRaw = 'notice'; break }
        1 { $TypeRaw = 'warning'; break }
        2 { $TypeRaw = 'error'; break }
    }
    [hashtable]$Property = @{}
    if ($File.Length -gt 0) {
        $Property.'file' = $File
    }
    if ($Line -gt 0) {
        $Property.'line' = $Line
    }
    if ($Column -gt 0) {
        $Property.'col' = $Column
    }
    if ($EndLine -gt 0) {
        $Property.'endLine' = $EndLine
    }
    if ($EndColumn -gt 0) {
        $Property.'endColumn' = $EndColumn
    }
    if ($Title.Length -gt 0) {
        $Property.'title' = $Title
    }
    Write-GHActionsCommand -Command $TypeRaw -Message $Message -Property $Property
}
<#
.SYNOPSIS
GitHub Actions - Write Debug
.DESCRIPTION
Prints a debug message to the log.
.PARAMETER Message
Message that need to log at debug level.
.OUTPUTS
Void
#>

function Write-GHActionsDebug {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string]$Message
    )
    begin {}
    process {
        Write-GHActionsCommand -Command 'debug' -Message $Message
    }
    end {}
}
<#
.SYNOPSIS
GitHub Actions - Write Error
.DESCRIPTION
Prints an error message to the log.
.PARAMETER Message
Message that need to log at error level.
.PARAMETER File
Issue file path.
.PARAMETER Line
Issue file line start.
.PARAMETER Col
Issue file column start.
.PARAMETER EndLine
Issue file line end.
.PARAMETER EndColumn
Issue file column end.
.PARAMETER Title
Issue title.
.OUTPUTS
Void
#>

function Write-GHActionsError {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$Message,
        [ValidatePattern('^.*$')][Alias('Path')][string]$File,
        [uint]$Line,
        [Alias('Col')][uint]$Column,
        [uint]$EndLine,
        [uint]$EndColumn,
        [ValidatePattern('^.*$')][Alias('Header')][string]$Title
    )
    Write-GHActionsAnnotation -Type 'Error' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title
}
<#
.SYNOPSIS
GitHub Actions - Write Fail
.DESCRIPTION
Prints an error message to the log and end the process.
.PARAMETER Message
Message that need to log at error level.
.OUTPUTS
Void
#>

function Write-GHActionsFail {
    [CmdletBinding()][OutputType([void])]
    param(
        [Parameter(Mandatory = $true, Position = 0)][string]$Message
    )
    Write-GHActionsAnnotation -Type 'Error' -Message $Message
    exit 1
}
<#
.SYNOPSIS
GitHub Actions - Write Notice
.DESCRIPTION
Prints a notice message to the log.
.PARAMETER Message
Message that need to log at notice level.
.PARAMETER File
Issue file path.
.PARAMETER Line
Issue file line start.
.PARAMETER Col
Issue file column start.
.PARAMETER EndLine
Issue file line end.
.PARAMETER EndColumn
Issue file column end.
.PARAMETER Title
Issue title.
.OUTPUTS
Void
#>

function Write-GHActionsNotice {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$Message,
        [ValidatePattern('^.*$')][Alias('Path')][string]$File,
        [uint]$Line,
        [Alias('Col')][uint]$Column,
        [uint]$EndLine,
        [uint]$EndColumn,
        [ValidatePattern('^.*$')][Alias('Header')][string]$Title
    )
    Write-GHActionsAnnotation -Type 'Notice' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title
}
Set-Alias -Name 'Write-GHActionsNote' -Value 'Write-GHActionsNotice' -Option ReadOnly -Scope 'Local'
<#
.SYNOPSIS
GitHub Actions - Write Warning
.DESCRIPTION
Prints a warning message to the log.
.PARAMETER Message
Message that need to log at warning level.
.PARAMETER File
Issue file path.
.PARAMETER Line
Issue file line start.
.PARAMETER Col
Issue file column start.
.PARAMETER EndLine
Issue file line end.
.PARAMETER EndColumn
Issue file column end.
.PARAMETER Title
Issue title.
.OUTPUTS
Void
#>

function Write-GHActionsWarning {
    [CmdletBinding()][OutputType([void])]
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$Message,
        [ValidatePattern('^.*$')][Alias('Path')][string]$File,
        [uint]$Line,
        [Alias('Col')][uint]$Column,
        [uint]$EndLine,
        [uint]$EndColumn,
        [ValidatePattern('^.*$')][Alias('Header')][string]$Title
    )
    Write-GHActionsAnnotation -Type 'Warning' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title
}
Set-Alias -Name 'Write-GHActionsWarn' -Value 'Write-GHActionsWarning' -Option ReadOnly -Scope 'Local'
Export-ModuleMember -Function @(
    'Add-GHActionsEnvironmentVariable',
    'Add-GHActionsPATH',
    'Add-GHActionsProblemMatcher',
    'Add-GHActionsSecretMask',
    'Disable-GHActionsEchoCommand',
    'Disable-GHActionsProcessingCommand',
    'Enable-GHActionsEchoCommand',
    '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'
) -Alias @(
    'Add-GHActionsEnv',
    'Add-GHActionsMask',
    'Add-GHActionsSecret',
    'Disable-GHActionsCommandEcho',
    'Disable-GHActionsCommandProcessing',
    'Enable-GHActionsCommandEcho',
    'Enable-GHActionsCommandProcessing',
    'Enter-GHActionsGroup',
    'Exit-GHActionsGroup',
    'Get-GHActionsEvent',
    'Get-GHActionsPayload',
    'Restore-GHActionsState',
    'Save-GHActionsState',
    'Write-GHActionsNote',
    'Write-GHActionsWarn'
)