Skip to content

Commit 4783ba1

Browse files
🩹 [Patch]: Add logging workflow commands (#113)
## Description - Add `Start-LogGroup`, `Stop-LogGroup` and `Set-LogGroup` functions. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 31837b8 commit 4783ba1

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Set-LogGroup {
2+
<#
3+
.SYNOPSIS
4+
Encapsulates commands with a log group in GitHub Actions
5+
6+
.DESCRIPTION
7+
DSL approach for GitHub Action commands.
8+
Allows for colapsing of code in IDE for code that belong together.
9+
10+
.EXAMPLE
11+
Set-LogGroup -Name 'MyGroup' -ScriptBlock {
12+
Write-Host 'Hello, World!'
13+
}
14+
15+
Creates a new log group named 'MyGroup' and writes 'Hello, World!' to the output.
16+
17+
.EXAMPLE
18+
LogGroup 'MyGroup' {
19+
Write-Host 'Hello, World!'
20+
}
21+
22+
Uses the alias 'LogGroup' to create a new log group named 'MyGroup' and writes 'Hello, World!' to the output.
23+
24+
.NOTES
25+
[GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines)
26+
#>
27+
[Alias('LogGroup')]
28+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
29+
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
30+
Justification = 'Does not change state'
31+
)]
32+
[CmdletBinding()]
33+
param(
34+
# The name of the log group
35+
[Parameter(Mandatory)]
36+
[string] $Name,
37+
38+
# The script block to execute
39+
[Parameter(Mandatory)]
40+
[scriptblock] $ScriptBlock
41+
)
42+
43+
Start-LogGroup -Name $Name
44+
. $ScriptBlock
45+
Stop-LogGroup
46+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function Start-LogGroup {
2+
<#
3+
.SYNOPSIS
4+
Starts a log group in GitHub Actions
5+
6+
.EXAMPLE
7+
New-LogGroup 'MyGroup'
8+
9+
Starts a new log group named 'MyGroup'
10+
11+
.NOTES
12+
[GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines)
13+
#>
14+
[Alias('New-LogGroup')]
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
16+
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
17+
Justification = 'Does not change state'
18+
)]
19+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
20+
'PSAvoidUsingWriteHost', '', Scope = 'Function',
21+
Justification = 'Intended for logging in Github Runners which does support Write-Host'
22+
)]
23+
[CmdletBinding()]
24+
param(
25+
# The name of the log group
26+
[Parameter(Mandatory)]
27+
[string] $Name
28+
)
29+
30+
Write-Host "::group::$Name"
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function Stop-LogGroup {
2+
<#
3+
.SYNOPSIS
4+
Stops the current log group in GitHub Actions
5+
6+
.EXAMPLE
7+
Stop-LogGroup
8+
9+
Starts a new log group named 'MyGroup'
10+
11+
.NOTES
12+
[GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines)
13+
#>
14+
[Alias('End-LogGroup')]
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
16+
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
17+
Justification = 'Does not change state'
18+
)]
19+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
20+
'PSAvoidUsingWriteHost', '', Scope = 'Function',
21+
Justification = 'Intended for logging in Github Runners'
22+
)]
23+
[CmdletBinding()]
24+
param()
25+
26+
Write-Host '::endgroup::'
27+
}

tests/Commands.Tests.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[CmdletBinding()]
2+
Param(
3+
# Path to the module to test.
4+
[Parameter()]
5+
[string] $Path
6+
)
7+
8+
Write-Verbose "Path to the module: [$Path]" -Verbose
9+
10+
Describe 'Commands' {
11+
It "Start-LogGroup 'MyGroup' should not throw" {
12+
{
13+
Start-LogGroup 'MyGroup'
14+
} | Should -Not -Throw
15+
}
16+
17+
It 'Stop-LogGroup should not throw' {
18+
{
19+
Stop-LogGroup
20+
} | Should -Not -Throw
21+
}
22+
23+
It "LogGroup 'MyGroup' should not throw" {
24+
{
25+
LogGroup 'MyGroup' {
26+
Get-ChildItem env: | Select-Object Name, Value | Format-Table -AutoSize
27+
}
28+
} | Should -Not -Throw
29+
}
30+
}
31+
32+
33+

0 commit comments

Comments
 (0)