From 035708b99266aa15b93e5c1a599d27045118d58a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 4 Jan 2025 20:32:33 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Improve=20debug=20and?= =?UTF-8?q?=20null/blank=20values=20on=20the=20GitHub=20Actions=20output?= =?UTF-8?q?=20commands=20(#250)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This pull request improves the debugging capabilities and handling of null or empty values in various functions related to GitHub Action outputs. Enhancements to debugging and handling of null or empty values: * [`src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1`](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L58-R93): Added multiple `Write-Debug` statements to provide detailed logging of the process flow and improved handling of null or empty `$InputData` and values. [[1]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L58-R93) [[2]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L100-R155) * [`src/functions/private/Commands/ConvertTo-GitHubOutput.ps1`](diffhunk://#diff-034d54e9e300e69d4f8ec92a22bf2cec2e21f82b9ff6d0d59af1014bebe56b10R58-R61): Added `Write-Debug` statements to log the input object type and value, and the processing of properties, including conversion to JSON with increased depth. [[1]](diffhunk://#diff-034d54e9e300e69d4f8ec92a22bf2cec2e21f82b9ff6d0d59af1014bebe56b10R58-R61) [[2]](diffhunk://#diff-034d54e9e300e69d4f8ec92a22bf2cec2e21f82b9ff6d0d59af1014bebe56b10R70-L81) * [`src/functions/public/Commands/Get-GitHubOutput.ps1`](diffhunk://#diff-7a9d9dc46c69778a8be116cb790362a94df6c76355c4575c5d195537097f4676L59-R75): Modified to read the file content as raw and added logic to handle empty lines, along with additional debug logging. * [`src/functions/public/Commands/Set-GitHubOutput.ps1`](diffhunk://#diff-e3aad576b04b558ce2a70ebd0dcc703418e81431e3c0f0ed63a3736ae35de2efR71-R75): Added a `Write-Verbose` statement to log the output availability in a more detailed manner. ## Type of change - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --- .github/workflows/Process-PSModule.yml | 3 - .../Commands/ConvertFrom-GitHubOutput.ps1 | 62 ++++++++++++------- .../Commands/ConvertTo-GitHubOutput.ps1 | 30 +++++---- .../public/Commands/Get-GitHubOutput.ps1 | 18 ++++-- .../public/Commands/Set-GitHubOutput.ps1 | 4 +- 5 files changed, 74 insertions(+), 43 deletions(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 3d703e6a5..5252ed337 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -36,6 +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: - Debug: true - Verbose: true diff --git a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 index 03eec2271..3e74ee36c 100644 --- a/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 +++ b/src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1 @@ -42,6 +42,7 @@ Mandatory, ValueFromPipeline )] + [AllowNull()] [string[]] $InputData, # Whether to convert the input data to a hashtable @@ -55,38 +56,41 @@ } process { - foreach ($item in $InputData) { - if ($item -is [string]) { - $lines += $item -split "`n" - } + Write-Debug "[$stackPath] - Process - Start" + if (-not $InputData) { + $InputData = '' } - } - - end { + foreach ($line in $InputData) { + Write-Debug "Line: $line" + $lines += $line -split "`n" + } + 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]" - # Skip empty or delimiter lines - if ($line -match '^-+$' -or [string]::IsNullOrWhiteSpace($line)) { - Write-Debug "[$line] - Skipping empty line" - $i++ - continue - } - # Check for key=value pattern if ($line -match '^([^=]+)=(.*)$') { - Write-Debug "[$line] - key=value pattern" + Write-Debug ' - key=value pattern' $key = $Matches[1].Trim() $value = $Matches[2] + if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) { + $result[$key] = '' + $i++ + continue + } + # Attempt to parse JSON if (Test-Json $value -ErrorAction SilentlyContinue) { - Write-Debug "[$line] - value is JSON" + Write-Debug "[$key] - value is JSON" $value = ConvertFrom-Json $value -AsHashtable:$AsHashtable } @@ -97,43 +101,57 @@ # Check for key<