Skip to content

Commit

Permalink
🩹 [Patch]: Streamline interaction between Get-GitHubOutput and `Con…
Browse files Browse the repository at this point in the history
…vertFrom-GitHubOutput` (#256)

## Description

This pull request primarily focuses on refactoring and simplifying the
codebase. The most significant changes include modifications to the
`ConvertFrom-GitHubOutput` function, removal of unnecessary debug code,
and streamlining the `Set-GitHubLogGroup`, `Start-GitHubLogGroup`, and
`Stop-GitHubLogGroup` functions.

Refactoring and simplification:

*
[`src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1`](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L23-R23):
Refactored the function to use `OutputContent` instead of `InputData`,
streamlined the processing logic, and improved debug messages for better
clarity.
[[1]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L23-R23)
[[2]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L41-R43)
[[3]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L55-R87)
[[4]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L102-R131)
[[5]](diffhunk://#diff-932d91e541fddb09fdf24c5538bc3bff2f26aab6f4da41cb937c5a0b4bd53f99L142-R148)
*
[`src/functions/public/Commands/Get-GitHubOutput.ps1`](diffhunk://#diff-7a9d9dc46c69778a8be116cb790362a94df6c76355c4575c5d195537097f4676L59-R60):
Simplified the function by using the `-Raw` parameter with `Get-Content`
and directly passing the content to `ConvertFrom-GitHubOutput`.

Removal of unnecessary debug code:

*
[`src/functions/public/Commands/Set-GitHubLogGroup.ps1`](diffhunk://#diff-18916321a4a96d2ef4e5dc8a43ef587452e3e04092aff724e60cae7b9d532f98L43-L60):
Removed the `begin`, `process`, and `end` blocks and unnecessary debug
messages, simplifying the function to directly use `Write-Host`.
*
[`src/functions/public/Commands/Start-GitHubLogGroup.ps1`](diffhunk://#diff-868454a7517a8963907fdde37442352a7ec723ff0b1952de42de95a9310423b9L30-L45):
Removed unnecessary debug messages and the `begin`, `process`, and `end`
blocks, simplifying the function to directly use `Write-Host`.
*
[`src/functions/public/Commands/Stop-GitHubLogGroup.ps1`](diffhunk://#diff-c2da82ea54497efb40d18798f4e8875747350804657ebe269d26102d1ce61a97L26-L41):
Removed unnecessary debug messages and the `begin`, `process`, and `end`
blocks, simplifying the function to directly use `Write-Host`.

Minor adjustments:

*
[`src/formats/GitHubContext.Format.ps1xml`](diffhunk://#diff-c7dd80cd4c4440ccbe24930842a2e172614dbfd79d31393d68852200eacc2c74R56-R58):
Added `Name` property and removed `ID` property from the list entries.
[[1]](diffhunk://#diff-c7dd80cd4c4440ccbe24930842a2e172614dbfd79d31393d68852200eacc2c74R56-R58)
[[2]](diffhunk://#diff-c7dd80cd4c4440ccbe24930842a2e172614dbfd79d31393d68852200eacc2c74L98-L100)

## 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 7, 2025
1 parent ccc9d37 commit 6ca1d3b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src/formats/GitHubContext.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>HostName</PropertyName>
</ListItem>
Expand Down Expand Up @@ -95,9 +98,6 @@
<ListItem>
<PropertyName>DatabaseID</PropertyName>
</ListItem>
<ListItem>
<PropertyName>ID</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Owner</PropertyName>
</ListItem>
Expand Down
67 changes: 33 additions & 34 deletions src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Numbers=12345
'@
$content | ConvertFrom-GitHubOutput
ConvertFrom-GitHubOutput -OutputContent $content
zen : something else
result : @{MyOutput=Hello, World!; Status=Success}
Expand All @@ -38,12 +38,9 @@
[CmdletBinding()]
param(
# The input data to convert
[Parameter(
Mandatory,
ValueFromPipeline
)]
[AllowNull()]
[string[]] $InputData,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string] $OutputContent,

# Whether to convert the input data to a hashtable
[switch] $AsHashtable
Expand All @@ -52,45 +49,42 @@
begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
$lines = @()
}

process {
Write-Debug "[$stackPath] - Process - Start"
if (-not $InputData) {
$InputData = ''
$lines = $OutputContent -split [System.Environment]::NewLine
Write-Debug "[$stackPath] - Output lines: $($lines.Count)"
if ($lines.count -eq 0) {
return @{}
}
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 "[$_]" }

$pad = $lines.count.ToString().Length
while ($i -lt $lines.Count) {
$lineNumber = ($i + 1).ToString().PadLeft($pad)
$line = $lines[$i].Trim()
Write-Debug "[$line]"
Write-Debug "[$lineNumber]: [$line]"

# Check for key=value pattern
# Check for key=value pattern (single-line)
if ($line -match '^([^=]+)=(.*)$') {
Write-Debug ' - key=value pattern'
Write-Debug ' - Single-line pattern'
$key = $Matches[1].Trim()
$value = $Matches[2]

if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) {
Write-Debug " - Single-line pattern - [$key] = [$value]"
# Check for empty value
if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value) -or $value.Length -eq 0) {
Write-Debug ' - Single-line pattern - Empty value'
$result[$key] = ''
$i++
continue
}

# Attempt to parse JSON
if (Test-Json $value -ErrorAction SilentlyContinue) {
Write-Debug "[$key] - value is JSON"
Write-Debug " - Single-line pattern - value is JSON"
$value = ConvertFrom-Json $value -AsHashtable:$AsHashtable
}

Expand All @@ -99,38 +93,42 @@
continue
}

# Check for key<<EOF pattern
# Check for key<<EOF pattern (multi-line)
if ($line -match '^([^<]+)<<(\S+)$') {
Write-Debug ' - key<<EOF pattern'
Write-Debug ' - Multi-line pattern'
$key = $Matches[1].Trim()
Write-Debug " - Multi-line pattern' - [$key]"
$eof_marker = $Matches[2]
Write-Debug " - key<<EOF pattern - [$eof_marker] - Start"
Write-Debug " - Multi-line pattern' - [$key] - [$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 " [$valueItem]"
Write-Debug " [$key] <- [$valueItem]"
$value_lines += $valueItem
$i++
}

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

$value = $value_lines -join "`n"
$value = $value_lines -join [System.Environment]::NewLine

if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value)) {
# Check for empty value
if ([string]::IsNullOrWhiteSpace($value) -or [string]::IsNullOrEmpty($value) -or $value.Length -eq 0) {
Write-Debug " - key<<EOF pattern - [$key] - Empty value"
$result[$key] = ''
continue
}

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

Expand All @@ -139,14 +137,15 @@
}

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

end {
Write-Debug "[$stackPath] - End - Start"
if ($AsHashtable) {
$result
} else {
Expand Down
19 changes: 2 additions & 17 deletions src/functions/public/Commands/Get-GitHubOutput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,8 @@
throw "File not found: $Path"
}

$outputContent = Get-Content -Path $Path
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 ($content | Out-String)
$content | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
$outputContent = Get-Content -Path $Path -Raw
ConvertFrom-GitHubOutput -OutputContent $outputContent -AsHashtable:$AsHashtable
} catch {
throw $_
}
Expand Down
26 changes: 10 additions & 16 deletions src/functions/public/Commands/Set-GitHubLogGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Does not change state'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingWriteHost', '', Scope = 'Function',
Justification = 'Intended for logging in Github Runners which does support Write-Host'
)]
[CmdletBinding()]
param(
# The name of the log group
Expand All @@ -40,22 +44,12 @@
[scriptblock] $ScriptBlock
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
}

process {
Start-GitHubLogGroup -Name $Name
try {
. $ScriptBlock
} catch {
throw $_
}
Stop-GitHubLogGroup
Write-Host "::group::$Name"
try {
. $ScriptBlock
} catch {
throw $_
}
Write-Host '::endgroup::'

end {
Write-Debug "[$stackPath] - End"
}
}
16 changes: 1 addition & 15 deletions src/functions/public/Commands/Start-GitHubLogGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@
[string] $Name
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
}
Write-Host "::group::$Name"

process {
try {
Write-Host "::group::$Name"
} catch {
throw $_
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
16 changes: 1 addition & 15 deletions src/functions/public/Commands/Stop-GitHubLogGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@
[Alias('Stop-LogGroup')]
param()

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
}
Write-Host '::endgroup::'

process {
try {
Write-Host '::endgroup::'
} catch {
throw $_
}
}

end {
Write-Debug "[$stackPath] - End"
}
}

0 comments on commit 6ca1d3b

Please sign in to comment.