Skip to content

Commit 9ad6865

Browse files
🚀 [Feature]: Improve error handling and parallel processing in Connect-GitHubApp
1 parent 924cf38 commit 9ad6865

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

‎src/functions/public/Auth/Connect-GitHubApp.ps1‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@
122122
HttpVersion = [string]$Context.HttpVersion
123123
PerPage = [int]$Context.PerPage
124124
ClientID = [string]$Context.ClientID
125-
InstallationID = [string]$installation.ID
126-
Permissions = [GitHubPermission[]]$installation.Permissions
127-
Events = [string[]]$installation.Events
128-
InstallationType = [string]$installation.Type
125+
InstallationID = [string]$Installation.ID
126+
Permissions = [GitHubPermission[]]$Installation.Permissions
127+
Events = [string[]]$Installation.Events
128+
InstallationType = [string]$Installation.Type
129129
Token = [securestring]$token.Token
130130
TokenExpiresAt = [datetime]$token.ExpiresAt
131131
}
132132

133-
switch ($installation.Type) {
133+
switch ($Installation.Type) {
134134
'User' {
135135
$contextParams['InstallationName'] = [string]$installation.Target.Name
136136
$contextParams['Owner'] = [string]$installation.Target.Name
@@ -146,7 +146,19 @@
146146
}
147147
Write-Verbose 'Logging in using a managed installation access token...'
148148
$contextParams | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
149-
$contextObj = [GitHubAppInstallationContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default))
149+
while ($true) {
150+
try {
151+
$contextObj = [GitHubAppInstallationContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default))
152+
} catch {
153+
if ($attempts -lt 3) {
154+
$attempts++
155+
Write-Warning "Failed to create context. Retrying... [$attempts]"
156+
Start-Sleep -Seconds (1 * $attempts)
157+
} else {
158+
throw $_
159+
}
160+
}
161+
}
150162
$contextObj | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
151163
if (-not $Silent) {
152164
$name = $contextObj.Name
@@ -166,13 +178,15 @@
166178
return
167179
}
168180

169-
$Installation | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
181+
$Installation | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
182+
Write-Host "Using GitHub $($script:PSModuleInfo.ModuleVersion)"
183+
Import-Module -Name 'GitHub' -RequiredVersion $script:PSModuleInfo.ModuleVersion
170184
$params = @{
171-
ID = $_.ID
172-
Context = $using:Context
173-
PassThru = $using:PassThru
174-
Silent = $using:Silent
175-
Default = $using:Default
185+
Installation = $_
186+
Context = $using:Context
187+
PassThru = $using:PassThru
188+
Silent = $using:Silent
189+
Default = $using:Default
176190
}
177191
Connect-GitHubApp @params
178192
}
@@ -203,13 +217,14 @@
203217
$_.Type -eq 'Enterprise' -and $_.Target.Name -like $enterpriseItem
204218
}
205219
}
206-
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
220+
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
221+
Import-Module -Name 'GitHub' -RequiredVersion $script:PSModuleInfo.ModuleVersion -Force
207222
$params = @{
208-
ID = $_.ID
209-
Context = $using:Context
210-
PassThru = $using:PassThru
211-
Silent = $using:Silent
212-
Default = $using:Default
223+
Installation = $_
224+
Context = $using:Context
225+
PassThru = $using:PassThru
226+
Silent = $using:Silent
227+
Default = $using:Default
213228
}
214229
Connect-GitHubApp @params
215230
}
@@ -218,13 +233,14 @@
218233
'All Installations' {
219234
Write-Verbose 'No target specified. Connecting to all installations.'
220235
$selectedInstallations = Get-GitHubAppInstallation -Context $Context
221-
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
236+
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
237+
Import-Module -Name 'GitHub' -RequiredVersion $script:PSModuleInfo.ModuleVersion -Force
222238
$params = @{
223-
ID = $_.ID
224-
Context = $using:Context
225-
PassThru = $using:PassThru
226-
Silent = $using:Silent
227-
Default = $using:Default
239+
Installation = $_
240+
Context = $using:Context
241+
PassThru = $using:PassThru
242+
Silent = $using:Silent
243+
Default = $using:Default
228244
}
229245
Connect-GitHubApp @params
230246
}

0 commit comments

Comments
 (0)