Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 [Patch]: Improve debug and null/blank values on the GitHub Actions output commands #250

Merged
merged 10 commits into from
Jan 4, 2025
Merged
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
Loading