Skip to content

Commit

Permalink
🪲 [Fix]: Fix coverage reporting and some GHA commands (#174)
Browse files Browse the repository at this point in the history
## Description

- Updated the schedule for updating the coverage report to run every
hour instead of on push or pull request events.
- Simplified the `Set-CoverageReport.ps1` script.
- Added new functions for handling GitHub Actions outputs so that we can
easily convert between objects and GitHub output syntax.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [x] 🪲 [Fix]
- [ ] 🩹 [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

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
MariusStorhaug and github-actions[bot] authored Dec 1, 2024
1 parent 00d6200 commit ea2ddb0
Show file tree
Hide file tree
Showing 12 changed files with 1,017 additions and 694 deletions.
29 changes: 3 additions & 26 deletions .github/workflows/Set-CoverageReport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ name: Update coverage report

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
branches:
- main
schedule:
- cron: '0 * * * *'

permissions:
contents: write
Expand All @@ -22,24 +19,4 @@ jobs:
- name: Update coverage report
uses: PSModule/GitHub-Script@v1
with:
Script: |
. ".\scripts\Set-CoverageReport.ps1" -Verbose
$changedFiles = git diff --name-only
$hasChanges = $null -ne $changedFiles
if (-not $changedFiles) {
Write-Host 'No changes detected'
return
}
LogGroup "Changed files [$($changedFiles.Count)]" {
$changedFiles | ForEach-Object { Write-Host $_ }
}
Get-GitHubContext | Format-Table
Get-GitHubGitConfig | Format-Table
git add .
git commit -m "Auto-generated changes"
git push
Script: . '.\scripts\Set-CoverageReport.ps1'
615 changes: 0 additions & 615 deletions Coverage.MD

This file was deleted.

685 changes: 685 additions & 0 deletions Coverage.md

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions scripts/Set-CoverageReport.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[CmdletBinding()]
#Requires -Modules MarkdownPS

[CmdletBinding()]
param()

function Find-APIMethod {
Expand Down Expand Up @@ -26,6 +28,7 @@ function Find-APIMethod {
$putMatches = Select-String -Path $filePath -Pattern $methodPattern -AllMatches
foreach ($match in $stringMatches) {
foreach ($putMatch in $putMatches) {
Write-Verbose '----------------------------------------'
Write-Verbose "Match found in file: $filePath"
Write-Verbose "API Endpoint: $($match.Matches.Value) near line $($match.LineNumber)"
Write-Verbose "Method: $($putMatch.Matches.Value) near line $($putMatch.LineNumber)"
Expand All @@ -51,7 +54,7 @@ LogGroup 'Generate coverage report' {
$response.paths.PSObject.Properties | ForEach-Object {
$path = $_.Name
$object = [pscustomobject]@{
Path = $path
Path = "``$path``"
DELETE = ''
GET = ''
PATCH = ''
Expand Down Expand Up @@ -100,9 +103,11 @@ LogGroup 'Generate coverage report' {
$($paths | New-MDTable)
"@
$coverageContent | Out-File -FilePath '.\Coverage.md'
Set-Content -Path 'Coverage.md' -Value $coverageContent
}

LogGroup 'Coverage report' {
Get-Content -Path '.\Coverage.md' | ForEach-Object { Write-Verbose $_ -Verbose }
}
Set-GitHubStepSummary -Summary $coverageContent

git add .
git commit -m 'Auto-generated changes'
git push
135 changes: 135 additions & 0 deletions src/functions/private/Commands/ConvertFrom-GitHubOutput.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
filter ConvertFrom-GitHubOutput {
<#
.SYNOPSIS
Gets the GitHub output.
.DESCRIPTION
Gets the GitHub output from $env:GITHUB_OUTPUT and creates an object with key-value pairs,
supporting both single-line and multi-line values, and parsing JSON values.
.EXAMPLE
$content = @'
zen=something else
result={"MyOutput":"Hello, World!","Status":"Success"}
MY_VALUE<<EOF_12a089b9-051e-4c4e-91c9-8e24fc2fbbf6
Line1
Line2
Line3
EOF_12a089b9-051e-4c4e-91c9-8e24fc2fbbf6
Config={"Nested":{"SubSetting":"SubValue"},"Setting1":"Value1","Setting2":2}
Numbers=12345
'@
$content | ConvertFrom-GitHubOutput
zen : something else
result : @{MyOutput=Hello, World!; Status=Success}
MY_VALUE : Line1
Line2
Line3
Config : {[Nested, System.Collections.Hashtable], [Setting1, Value1], [Setting2, 2]}
Numbers : 12345
This will convert the GitHub Actions output syntax to a PowerShell object.
#>
[OutputType([pscustomobject])]
[OutputType([hashtable])]
[CmdletBinding()]
param(
# The input data to convert
[Parameter(
Mandatory,
ValueFromPipeline
)]
[string[]] $InputData,

# Whether to convert the input data to a hashtable
[switch] $AsHashtable
)

begin {
$lines = @()
}

process {
foreach ($item in $InputData) {
if ($item -is [string]) {
$lines += $item -split "`n"
}
}
}

end {
# Initialize variables
$result = @{}
$i = 0

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"
$key = $Matches[1].Trim()
$value = $Matches[2]

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

$result[$key] = $value
$i++
continue
}

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

while ($i -lt $lines.Count -and $lines[$i] -ne $eof_marker) {
$valueItem = $lines[$i].Trim()
Write-Debug "[$line] - key<<EOF pattern - [$eof_marker] - [$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"
$i++
}

$value = $value_lines -join "`n"

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

$result[$key] = $value
continue
}
$i++
}
if ($AsHashtable) {
return $result
}
[PSCustomObject]$result
}
}
77 changes: 77 additions & 0 deletions src/functions/private/Commands/ConvertTo-GitHubOutput.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
filter ConvertTo-GitHubOutput {
<#
.SYNOPSIS
Converts a PowerShell object's properties to expected format for GitHub Actions output syntax.
.DESCRIPTION
The function iterates over each property of the provided PowerShell object and writes
them to a specified file in the format used by GitHub Actions for outputs. It supports:
- Single-line values (written as key=value).
- Multi-line string values (using key<<EOF syntax with a unique EOF marker).
- Converts hashtables and PSCustomObject values to compressed JSON strings.
.EXAMPLE
$object = [PSCustomObject]@{
zen = 'something else'
result = [PSCustomObject]@{ MyOutput = "Hello, World!"; Status = "Success" }
MY_VALUE = "Line1`nLine2`nLine3"
Config = @{ Setting1 = "Value1"; Setting2 = 2; Nested = @{ SubSetting = "SubValue" } }
Numbers = 12345
}
$object | ConvertTo-GitHubOutput
zen=something else
result={"MyOutput":"Hello, World!","Status":"Success"}
MY_VALUE<<EOF_12a089b9-051e-4c4e-91c9-8e24fc2fbbf6
Line1
Line2
Line3
EOF_12a089b9-051e-4c4e-91c9-8e24fc2fbbf6
Config={"Nested":{"SubSetting":"SubValue"},"Setting1":"Value1","Setting2":2}
Numbers=12345
This will convert the properties of $object to GitHub Actions output syntax.
#>
[OutputType([string])]
[CmdletBinding()]
param(
# The PowerShell object containing the key-value pairs to be saved.
# Each property of the object represents a key.
[Parameter(
Mandatory,
ValueFromPipeline
)]
[object] $InputObject
)

$outputLines = @()

if ($InputObject -is [hashtable]) {
$InputObject = [PSCustomObject]$InputObject
}

foreach ($property in $InputObject.PSObject.Properties) {
$key = $property.Name
$value = $property.Value

# Convert hashtable or PSCustomObject to compressed JSON
if ($value -is [hashtable] -or $value -is [PSCustomObject]) {
$value = $value | ConvertTo-Json -Compress
}

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"
}
}
$outputLines
}
48 changes: 48 additions & 0 deletions src/functions/public/Commands/Get-GitHubOutput.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function Get-GitHubOutput {
<#
.SYNOPSIS
Gets the GitHub output.
.DESCRIPTION
Gets the GitHub output from $env:GITHUB_OUTPUT and creates an object with key-value pairs, supporting both single-line and multi-line values
.EXAMPLE
Get-GitHubOutput
MY_VALUE result zen
-------- ------ ---
qwe… @{"MyOutput":"Hello, World!"} something else
Gets the GitHub output and returns an object with key-value pairs.
.EXAMPLE
Get-GitHubOutput -AsHashtable
Name Value
---- -----
MyArray 1 2 3
MyOutput Hello, World!
zen something else
result {[thisisatest, a simple value]}
mystuff {[MyOutput, Hello, World!]}
MY_VALUE qwe…
Gets the GitHub output and returns a hashtable.
#>
[OutputType([pscustomobject])]
[CmdletBinding()]
param(
# Returns the output as a hashtable.
[Parameter()]
[switch] $AsHashtable,

# The path to the GitHub output file.
[Parameter()]
[string] $Path = $env:GITHUB_OUTPUT
)

if (-not (Test-Path -Path $Path)) {
throw "File not found: $Path"
}

Get-Content -Path $Path | ConvertFrom-GitHubOutput -AsHashtable:$AsHashtable
}
12 changes: 6 additions & 6 deletions src/functions/public/Commands/Set-GitHubEnvironmentVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@

Write-Verbose "Env: [$Name] = [$Value]"

$Value = $Value.Split([System.Environment]::NewLine)
$guid = [guid]::NewGuid().Guid
"$Name<<$guid" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
$Value | ForEach-Object {
$_ | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
}
"$guid" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
$content = @"
$Name<<$guid
$Value
$guid
"@
$content | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
}
Loading

0 comments on commit ea2ddb0

Please sign in to comment.