From 33908ef0b177e1bce488c6df10ddae2a66657153 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 24 Dec 2024 13:18:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20verbose=20loggi?= =?UTF-8?q?ng=20for=20emoji=20download=20process=20in=20Get-GitHubEmoji=20?= =?UTF-8?q?function=20(#236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This pull request includes significant changes to the `Get-GitHubEmoji` function in `src/functions/public/Emojis/Get-GitHubEmoji.ps1` and updates to the corresponding tests in `tests/GitHub.Tests.ps1`. The changes improve parameter handling, enhance error handling, and update test cases. Improvements to `Get-GitHubEmoji` function: * Added `DefaultParameterSetName` to `CmdletBinding` to ensure the correct parameter set is used by default. * Updated the `Destination` parameter to be mandatory when using the 'Download' parameter set. * Enhanced the response handling by using `Select-Object -ExpandProperty Response` for cleaner code. * Improved the download logic with better error handling and retry mechanisms for failed downloads. Updates to test cases: * Changed the destination for downloading emojis in tests from `$env:TEMP` to `$Home` to ensure compatibility across different environments. [[1]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL333-R334) [[2]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL454-R454) [[3]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL574-R574) [[4]](diffhunk://#diff-0b1d9ba345a583adce874126c13d6edd3f789416bb9c4db5df1e18af3608554cL681-R681) ## Type of change - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --- .../public/Emojis/Get-GitHubEmoji.ps1 | 32 ++++++++++++++----- tests/GitHub.Tests.ps1 | 10 +++--- 2 files changed, 29 insertions(+), 13 deletions(-) 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' {