Skip to content

Commit 035708b

Browse files
🩹 [Patch]: Improve debug and null/blank values on the GitHub Actions 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
1 parent 5a8def0 commit 035708b

File tree

5 files changed

+74
-43
lines changed

5 files changed

+74
-43
lines changed

.github/workflows/Process-PSModule.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,3 @@ jobs:
3636
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
3737
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
3838
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
39-
with:
40-
Debug: true
41-
Verbose: true

src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
Mandatory,
4343
ValueFromPipeline
4444
)]
45+
[AllowNull()]
4546
[string[]] $InputData,
4647

4748
# Whether to convert the input data to a hashtable
@@ -55,38 +56,41 @@
5556
}
5657

5758
process {
58-
foreach ($item in $InputData) {
59-
if ($item -is [string]) {
60-
$lines += $item -split "`n"
61-
}
59+
Write-Debug "[$stackPath] - Process - Start"
60+
if (-not $InputData) {
61+
$InputData = ''
6262
}
63-
}
64-
65-
end {
63+
foreach ($line in $InputData) {
64+
Write-Debug "Line: $line"
65+
$lines += $line -split "`n"
66+
}
67+
Write-Debug "[$stackPath] - End - Start"
6668
# Initialize variables
6769
$result = @{}
6870
$i = 0
6971

72+
Write-Debug "Lines: $($lines.Count)"
73+
$lines | ForEach-Object { Write-Debug "[$_]" }
74+
7075
while ($i -lt $lines.Count) {
7176
$line = $lines[$i].Trim()
7277
Write-Debug "[$line]"
7378

74-
# Skip empty or delimiter lines
75-
if ($line -match '^-+$' -or [string]::IsNullOrWhiteSpace($line)) {
76-
Write-Debug "[$line] - Skipping empty line"
77-
$i++
78-
continue
79-
}
80-
8179
# Check for key=value pattern
8280
if ($line -match '^([^=]+)=(.*)$') {
83-
Write-Debug "[$line] - key=value pattern"
81+
Write-Debug ' - key=value pattern'
8482
$key = $Matches[1].Trim()
8583
$value = $Matches[2]
8684

85+
if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) {
86+
$result[$key] = ''
87+
$i++
88+
continue
89+
}
90+
8791
# Attempt to parse JSON
8892
if (Test-Json $value -ErrorAction SilentlyContinue) {
89-
Write-Debug "[$line] - value is JSON"
93+
Write-Debug "[$key] - value is JSON"
9094
$value = ConvertFrom-Json $value -AsHashtable:$AsHashtable
9195
}
9296

@@ -97,43 +101,57 @@
97101

98102
# Check for key<<EOF pattern
99103
if ($line -match '^([^<]+)<<(\S+)$') {
100-
Write-Debug "[$line] - key<<EOF pattern"
104+
Write-Debug ' - key<<EOF pattern'
101105
$key = $Matches[1].Trim()
102106
$eof_marker = $Matches[2]
103-
Write-Debug "[$line] - key<<EOF pattern - [$eof_marker]"
107+
Write-Debug " - key<<EOF pattern - [$eof_marker] - Start"
104108
$i++
105109
$value_lines = @()
106110

111+
# Read lines until the EOF marker
107112
while ($i -lt $lines.Count -and $lines[$i] -ne $eof_marker) {
108113
$valueItem = $lines[$i].Trim()
109-
Write-Debug "[$line] - key<<EOF pattern - [$eof_marker] - [$valueItem]"
114+
Write-Debug " [$valueItem]"
110115
$value_lines += $valueItem
111116
$i++
112117
}
113118

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

120125
$value = $value_lines -join "`n"
121126

127+
if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) {
128+
$result[$key] = ''
129+
continue
130+
}
131+
122132
if (Test-Json $value -ErrorAction SilentlyContinue) {
123-
Write-Debug "[$line] - key<<EOF pattern - value is JSON"
133+
Write-Debug ' - key<<EOF pattern - value is JSON'
124134
$value = ConvertFrom-Json $value -AsHashtable:$AsHashtable
125135
}
126136

127137
$result[$key] = $value
128138
continue
129139
}
140+
141+
# Unexpected line type
142+
Write-Debug ' - Skipping empty line'
130143
$i++
144+
continue
131145
}
146+
Write-Debug "[$stackPath] - Process - End"
147+
}
148+
149+
end {
132150
if ($AsHashtable) {
133151
$result
134152
} else {
135153
[PSCustomObject]$result
136154
}
137-
Write-Debug "[$stackPath] - End"
155+
Write-Debug "[$stackPath] - End - End"
138156
}
139157
}

src/functions/private/Commands/ConvertTo-GitHubOutput.ps1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
try {
5656
$outputLines = @()
5757

58+
Write-Debug "Input object type: $($InputObject.GetType().Name)"
59+
Write-Debug "Input object value:"
60+
Write-Debug $InputObject
61+
5862
if ($InputObject -is [hashtable]) {
5963
$InputObject = [PSCustomObject]$InputObject
6064
}
@@ -63,22 +67,24 @@
6367
$key = $property.Name
6468
$value = $property.Value
6569

70+
Write-Debug "Processing property: $key"
71+
Write-Debug "Property value type: $($value.GetType().Name)"
72+
Write-Debug "Property value:"
73+
Write-Debug $value
74+
6675
# Convert hashtable or PSCustomObject to compressed JSON
6776
if ($value -is [hashtable] -or $value -is [PSCustomObject]) {
68-
$value = $value | ConvertTo-Json -Compress
77+
Write-Debug "Converting property value to JSON"
78+
$value = $value | ConvertTo-Json -Compress -Depth 100
79+
Write-Debug 'Property value:'
80+
Write-Debug $value
6981
}
7082

71-
if ($value -is [string] -and $value.Contains("`n")) {
72-
# Multi-line value
73-
$guid = [Guid]::NewGuid().ToString()
74-
$EOFMarker = "EOF_$guid"
75-
$outputLines += "$key<<$EOFMarker"
76-
$outputLines += $value
77-
$outputLines += $EOFMarker
78-
} else {
79-
# Single-line value
80-
$outputLines += "$key=$value"
81-
}
83+
$guid = [Guid]::NewGuid().ToString()
84+
$EOFMarker = "EOF_$guid"
85+
$outputLines += "$key<<$EOFMarker"
86+
$outputLines += $value
87+
$outputLines += $EOFMarker
8288
}
8389
$outputLines
8490
} catch {

src/functions/public/Commands/Get-GitHubOutput.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@
5656
throw "File not found: $Path"
5757
}
5858

59-
$outputContent = Get-Content -Path $Path
60-
if (-not $outputContent) {
59+
$outputContent = Get-Content -Path $Path -Raw
60+
Write-Debug "[$stackPath] - Output lines: $($outputContent.Count)"
61+
if ($outputContent.count -eq 0) {
6162
return @{}
6263
}
64+
65+
$content = @()
66+
foreach ($line in $outputContent) {
67+
if ([string]::IsNullOrWhiteSpace($line) -or [string]::IsNullOrEmpty($line)) {
68+
$content += ''
69+
continue
70+
}
71+
$content += $line
72+
}
6373
Write-Debug "[$stackPath] - Output content"
64-
Write-Debug ($outputContent | Out-String)
65-
$outputContent | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
74+
Write-Debug ($content | Out-String)
75+
$content | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
6676
} catch {
6777
throw $_
6878
}

src/functions/public/Commands/Set-GitHubOutput.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@
6868
$outputs['result'] = @{}
6969
}
7070
$outputs['result'][$Name] = $Value
71+
Write-Verbose "Output: [$Name] avaiable as `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).$Name }}'"
7172
} else {
7273
$outputs[$Name] = $Value
74+
Write-Verbose "Output: [$Name] avaiable as `${{ steps.$env:GITHUB_ACTION.outputs.$Name }}'"
7375
}
7476

75-
Write-Verbose "Output: [$Name] avaiable as `${{ steps.$env:GITHUB_ACTION.outputs.$Name }}'"
76-
7777
if ($PSCmdlet.ShouldProcess('GitHub Output', 'Set')) {
7878
$outputs | ConvertTo-GitHubOutput | Set-Content -Path $Path
7979
}

0 commit comments

Comments
 (0)