From 1061ffbbe33e21411aecf02fea3b6915f1ec15b5 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 3 Jan 2025 15:03:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Enhance=20Get-GitHubO?= =?UTF-8?q?utput=20with=20debug=20logging=20and=20handle=20empty=20output?= =?UTF-8?q?=20(#247)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This pull request includes a change to the `Get-GitHubOutput.ps1` script to improve debugging and error handling. Improvements to debugging and error handling: * [`src/functions/public/Commands/Get-GitHubOutput.ps1`](diffhunk://#diff-7a9d9dc46c69778a8be116cb790362a94df6c76355c4575c5d195537097f4676L54-R60): Added debug logging for output content and a check to return early if the content is empty. ## Type of change - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist - [ ] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --- .../public/Commands/Get-GitHubOutput.ps1 | 15 +++++++++++++-- .../public/Commands/Set-GitHubOutput.ps1 | 10 ++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/functions/public/Commands/Get-GitHubOutput.ps1 b/src/functions/public/Commands/Get-GitHubOutput.ps1 index ed7b7ff67..6285baae6 100644 --- a/src/functions/public/Commands/Get-GitHubOutput.ps1 +++ b/src/functions/public/Commands/Get-GitHubOutput.ps1 @@ -28,7 +28,7 @@ Gets the GitHub output and returns a hashtable. #> - [OutputType([pscustomobject])] + [OutputType([hashtable])] [CmdletBinding()] param( # Returns the output as a hashtable. @@ -47,11 +47,22 @@ process { try { + if (-not $Path) { + throw 'The path to the GitHub output file is not set. Please set the path to the GitHub output file using the -Path parameter.' + } + Write-Debug "[$stackPath] - Output path" + Write-Debug $Path if (-not (Test-Path -Path $Path)) { throw "File not found: $Path" } - Get-Content -Path $Path | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable + $outputContent = Get-Content -Path $Path + if (-not $outputContent) { + return @{} + } + Write-Debug "[$stackPath] - Output content" + Write-Debug $outputContent + $outputContent | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable } catch { throw $_ } diff --git a/src/functions/public/Commands/Set-GitHubOutput.ps1 b/src/functions/public/Commands/Set-GitHubOutput.ps1 index f3d54d5f2..85b882c67 100644 --- a/src/functions/public/Commands/Set-GitHubOutput.ps1 +++ b/src/functions/public/Commands/Set-GitHubOutput.ps1 @@ -63,13 +63,11 @@ # If the script is running in a GitHub composite action, accumulate the output under the 'result' key, # else append the key-value pair directly. if ($env:PSMODULE_GITHUB_SCRIPT) { - if (-not $outputs.result) { - $outputs.result = @{ - $Name = $Value - } - } else { - $outputs.result[$Name] = $Value + Write-Debug "[$stackPath] - Running in GitHub-Script composite action" + if (-not $outputs.ContainsKey('result')) { + $outputs['result'] = @{} } + $outputs['result'][$Name] = $Value } else { $outputs[$Name] = $Value }