Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}