Skip to content
32 changes: 24 additions & 8 deletions src/functions/public/Emojis/Get-GitHubEmoji.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down