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

🪲 [Fix]: Fix coverage reporting and some GHA commands #174

Merged
merged 45 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
91e8b62
Fix coverage report
MariusStorhaug Dec 1, 2024
ec5109e
gitadd all :)
MariusStorhaug Dec 1, 2024
9e7c3d3
test
MariusStorhaug Dec 1, 2024
3107063
🩹 [Patch]: Improve error handling in user authentication
MariusStorhaug Dec 1, 2024
4eae7ce
test
MariusStorhaug Dec 1, 2024
6244137
test
MariusStorhaug Dec 1, 2024
fdfb070
test
MariusStorhaug Dec 1, 2024
9edb972
test
MariusStorhaug Dec 1, 2024
b088881
test
MariusStorhaug Dec 1, 2024
2274f76
test
MariusStorhaug Dec 1, 2024
43b266d
test
MariusStorhaug Dec 1, 2024
cfb8d04
test
MariusStorhaug Dec 1, 2024
225c38f
Check precense of coverage.md
MariusStorhaug Dec 1, 2024
ec01f59
Refactor coverage report workflow and script for improved clarity and…
MariusStorhaug Dec 1, 2024
1b93e58
test
MariusStorhaug Dec 1, 2024
c3a387a
test
MariusStorhaug Dec 1, 2024
0acc78d
test
MariusStorhaug Dec 1, 2024
32e45b2
Auto-generated changes
github-actions[bot] Dec 1, 2024
5591925
test
MariusStorhaug Dec 1, 2024
1f622ee
Auto-generated changes
github-actions[bot] Dec 1, 2024
b2216f5
Refactor Set-CoverageReport.ps1 to clean up whitespace and improve gi…
MariusStorhaug Dec 1, 2024
f87615e
Add logging for git diff command in Set-CoverageReport.ps1
MariusStorhaug Dec 1, 2024
c48be9a
Auto-generated changes
github-actions[bot] Dec 1, 2024
5192f7a
Remove unused logging and HTML table from Coverage.md
MariusStorhaug Dec 1, 2024
0a16dd5
Auto-generated changes
github-actions[bot] Dec 1, 2024
6ee25da
Have git config be local
MariusStorhaug Dec 1, 2024
89d1560
dont trigger on pull req, as its detatched
MariusStorhaug Dec 1, 2024
9012f86
Add a step summary
MariusStorhaug Dec 1, 2024
33732c4
test
MariusStorhaug Dec 1, 2024
d3af3fe
Auto-generated changes
github-actions[bot] Dec 1, 2024
56aa559
test
MariusStorhaug Dec 1, 2024
b3a64c3
Fixes for Env and output
MariusStorhaug Dec 1, 2024
ca68e3c
Add Get-GitHubOutput function to retrieve GitHub output as key-value …
MariusStorhaug Dec 1, 2024
289b570
Add ConvertFrom-GitHubOutput and ConvertTo-GitHubOutput functions for…
MariusStorhaug Dec 1, 2024
345e343
Remove commented-out example code from ConvertFrom-GitHubOutput function
MariusStorhaug Dec 1, 2024
a91f444
Refactor ConvertTo-GitHubOutput function to simplify parameters and e…
MariusStorhaug Dec 1, 2024
0465cc4
Return output lines from ConvertTo-GitHubOutput function for improved…
MariusStorhaug Dec 1, 2024
7c13e2b
Enhance ConvertFrom-GitHubOutput and ConvertTo-GitHubOutput functions…
MariusStorhaug Dec 1, 2024
affaeab
Add support for hashtable conversion in ConvertFrom-GitHubOutput and …
MariusStorhaug Dec 1, 2024
8e88e9e
Refactor Get-GitHubOutput and Set-GitHubOutput functions to accept a …
MariusStorhaug Dec 1, 2024
3996a1f
Enhance documentation for Get-GitHubOutput and Set-GitHubOutput funct…
MariusStorhaug Dec 1, 2024
add1690
Update output type annotations in ConvertFrom-GitHubOutput to include…
MariusStorhaug Dec 1, 2024
e849b73
Update Set-GitHubOutput examples to use consistent casing for 'ID'
MariusStorhaug Dec 1, 2024
a05cc5a
Remove fetch-depth option from checkout step in Set-CoverageReport wo…
MariusStorhaug Dec 1, 2024
a28365e
Replace Out-File with Set-GitHubStepSummary for setting coverage repo…
MariusStorhaug Dec 1, 2024
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
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
}
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
Loading