Skip to content

Commit 2575b03

Browse files
🪲[Fix]: Fix output formatting of Release Assets (#476)
## Description This pull request enhances the handling of `GitHubReleaseAsset` objects and their formatting, improves the readability of file size values, and adds new tests for pipeline functionality. The most important changes include the addition of a file size formatting method, updates to constructors for `GitHubReleaseAsset`, and the creation of a new XML-based table view for release assets. ### Enhancements to `GitHubReleaseAsset` handling * Updated constructors across multiple files to use `[GitHubReleaseAsset]::new` instead of direct object casting, ensuring consistency and clarity in object creation. ### File size formatting improvements * Added a static `FormatFileSize` method to `GitHubFormatter` for converting file sizes into human-readable formats (e.g., KB, MB, GB, TB). ### XML-based table view for release assets * Introduced a new XML view definition (`GitHubReleaseAssetTable`) to display release asset details in a tabular format, including columns for name, state, size, downloads, creation date, and update date. ### Improved datetime handling * Simplified `CreatedAt` and `UpdatedAt` properties by removing explicit `DateTime.Parse` calls, relying on native datetime parsing. ### New tests for pipeline functionality * Added tests to verify that `Get-GitHubReleaseAsset` retrieves assets correctly when used in a pipeline, validating properties such as ID, name, size, and datetime fields. ## 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. --------- Co-authored-by: Copilot <[email protected]>
1 parent aa23533 commit 2575b03

File tree

8 files changed

+114
-9
lines changed

8 files changed

+114
-9
lines changed

src/classes/public/GitHubFormatter.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
$reset = "`e[0m"
2020
return "$color$Text$reset"
2121
}
22+
23+
static [string] FormatFileSize([long]$size) {
24+
switch ($size) {
25+
{ $_ -ge 1TB } { return '{0:N2} TB' -f ($size / 1TB) }
26+
{ $_ -ge 1GB } { return '{0:N2} GB' -f ($size / 1GB) }
27+
{ $_ -ge 1MB } { return '{0:N2} MB' -f ($size / 1MB) }
28+
{ $_ -ge 1KB } { return '{0:N2} KB' -f ($size / 1KB) }
29+
}
30+
return "$size B"
31+
}
2232
}

src/classes/public/Releases/GitHubReleaseAsset.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
$this.ContentType = $Object.content_type
5353
$this.Size = $Object.size
5454
$this.Downloads = $Object.download_count
55-
$this.CreatedAt = [datetime]::Parse($Object.created_at)
56-
$this.UpdatedAt = [datetime]::Parse($Object.updated_at)
55+
$this.CreatedAt = $Object.created_at
56+
$this.UpdatedAt = $Object.updated_at
5757
$this.UploadedBy = [GitHubUser]::new($Object.uploader)
5858
} else {
5959
$this.NodeID = $Object.id
@@ -62,8 +62,8 @@
6262
$this.ContentType = $Object.contentType
6363
$this.Size = $Object.size
6464
$this.Downloads = $Object.downloadCount
65-
$this.CreatedAt = [datetime]::Parse($Object.createdAt)
66-
$this.UpdatedAt = [datetime]::Parse($Object.updatedAt)
65+
$this.CreatedAt = $Object.createdAt
66+
$this.UpdatedAt = $Object.updatedAt
6767
$this.UploadedBy = [GitHubUser]::new($Object.uploadedBy)
6868
}
6969
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<View>
5+
<Name>GitHubReleaseAssetTable</Name>
6+
<ViewSelectedBy>
7+
<TypeName>GitHubReleaseAsset</TypeName>
8+
</ViewSelectedBy>
9+
<TableControl>
10+
<TableHeaders>
11+
<TableColumnHeader>
12+
<Label>Name</Label>
13+
</TableColumnHeader>
14+
<TableColumnHeader>
15+
<Label>State</Label>
16+
</TableColumnHeader>
17+
<TableColumnHeader>
18+
<Label>Size</Label>
19+
</TableColumnHeader>
20+
<TableColumnHeader>
21+
<Label>Downloads</Label>
22+
</TableColumnHeader>
23+
<TableColumnHeader>
24+
<Label>CreatedAt</Label>
25+
</TableColumnHeader>
26+
<TableColumnHeader>
27+
<Label>UpdatedAt</Label>
28+
</TableColumnHeader>
29+
</TableHeaders>
30+
<TableRowEntries>
31+
<TableRowEntry>
32+
<TableColumnItems>
33+
<TableColumnItem>
34+
<ScriptBlock>
35+
if ($Host.UI.SupportsVirtualTerminal -and
36+
($env:GITHUB_ACTIONS -ne 'true')) {
37+
$PSStyle.FormatHyperlink($_.Name,$_.Url)
38+
} else {
39+
$_.Name
40+
}
41+
</ScriptBlock>
42+
</TableColumnItem>
43+
<TableColumnItem>
44+
<PropertyName>State</PropertyName>
45+
</TableColumnItem>
46+
<TableColumnItem>
47+
<ScriptBlock>
48+
[GitHubFormatter]::FormatFileSize($_.Size)
49+
</ScriptBlock>
50+
<Alignment>Right</Alignment>
51+
</TableColumnItem>
52+
<TableColumnItem>
53+
<PropertyName>Downloads</PropertyName>
54+
<Alignment>Right</Alignment>
55+
</TableColumnItem>
56+
<TableColumnItem>
57+
<PropertyName>CreatedAt</PropertyName>
58+
</TableColumnItem>
59+
<TableColumnItem>
60+
<PropertyName>UpdatedAt</PropertyName>
61+
</TableColumnItem>
62+
</TableColumnItems>
63+
</TableRowEntry>
64+
</TableRowEntries>
65+
</TableControl>
66+
</View>
67+
</ViewDefinitions>
68+
</Configuration>

src/functions/private/Releases/Assets/Get-GitHubReleaseAssetByID.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
}
5656

5757
Invoke-GitHubAPI @apiParams | ForEach-Object {
58-
[GitHubReleaseAsset]($_.Response)
58+
[GitHubReleaseAsset]::new($_.Response)
5959
}
6060
}
6161

src/functions/private/Releases/Assets/Get-GitHubReleaseAssetByReleaseID.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
foreach ($asset in $_.Response) {
6767
if ($PSBoundParameters.ContainsKey('Name')) {
6868
if ($asset.name -eq $Name) {
69-
[GitHubReleaseAsset]($asset)
69+
[GitHubReleaseAsset]::new($asset)
7070
break
7171
}
7272
} else {
73-
[GitHubReleaseAsset]($asset)
73+
[GitHubReleaseAsset]::new($asset)
7474
}
7575
}
7676
}

src/functions/public/Releases/Assets/Add-GitHubReleaseAsset.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
}
182182

183183
Invoke-GitHubAPI @apiParams | ForEach-Object {
184-
[GitHubReleaseAsset]($_.Response)
184+
[GitHubReleaseAsset]::new($_.Response)
185185
}
186186
}
187187

src/functions/public/Releases/Assets/Update-GitHubReleaseAsset.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
if ($PSCmdlet.ShouldProcess("assets for release with ID [$ID] in [$Owner/$Repository]", 'Set')) {
8585
Invoke-GitHubAPI @apiParams | ForEach-Object {
86-
[GitHubReleaseAsset]($_.Response)
86+
[GitHubReleaseAsset]::new($_.Response)
8787
}
8888
}
8989
}

tests/Releases.Tests.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,33 @@ ID,Name,Value
644644
Test-Path -Path $downloadedFile.FullName | Should -BeTrue
645645
$downloadedFile.Name | Should -Be $asset.Name
646646
}
647+
648+
It 'Get-GitHubReleaseAsset - Gets assets from release using pipeline' {
649+
$assets = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts | Get-GitHubReleaseAsset
650+
LogGroup 'Release assets from pipeline' {
651+
Write-Host ($assets | Format-List -Property * | Out-String)
652+
}
653+
$assets | Should -Not -BeNullOrEmpty
654+
$assets.Count | Should -BeGreaterThan 0
655+
foreach ($asset in $assets) {
656+
$asset | Should -BeOfType [GitHubReleaseAsset]
657+
$asset.ID | Should -Not -BeNullOrEmpty
658+
$asset.NodeID | Should -Not -BeNullOrEmpty
659+
$asset.Url | Should -Not -BeNullOrEmpty
660+
$asset.Name | Should -Not -BeNullOrEmpty
661+
# $asset.Label | Should -Not -BeNullOrEmpty - Label is optional and may not be set
662+
$asset.State | Should -Be 'uploaded'
663+
$asset.ContentType | Should -Not -BeNullOrEmpty
664+
$asset.Size | Should -BeGreaterOrEqual 0
665+
$asset.Downloads | Should -BeGreaterOrEqual 0
666+
$asset.CreatedAt | Should -Not -BeNullOrEmpty
667+
$asset.CreatedAt | Should -BeOfType 'DateTime'
668+
$asset.UpdatedAt | Should -Not -BeNullOrEmpty
669+
$asset.UpdatedAt | Should -BeOfType 'DateTime'
670+
$asset.UploadedBy | Should -Not -BeNullOrEmpty
671+
$asset.UploadedBy | Should -BeOfType 'GitHubUser'
672+
}
673+
}
647674
}
648675
}
649676
}

0 commit comments

Comments
 (0)