diff --git a/src/functions/public/Emojis/Get-GitHubEmoji.ps1 b/src/functions/public/Emojis/Get-GitHubEmoji.ps1 index 3db00fbb4..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. @@ -47,13 +50,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 { - Invoke-WebRequest -Uri $_.Value -OutFile "$using:Destination/$($_.Name).png" + if ($PSCmdlet.ParameterSetName -eq 'Download') { + $failedEmojis = @() + if (-not (Test-Path -Path $Destination)) { + $null = New-Item -Path $Destination -ItemType Directory -Force + } + $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]" + 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..f61242e45 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -330,8 +330,8 @@ 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' { - { Get-GitHubEmoji -Destination $env:TEMP } | Should -Not -Throw + It 'Can download the emojis' { + { Get-GitHubEmoji -Destination $Home } | Should -Not -Throw } } Context 'Repository' { @@ -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' {