From 6bf605eacd122a0760527023217fbef34c734bf6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 23 Dec 2024 23:47:45 +0100 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20verbose=20l?= =?UTF-8?q?ogging=20for=20emoji=20download=20process=20in=20Get-GitHubEmoj?= =?UTF-8?q?i=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Emojis/Get-GitHubEmoji.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index 3db00fbb4..0c4d465e8 100644 --- a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 +++ b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 @@ -53,6 +53,7 @@ if (Test-Path -Path $Destination) { $response.PSObject.Properties | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel { + Write-Verbose "Downloading [$($_.Name).png] to [$using:Destination/$($_.Name).png]" Invoke-WebRequest -Uri $_.Value -OutFile "$using:Destination/$($_.Name).png" } } else { From 15234ba7197fa7d3df01009e5ba5611ab4c7e095 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 10:07:53 +0100 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Enhance=20emoji?= =?UTF-8?q?=20download=20process=20with=20error=20handling=20and=20verbose?= =?UTF-8?q?=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../public/Emojis/Get-GitHubEmoji.ps1 | 26 ++++++++++++++----- tests/GitHub.Tests.ps1 | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index 0c4d465e8..33ab72cf2 100644 --- a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 +++ b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 @@ -47,14 +47,26 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject | ForEach-Object { - Write-Output $_.Response - } + $response = Invoke-GitHubAPI @inputObject | Select-Object -ExpandProperty Response - if (Test-Path -Path $Destination) { - $response.PSObject.Properties | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel { - Write-Verbose "Downloading [$($_.Name).png] to [$using:Destination/$($_.Name).png]" - Invoke-WebRequest -Uri $_.Value -OutFile "$using:Destination/$($_.Name).png" + if ($PSBoundParameters.ContainsKey('Destination')) { + $failedEmojis = @() + if (-not (Test-Path -Path $Destination)) { + New-Item -Path $Destination -ItemType Directory -Force | Out-Null + } + $failedEmojis = $response.PSObject.Properties | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel { + $emoji = $_ + Write-Verbose "Downloading [$($emoji.Name).png] from [$($emoji.Value)] -> [$using:Destination/$($emoji.Name).png]" -Verbose + try { + Invoke-WebRequest -Uri $emoji.Value -OutFile "$using:Destination/$($emoji.Name).png" -RetryIntervalSec 1 -MaximumRetryCount 5 + } catch { + $emoji + Write-Warning "Could not download [$($emoji.Name).png] from [$($emoji.Value)] -> [$using:Destination/$($emoji.Name).png]" + } + } + if ($failedEmojis.Count -gt 0) { + Write-Warning "Failed to download the following emojis:" + $failedEmojis | Out-String -Stream | ForEach-Object { Write-Warning $_ } } } else { $response diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 29c3be9cf..ed8c349d0 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -330,7 +330,7 @@ Describe 'As a user - Fine-grained PAT token - user account access' { It 'Can be called with no parameters' { { Get-GitHubEmoji } | Should -Not -Throw } - It 'Can be download the emojis' { + It 'Can download the emojis' { { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw } } From 5823dca6a40251cfacf860dd59105ca00814e9a9 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 10:15:04 +0100 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Remove=20redundan?= =?UTF-8?q?t=20verbose=20flag=20in=20emoji=20download=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Emojis/Get-GitHubEmoji.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index 33ab72cf2..f77c4fd4c 100644 --- a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 +++ b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 @@ -56,7 +56,7 @@ } $failedEmojis = $response.PSObject.Properties | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel { $emoji = $_ - Write-Verbose "Downloading [$($emoji.Name).png] from [$($emoji.Value)] -> [$using:Destination/$($emoji.Name).png]" -Verbose + Write-Verbose "Downloading [$($emoji.Name).png] from [$($emoji.Value)] -> [$using:Destination/$($emoji.Name).png]" try { Invoke-WebRequest -Uri $emoji.Value -OutFile "$using:Destination/$($emoji.Name).png" -RetryIntervalSec 1 -MaximumRetryCount 5 } catch { From 2e501db9e108d7d4b9ab37c83c0d38c4a393ad1c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 10:15:51 +0100 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Simplify=20direct?= =?UTF-8?q?ory=20creation=20by=20suppressing=20output=20in=20Get-GitHubEmo?= =?UTF-8?q?ji=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Emojis/Get-GitHubEmoji.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index f77c4fd4c..3d279069f 100644 --- a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 +++ b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 @@ -52,7 +52,7 @@ if ($PSBoundParameters.ContainsKey('Destination')) { $failedEmojis = @() if (-not (Test-Path -Path $Destination)) { - New-Item -Path $Destination -ItemType Directory -Force | Out-Null + $null = New-Item -Path $Destination -ItemType Directory -Force } $failedEmojis = $response.PSObject.Properties | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel { $emoji = $_ From 940354c36551e24e45f8b2092d4c97f08ee02700 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 10:55:16 +0100 Subject: [PATCH 5/7] Update parameter binding for Get-GitHubEmoji function to enforce mandatory destination in download context --- src/functions/public/Emojis/Get-GitHubEmoji.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index 3d279069f..e8e61bbee 100644 --- a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 +++ b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 @@ -20,10 +20,13 @@ .NOTES [Get emojis](https://docs.github.com/rest/reference/emojis#get-emojis) #> - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] param( # The path to the directory where the emojis will be downloaded. - [Parameter()] + [Parameter( + Mandatory, + ParameterSetName = 'Download' + )] [string] $Destination, # The context to run the command in. Used to get the details for the API call. @@ -49,7 +52,7 @@ $response = Invoke-GitHubAPI @inputObject | Select-Object -ExpandProperty Response - if ($PSBoundParameters.ContainsKey('Destination')) { + if ($PSCmdlet.ParameterSetName -eq 'Download') { $failedEmojis = @() if (-not (Test-Path -Path $Destination)) { $null = New-Item -Path $Destination -ItemType Directory -Force @@ -65,7 +68,7 @@ } } if ($failedEmojis.Count -gt 0) { - Write-Warning "Failed to download the following emojis:" + Write-Warning 'Failed to download the following emojis:' $failedEmojis | Out-String -Stream | ForEach-Object { Write-Warning $_ } } } else { From b91de0046f646640f7b7bb091dcf704ad7673748 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 11:53:06 +0100 Subject: [PATCH 6/7] Update emoji download test to use user home directory as destination --- tests/GitHub.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index ed8c349d0..80991976a 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -331,7 +331,7 @@ Describe 'As a user - Fine-grained PAT token - user account access' { { Get-GitHubEmoji } | Should -Not -Throw } It 'Can download the emojis' { - { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw + { Get-GitHubEmoji -Destination $Home } | Should -Not -Throw } } Context 'Repository' { From e1da61b403a18a3bea760436aa4ee9b6354c5720 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 12:44:37 +0100 Subject: [PATCH 7/7] Update emoji download tests to use user home directory as destination --- tests/GitHub.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 80991976a..f61242e45 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -451,7 +451,7 @@ Describe 'As a user - Fine-grained PAT token - organization account access' { { Get-GitHubEmoji } | Should -Not -Throw } It 'Can be download the emojis' { - { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw + { Get-GitHubEmoji -Destination $Home } | Should -Not -Throw } } Context 'Repository' { @@ -571,7 +571,7 @@ Describe 'As a user - Classic PAT token' { { Get-GitHubEmoji } | Should -Not -Throw } It 'Can be download the emojis' { - { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw + { Get-GitHubEmoji -Destination $Home } | Should -Not -Throw } } Context 'GitIgnore' { @@ -678,7 +678,7 @@ Describe 'As GitHub Actions' { { Get-GitHubEmoji } | Should -Not -Throw } It 'Can be download the emojis' { - { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw + { Get-GitHubEmoji -Destination $Home } | Should -Not -Throw } } Context 'GitIgnore' {