From 2c9c405ee1b063f5c2e28f0d493ad667afaa2244 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 12:09:33 +0100 Subject: [PATCH 01/11] Simplify logging in GitHub log group commands by removing debug statements --- src/functions/public/Commands/Set-GitHubLogGroup.ps1 | 7 ++----- src/functions/public/Commands/Start-GitHubLogGroup.ps1 | 9 ++------- src/functions/public/Commands/Stop-GitHubLogGroup.ps1 | 9 ++------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/functions/public/Commands/Set-GitHubLogGroup.ps1 b/src/functions/public/Commands/Set-GitHubLogGroup.ps1 index 4e1c48653..70aeca7b8 100644 --- a/src/functions/public/Commands/Set-GitHubLogGroup.ps1 +++ b/src/functions/public/Commands/Set-GitHubLogGroup.ps1 @@ -41,21 +41,18 @@ ) begin { - $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Host "::group::$Name" } process { - Start-GitHubLogGroup -Name $Name try { . $ScriptBlock } catch { throw $_ } - Stop-GitHubLogGroup } end { - Write-Debug "[$stackPath] - End" + Write-Host '::endgroup::' } } diff --git a/src/functions/public/Commands/Start-GitHubLogGroup.ps1 b/src/functions/public/Commands/Start-GitHubLogGroup.ps1 index 00f4f00a8..3aea22d06 100644 --- a/src/functions/public/Commands/Start-GitHubLogGroup.ps1 +++ b/src/functions/public/Commands/Start-GitHubLogGroup.ps1 @@ -27,10 +27,7 @@ [string] $Name ) - begin { - $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" - } + begin {} process { try { @@ -40,7 +37,5 @@ } } - end { - Write-Debug "[$stackPath] - End" - } + end {} } diff --git a/src/functions/public/Commands/Stop-GitHubLogGroup.ps1 b/src/functions/public/Commands/Stop-GitHubLogGroup.ps1 index 2d5b8220c..a753b8989 100644 --- a/src/functions/public/Commands/Stop-GitHubLogGroup.ps1 +++ b/src/functions/public/Commands/Stop-GitHubLogGroup.ps1 @@ -23,10 +23,7 @@ [Alias('Stop-LogGroup')] param() - begin { - $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" - } + begin {} process { try { @@ -36,7 +33,5 @@ } } - end { - Write-Debug "[$stackPath] - End" - } + end {} } From e8485623f3a95701f2f64a2be6df01fdf83f0993 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 12:10:01 +0100 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20debug=20l?= =?UTF-8?q?ogging=20for=20empty=20lines=20in=20Get-GitHubOutput=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Commands/Get-GitHubOutput.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/functions/public/Commands/Get-GitHubOutput.ps1 b/src/functions/public/Commands/Get-GitHubOutput.ps1 index 28d3cf772..bb1065110 100644 --- a/src/functions/public/Commands/Get-GitHubOutput.ps1 +++ b/src/functions/public/Commands/Get-GitHubOutput.ps1 @@ -65,6 +65,7 @@ $content = @() foreach ($line in $outputContent) { if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) { + Write-Debug "[$line] - Empty line" $content += '' continue } @@ -72,7 +73,7 @@ } Write-Debug "[$stackPath] - Output content" Write-Debug ($content | Out-String) - $content | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable + ConvertFrom-GitHubOutput -InputData $content -AsHashtable:$AsHashtable } catch { throw $_ } From d7103fa3ae1ddb1d65eeb8e18452e53a7b6a56f3 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 12:10:08 +0100 Subject: [PATCH 03/11] Enhance GitHubContext format by adding 'Name' property and removing 'ID' property --- src/formats/GitHubContext.Format.ps1xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/formats/GitHubContext.Format.ps1xml b/src/formats/GitHubContext.Format.ps1xml index 638e3d673..5897f2b75 100644 --- a/src/formats/GitHubContext.Format.ps1xml +++ b/src/formats/GitHubContext.Format.ps1xml @@ -53,6 +53,9 @@ + + Name + HostName @@ -95,9 +98,6 @@ DatabaseID - - ID - Owner From ee109e508f97cac2df494279e874ca3107842503 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 12:10:40 +0100 Subject: [PATCH 04/11] FF >> --- .github/workflows/Process-PSModule.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 5252ed337..bc8b830f0 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -36,3 +36,5 @@ jobs: TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }} TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }} TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }} + with: + SkipTests: All From 61850845a2f9b47dfa5795c9f44696beccc99bc2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 12:55:55 +0100 Subject: [PATCH 05/11] Refactor ConvertFrom-GitHubOutput to accept OutputContent parameter and improve handling of empty lines --- .../Commands/ConvertFrom-GitHubOutput.ps1 | 47 +++++++++++-------- .../public/Commands/Get-GitHubOutput.ps1 | 20 ++------ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index 3e74ee36c..541c32d48 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -20,7 +20,7 @@ Numbers=12345 '@ - $content | ConvertFrom-GitHubOutput + ConvertFrom-GitHubOutput -OutputContent $content zen : something else result : @{MyOutput=Hello, World!; Status=Success} @@ -38,12 +38,9 @@ [CmdletBinding()] param( # The input data to convert - [Parameter( - Mandatory, - ValueFromPipeline - )] - [AllowNull()] - [string[]] $InputData, + [Parameter(Mandatory)] + [AllowEmptyString()] + [string] $OutputContent, # Whether to convert the input data to a hashtable [switch] $AsHashtable @@ -55,23 +52,28 @@ $lines = @() } + process { Write-Debug "[$stackPath] - Process - Start" - if (-not $InputData) { - $InputData = '' + $lines = $OutputContent -split [System.Environment]::NewLine + Write-Debug "[$stackPath] - Output lines: $($lines.Count)" + if ($lines.count -eq 0) { + return @{} } - foreach ($line in $InputData) { - Write-Debug "Line: $line" - $lines += $line -split "`n" + $lines | ForEach-Object { Write-Debug "[$_]" } + + foreach ($line in $lines) { + if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) { + Write-Debug "[$line] - Empty line" + $content += '' + continue + } + Write-Debug "[$line] - Non-empty line" + $content += $line } - Write-Debug "[$stackPath] - End - Start" # Initialize variables $result = @{} $i = 0 - - Write-Debug "Lines: $($lines.Count)" - $lines | ForEach-Object { Write-Debug "[$_]" } - while ($i -lt $lines.Count) { $line = $lines[$i].Trim() Write-Debug "[$line]" @@ -82,7 +84,9 @@ $key = $Matches[1].Trim() $value = $Matches[2] + # Check for empty value if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) { + Write-Debug ' - key=value pattern - Empty value' $result[$key] = '' $i++ continue @@ -90,7 +94,7 @@ # Attempt to parse JSON if (Test-Json $value -ErrorAction SilentlyContinue) { - Write-Debug "[$key] - value is JSON" + Write-Debug " - key=value pattern - value is JSON" $value = ConvertFrom-Json $value -AsHashtable:$AsHashtable } @@ -122,13 +126,16 @@ $i++ } - $value = $value_lines -join "`n" + $value = $value_lines -join [System.Environment]::NewLine + # Check for empty value if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) { + Write-Debug ' - key< Date: Tue, 7 Jan 2025 13:27:04 +0100 Subject: [PATCH 06/11] Improve handling of empty values in ConvertFrom-GitHubOutput function --- src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index 541c32d48..1d991cad5 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -60,7 +60,6 @@ if ($lines.count -eq 0) { return @{} } - $lines | ForEach-Object { Write-Debug "[$_]" } foreach ($line in $lines) { if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) { @@ -85,7 +84,7 @@ $value = $Matches[2] # Check for empty value - if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) { + if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value) -or $value.Length -eq 0) { Write-Debug ' - key=value pattern - Empty value' $result[$key] = '' $i++ @@ -129,7 +128,7 @@ $value = $value_lines -join [System.Environment]::NewLine # Check for empty value - if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) { + if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value) -or $value.Length -eq 0) { Write-Debug ' - key< Date: Tue, 7 Jan 2025 14:21:12 +0100 Subject: [PATCH 07/11] Refactor logging group commands to simplify structure and improve readability --- .../Commands/ConvertFrom-GitHubOutput.ps1 | 40 +++++++------------ .../public/Commands/Set-GitHubLogGroup.ps1 | 19 +++------ .../public/Commands/Start-GitHubLogGroup.ps1 | 11 +---- .../public/Commands/Stop-GitHubLogGroup.ps1 | 11 +---- 4 files changed, 22 insertions(+), 59 deletions(-) diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index 1d991cad5..1434bd0e6 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -49,10 +49,8 @@ begin { $stackPath = Get-PSCallStackPath Write-Debug "[$stackPath] - Start" - $lines = @() } - process { Write-Debug "[$stackPath] - Process - Start" $lines = $OutputContent -split [System.Environment]::NewLine @@ -61,31 +59,21 @@ return @{} } - foreach ($line in $lines) { - if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) { - Write-Debug "[$line] - Empty line" - $content += '' - continue - } - Write-Debug "[$line] - Non-empty line" - $content += $line - } - # Initialize variables $result = @{} $i = 0 - while ($i -lt $lines.Count) { - $line = $lines[$i].Trim() + foreach ($line in $lines) { Write-Debug "[$line]" - # Check for key=value pattern + # Check for key=value pattern (single-line) if ($line -match '^([^=]+)=(.*)$') { - Write-Debug ' - key=value pattern' + Write-Debug ' - Single-line pattern' $key = $Matches[1].Trim() $value = $Matches[2] + Write-Debug " - Single-line pattern - [$key] = [$value]" # Check for empty value if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value) -or $value.Length -eq 0) { - Write-Debug ' - key=value pattern - Empty value' + Write-Debug ' - Single-line pattern - Empty value' $result[$key] = '' $i++ continue @@ -93,7 +81,7 @@ # Attempt to parse JSON if (Test-Json $value -ErrorAction SilentlyContinue) { - Write-Debug " - key=value pattern - value is JSON" + Write-Debug " - Single-line pattern - value is JSON" $value = ConvertFrom-Json $value -AsHashtable:$AsHashtable } @@ -102,12 +90,13 @@ continue } - # Check for key< Date: Tue, 7 Jan 2025 15:08:16 +0100 Subject: [PATCH 08/11] Refactor ConvertFrom-GitHubOutput to use while loop for line iteration and improve debug output format --- src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index 1434bd0e6..c9a736e58 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -61,7 +61,7 @@ $result = @{} $i = 0 - foreach ($line in $lines) { + while ($i -lt $lines.Count) { Write-Debug "[$line]" # Check for key=value pattern (single-line) @@ -103,7 +103,7 @@ # Read lines until the EOF marker while ($i -lt $lines.Count -and $lines[$i] -ne $eof_marker) { $valueItem = $lines[$i].Trim() - Write-Debug " [$valueItem]" + Write-Debug " [$key] <- [$valueItem]" $value_lines += $valueItem $i++ } From c31c732f244bfce628e73cb1a17ffc5d41a5b9dd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 15:24:12 +0100 Subject: [PATCH 09/11] Trim whitespace from lines in ConvertFrom-GitHubOutput for improved processing --- src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index c9a736e58..cd921e9b9 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -62,6 +62,7 @@ $result = @{} $i = 0 while ($i -lt $lines.Count) { + $line = $lines[$i].Trim() Write-Debug "[$line]" # Check for key=value pattern (single-line) From 1d31375678a505fd06d46df7339e3c2dcfde9831 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 16:23:47 +0100 Subject: [PATCH 10/11] Enhance debugging output in ConvertFrom-GitHubOutput and remove unnecessary debug logs in Get-GitHubOutput --- .github/workflows/Process-PSModule.yml | 2 -- src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 | 4 +++- src/functions/public/Commands/Get-GitHubOutput.ps1 | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index bc8b830f0..5252ed337 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -36,5 +36,3 @@ jobs: TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }} TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }} TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }} - with: - SkipTests: All diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index cd921e9b9..c89a6cc91 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -61,9 +61,11 @@ $result = @{} $i = 0 + $pad = $lines.count.ToString().Length while ($i -lt $lines.Count) { + $lineNumber = ($i + 1).ToString().PadLeft($pad) $line = $lines[$i].Trim() - Write-Debug "[$line]" + Write-Debug "[$lineNumber]: [$line]" # Check for key=value pattern (single-line) if ($line -match '^([^=]+)=(.*)$') { diff --git a/src/functions/public/Commands/Get-GitHubOutput.ps1 b/src/functions/public/Commands/Get-GitHubOutput.ps1 index 0655464b5..166423781 100644 --- a/src/functions/public/Commands/Get-GitHubOutput.ps1 +++ b/src/functions/public/Commands/Get-GitHubOutput.ps1 @@ -57,8 +57,6 @@ } $outputContent = Get-Content -Path $Path -Raw - Write-Debug "[$stackPath] - Output content" - Write-Debug ($outputContent | Out-String) ConvertFrom-GitHubOutput -OutputContent $outputContent -AsHashtable:$AsHashtable } catch { throw $_ From 90d17431e01511e1b318b67ca86a55dc5aad8b2f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Jan 2025 16:28:53 +0100 Subject: [PATCH 11/11] Suppress code analysis warning for Write-Host usage in Set-GitHubLogGroup function for compatibility with GitHub Runners --- src/functions/public/Commands/Set-GitHubLogGroup.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/functions/public/Commands/Set-GitHubLogGroup.ps1 b/src/functions/public/Commands/Set-GitHubLogGroup.ps1 index a70afec9f..5ac4e0845 100644 --- a/src/functions/public/Commands/Set-GitHubLogGroup.ps1 +++ b/src/functions/public/Commands/Set-GitHubLogGroup.ps1 @@ -29,6 +29,10 @@ '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( # The name of the log group