Skip to content

Commit 63c95b3

Browse files
🪲 [Fix]: Get-GitHubContext no longer fails when there are no contexts to return (#402)
## Description This pull request includes improvements to `Get-GitHubContext`, replacing a thrown exception with a warning, causing it not error out if there are no contexts to return. ### Error Handling Improvements: * Replaced the thrown exception for missing default GitHub context with a warning message and an early return. This prevents the script from terminating unexpectedly. * Added a new test case to verify that `Get-GitHubContext` does not throw an exception when no contexts are available and returns an empty result. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [x] 🪲 [Fix] - [ ] 🩹 [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
1 parent 31e17d6 commit 63c95b3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/functions/public/Auth/Context/Get-GitHubContext.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
Write-Debug 'Getting default context.'
5858
$ID = $script:GitHub.Config.DefaultContext
5959
if ([string]::IsNullOrEmpty($ID)) {
60-
throw "No default GitHub context found. Please run 'Switch-GitHubContext' or 'Connect-GitHub' to configure a GitHub context."
60+
$msg = "No default GitHub context found. Please run 'Switch-GitHubContext' or 'Connect-GitHub' to configure a GitHub context."
61+
Write-Warning $msg
62+
return
6163
}
6264
}
6365
}

tests/GitHub.Tests.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ Describe 'Auth' {
122122
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent
123123
(Get-GitHubContext -ListAvailable).count | Should -Be 0
124124
}
125+
126+
It 'Get-GitHubContext - Does not fail when there are 0 contexts' {
127+
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent
128+
{ Get-GitHubContext } | Should -Not -Throw
129+
$contexts = Get-GitHubContext
130+
$contexts | Should -BeNullOrEmpty
131+
}
125132
}
126133
}
127134

@@ -750,7 +757,7 @@ Describe 'Webhooks' {
750757

751758
Describe 'Anonymous - Functions that can run anonymously' {
752759
It 'Get-GithubRateLimit - Using -Anonymous' {
753-
$rateLimit = Get-GitHubRateLimit -Anonymous -Debug -Verbose
760+
$rateLimit = Get-GitHubRateLimit -Anonymous
754761
LogGroup 'Rate Limit' {
755762
Write-Host ($rateLimit | Format-Table | Out-String)
756763
}
@@ -764,7 +771,7 @@ Describe 'Anonymous - Functions that can run anonymously' {
764771
$rateLimit | Should -Not -BeNullOrEmpty
765772
}
766773
It 'Get-GithubRateLimit - Using -Context Anonymous' {
767-
$rateLimit = Get-GitHubRateLimit -Context Anonymous -Debug -Verbose
774+
$rateLimit = Get-GitHubRateLimit -Context Anonymous
768775
LogGroup 'Rate Limit' {
769776
Write-Host ($rateLimit | Format-List | Out-String)
770777
}

0 commit comments

Comments
 (0)