Skip to content

Commit

Permalink
🩹 [Patch]: Improve debug and null/blank values on the GitHub Actions …
Browse files Browse the repository at this point in the history
…output commands (#250)

## 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

<!-- 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
  • Loading branch information
MariusStorhaug authored Jan 4, 2025
1 parent 5a8def0 commit 035708b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 43 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/Process-PSModule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
62 changes: 40 additions & 22 deletions src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Mandatory,
ValueFromPipeline
)]
[AllowNull()]
[string[]] $InputData,

# Whether to convert the input data to a hashtable
Expand All @@ -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
}

Expand All @@ -97,43 +101,57 @@

# Check for key<<EOF pattern
if ($line -match '^([^<]+)<<(\S+)$') {
Write-Debug "[$line] - key<<EOF pattern"
Write-Debug ' - key<<EOF pattern'
$key = $Matches[1].Trim()
$eof_marker = $Matches[2]
Write-Debug "[$line] - key<<EOF pattern - [$eof_marker]"
Write-Debug " - key<<EOF pattern - [$eof_marker] - Start"
$i++
$value_lines = @()

# Read lines until the EOF marker
while ($i -lt $lines.Count -and $lines[$i] -ne $eof_marker) {
$valueItem = $lines[$i].Trim()
Write-Debug "[$line] - key<<EOF pattern - [$eof_marker] - [$valueItem]"
Write-Debug " [$valueItem]"
$value_lines += $valueItem
$i++
}

# Skip the EOF marker
if ($i -lt $lines.Count -and $lines[$i] -eq $eof_marker) {
Write-Debug "[$line] - key<<EOF pattern - Closing"
Write-Debug " - key<<EOF pattern - [$eof_marker] - End"
$i++
}

$value = $value_lines -join "`n"

if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) {
$result[$key] = ''
continue
}

if (Test-Json $value -ErrorAction SilentlyContinue) {
Write-Debug "[$line] - key<<EOF pattern - value is JSON"
Write-Debug ' - key<<EOF pattern - value is JSON'
$value = ConvertFrom-Json $value -AsHashtable:$AsHashtable
}

$result[$key] = $value
continue
}

# Unexpected line type
Write-Debug ' - Skipping empty line'
$i++
continue
}
Write-Debug "[$stackPath] - Process - End"
}

end {
if ($AsHashtable) {
$result
} else {
[PSCustomObject]$result
}
Write-Debug "[$stackPath] - End"
Write-Debug "[$stackPath] - End - End"
}
}
30 changes: 18 additions & 12 deletions src/functions/private/Commands/ConvertTo-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
try {
$outputLines = @()

Write-Debug "Input object type: $($InputObject.GetType().Name)"
Write-Debug "Input object value:"
Write-Debug $InputObject

if ($InputObject -is [hashtable]) {
$InputObject = [PSCustomObject]$InputObject
}
Expand All @@ -63,22 +67,24 @@
$key = $property.Name
$value = $property.Value

Write-Debug "Processing property: $key"
Write-Debug "Property value type: $($value.GetType().Name)"
Write-Debug "Property value:"
Write-Debug $value

# Convert hashtable or PSCustomObject to compressed JSON
if ($value -is [hashtable] -or $value -is [PSCustomObject]) {
$value = $value | ConvertTo-Json -Compress
Write-Debug "Converting property value to JSON"
$value = $value | ConvertTo-Json -Compress -Depth 100
Write-Debug 'Property value:'
Write-Debug $value
}

if ($value -is [string] -and $value.Contains("`n")) {
# Multi-line value
$guid = [Guid]::NewGuid().ToString()
$EOFMarker = "EOF_$guid"
$outputLines += "$key<<$EOFMarker"
$outputLines += $value
$outputLines += $EOFMarker
} else {
# Single-line value
$outputLines += "$key=$value"
}
$guid = [Guid]::NewGuid().ToString()
$EOFMarker = "EOF_$guid"
$outputLines += "$key<<$EOFMarker"
$outputLines += $value
$outputLines += $EOFMarker
}
$outputLines
} catch {
Expand Down
18 changes: 14 additions & 4 deletions src/functions/public/Commands/Get-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@
throw "File not found: $Path"
}

$outputContent = Get-Content -Path $Path
if (-not $outputContent) {
$outputContent = Get-Content -Path $Path -Raw
Write-Debug "[$stackPath] - Output lines: $($outputContent.Count)"
if ($outputContent.count -eq 0) {
return @{}
}

$content = @()
foreach ($line in $outputContent) {
if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) {
$content += ''
continue
}
$content += $line
}
Write-Debug "[$stackPath] - Output content"
Write-Debug ($outputContent | Out-String)
$outputContent | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
Write-Debug ($content | Out-String)
$content | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
} catch {
throw $_
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/public/Commands/Set-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
$outputs['result'] = @{}
}
$outputs['result'][$Name] = $Value
Write-Verbose "Output: [$Name] avaiable as `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).$Name }}'"
} else {
$outputs[$Name] = $Value
Write-Verbose "Output: [$Name] avaiable as `${{ steps.$env:GITHUB_ACTION.outputs.$Name }}'"
}

Write-Verbose "Output: [$Name] avaiable as `${{ steps.$env:GITHUB_ACTION.outputs.$Name }}'"

if ($PSCmdlet.ShouldProcess('GitHub Output', 'Set')) {
$outputs | ConvertTo-GitHubOutput | Set-Content -Path $Path
}
Expand Down

0 comments on commit 035708b

Please sign in to comment.