From 37fb69375f6b67828785d57e2ed4aefba92f1ae2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:23:05 +0200 Subject: [PATCH 1/9] Add logging workflow commands --- src/public/Commands/Set-LogGroup.ps1 | 44 ++++++++++++++++++++++++++ src/public/Commands/Start-LogGroup.ps1 | 30 ++++++++++++++++++ src/public/Commands/Stop-LogGroup.ps1 | 27 ++++++++++++++++ tests/Commands.Tests.ps1 | 33 +++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 src/public/Commands/Set-LogGroup.ps1 create mode 100644 src/public/Commands/Start-LogGroup.ps1 create mode 100644 src/public/Commands/Stop-LogGroup.ps1 create mode 100644 tests/Commands.Tests.ps1 diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 new file mode 100644 index 000000000..ecb844a6f --- /dev/null +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -0,0 +1,44 @@ +function Set-LogGroup { + <# + .SYNOPSIS + Encapsulates commands with a log group in GitHub Actions + + .DESCRIPTION + DSL approach for GitHub Action commands. + Allows for colapsing of code in IDE for code that belong together. + + .EXAMPLE + Set-LogGroup -Name 'MyGroup' -ScriptBlock { + Write-Host 'Hello, World!' + } + + Creates a new log group named 'MyGroup' and writes 'Hello, World!' to the output. + + .EXAMPLE + LogGroup 'MyGroup' { + Write-Host 'Hello, World!' + } + + Uses the alias 'LogGroup' to create a new log group named 'MyGroup' and writes 'Hello, World!' to the output. + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('LogGroup')] + [Alias('Group')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string] $Name, + [Parameter(Mandatory)] + [scriptblock] $ScriptBlock + ) + + Start-LogGroup $Name + Write-Output $ScriptBlock.Invoke() + Stop-LogGroup +} diff --git a/src/public/Commands/Start-LogGroup.ps1 b/src/public/Commands/Start-LogGroup.ps1 new file mode 100644 index 000000000..b14721406 --- /dev/null +++ b/src/public/Commands/Start-LogGroup.ps1 @@ -0,0 +1,30 @@ +function Start-LogGroup { + <# + .SYNOPSIS + Starts a log group in GitHub Actions + + .EXAMPLE + New-LogGroup 'MyGroup' + + Starts a new log group named 'MyGroup' + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('New-LogGroup')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingWriteHost', '', Scope = 'Function', + Justification = 'Intended for logging in Github Runners which does support Write-Host' + )] + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string] $Name + ) + + Write-Host "::group::$Name" +} diff --git a/src/public/Commands/Stop-LogGroup.ps1 b/src/public/Commands/Stop-LogGroup.ps1 new file mode 100644 index 000000000..4bb022070 --- /dev/null +++ b/src/public/Commands/Stop-LogGroup.ps1 @@ -0,0 +1,27 @@ +function Stop-LogGroup { + <# + .SYNOPSIS + Stops the current log group in GitHub Actions + + .EXAMPLE + Stop-LogGroup + + Starts a new log group named 'MyGroup' + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('End-LogGroup')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingWriteHost', '', Scope = 'Function', + Justification = 'Intended for logging in Github Runners' + )] + [CmdletBinding()] + param() + + Write-Host '::endgroup::' +} diff --git a/tests/Commands.Tests.ps1 b/tests/Commands.Tests.ps1 new file mode 100644 index 000000000..7bcc43cc1 --- /dev/null +++ b/tests/Commands.Tests.ps1 @@ -0,0 +1,33 @@ +[CmdletBinding()] +Param( + # Path to the module to test. + [Parameter()] + [string] $Path +) + +Write-Verbose "Path to the module: [$Path]" -Verbose + +Describe 'Commands' { + It "Start-LogGroup 'MyGroup' should not throw" { + { + Start-LogGroup 'MyGroup' + } | Should -Not -Throw + } + + It 'Stop-LogGroup should not throw' { + { + Stop-LogGroup + } | Should -Not -Throw + } + + It "LogGroup 'MyGroup' should not throw" { + { + LogGroup 'MyGroup' { + Write-Host 'Hello, World!' + } + } | Should -Not -Throw + } +} + + + From 55cd4b8167ee461cb0a8bffd0f572aebdc498c5e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:28:42 +0200 Subject: [PATCH 2/9] Remove Group as alias --- src/public/Commands/Set-LogGroup.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index ecb844a6f..f7f5de8eb 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -25,7 +25,6 @@ [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) #> [Alias('LogGroup')] - [Alias('Group')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', Justification = 'Does not change state' From 8c8281f774e6eb9eece181283c37d6141a480073 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:31:08 +0200 Subject: [PATCH 3/9] Fixed --- src/public/Commands/Set-LogGroup.ps1 | 3 +++ src/public/Commands/Start-LogGroup.ps1 | 1 + 2 files changed, 4 insertions(+) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index f7f5de8eb..a5b861876 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -31,8 +31,11 @@ )] [CmdletBinding()] param( + # The name of the log group [Parameter(Mandatory)] [string] $Name, + + # The script block to execute [Parameter(Mandatory)] [scriptblock] $ScriptBlock ) diff --git a/src/public/Commands/Start-LogGroup.ps1 b/src/public/Commands/Start-LogGroup.ps1 index b14721406..96ba0fe74 100644 --- a/src/public/Commands/Start-LogGroup.ps1 +++ b/src/public/Commands/Start-LogGroup.ps1 @@ -22,6 +22,7 @@ )] [CmdletBinding()] param( + # The name of the log group [Parameter(Mandatory)] [string] $Name ) From 65dcede90f51e8aae16519c96339d6ab78440858 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:43:40 +0200 Subject: [PATCH 4/9] test --- tests/Commands.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Commands.Tests.ps1 b/tests/Commands.Tests.ps1 index 7bcc43cc1..36d733518 100644 --- a/tests/Commands.Tests.ps1 +++ b/tests/Commands.Tests.ps1 @@ -23,7 +23,7 @@ Describe 'Commands' { It "LogGroup 'MyGroup' should not throw" { { LogGroup 'MyGroup' { - Write-Host 'Hello, World!' + Get-GitHubRepository } } | Should -Not -Throw } From 60b129aac35a24e02a497be5ddf952f184062ff4 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:58:29 +0200 Subject: [PATCH 5/9] Use invoke command --- src/public/Commands/Set-LogGroup.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index a5b861876..132491aba 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -41,6 +41,10 @@ ) Start-LogGroup $Name - Write-Output $ScriptBlock.Invoke() + Invoke-Command -ScriptBlock $ScriptBlock Stop-LogGroup } + +LogGroup 'MyGroup' { + Get-Process +} From 283100764f6fc2c1ced29d1f9d739fd973ecb385 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:58:46 +0200 Subject: [PATCH 6/9] Add name parameter --- src/public/Commands/Set-LogGroup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index 132491aba..43bedb47e 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -40,7 +40,7 @@ [scriptblock] $ScriptBlock ) - Start-LogGroup $Name + Start-LogGroup -Name $Name Invoke-Command -ScriptBlock $ScriptBlock Stop-LogGroup } From 35f49c96768d7a44917fc4cc34b363aac5143e20 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 06:59:20 +0200 Subject: [PATCH 7/9] cleanup --- src/public/Commands/Set-LogGroup.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index 43bedb47e..5620edbfe 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -44,7 +44,3 @@ Invoke-Command -ScriptBlock $ScriptBlock Stop-LogGroup } - -LogGroup 'MyGroup' { - Get-Process -} From 3594608aa396bd9987770f19dd53006ba537b2c9 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 07:22:27 +0200 Subject: [PATCH 8/9] Test get-process --- tests/Commands.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Commands.Tests.ps1 b/tests/Commands.Tests.ps1 index 36d733518..3f77dfa7d 100644 --- a/tests/Commands.Tests.ps1 +++ b/tests/Commands.Tests.ps1 @@ -23,7 +23,7 @@ Describe 'Commands' { It "LogGroup 'MyGroup' should not throw" { { LogGroup 'MyGroup' { - Get-GitHubRepository + Get-ChildItem env: | Select-Object Name, Value | Format-Table -AutoSize } } | Should -Not -Throw } From c9553b2b4ea896e8a163b65f7b371afc0f9f9b6d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 8 Aug 2024 08:08:43 +0200 Subject: [PATCH 9/9] dot --- src/public/Commands/Set-LogGroup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 index 5620edbfe..feb266b4a 100644 --- a/src/public/Commands/Set-LogGroup.ps1 +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -41,6 +41,6 @@ ) Start-LogGroup -Name $Name - Invoke-Command -ScriptBlock $ScriptBlock + . $ScriptBlock Stop-LogGroup }