Skip to content

Commit a4e8543

Browse files
🚀 [Enhancement]: Add ThrottleLimit parameter to Connect-GitHubApp for improved parallel processing of installations. Update context handling to utilize using scope for better variable access in parallel execution.
1 parent 0fc891e commit a4e8543

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
[Parameter()]
8282
[switch] $Default,
8383

84+
# The maximum number of parallel threads to use when connecting to multiple installations.
85+
[Parameter()]
86+
[int] $ThrottleLimit = [System.Environment]::ProcessorCount,
87+
8488
# The context to run the command in. Used to get the details for the API call.
8589
# Can be either a string or a GitHubContext object.
8690
[Parameter()]
@@ -92,6 +96,7 @@
9296
Write-Debug "[$stackPath] - Start"
9397
$Context = Resolve-GitHubContext -Context $Context
9498
Assert-GitHubContext -Context $Context -AuthType App
99+
$contextObjects = [System.Collections.ArrayList]::new()
95100
}
96101

97102
process {
@@ -151,21 +156,22 @@
151156
}
152157

153158
Write-Verbose "Found [$($selectedInstallations.Count)] installations for the target."
154-
$selectedInstallations | ForEach-Object {
159+
$contextParamList = $selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
160+
Import-Module -Name $script:PSModuleInfo.Name -RequiredVersion $script:PSModuleInfo.Version -Force -ErrorAction Stop
155161
$installation = $_
156162
Write-Verbose "Processing installation [$($installation.Target.Name)] [$($installation.id)]"
157163
$token = New-GitHubAppInstallationAccessToken -Context $Context -ID $installation.id
158164

159165
$contextParams = @{
160166
AuthType = [string]'IAT'
161167
TokenType = [string]'ghs'
162-
DisplayName = [string]$Context.DisplayName
163-
ApiBaseUri = [string]$Context.ApiBaseUri
164-
ApiVersion = [string]$Context.ApiVersion
165-
HostName = [string]$Context.HostName
166-
HttpVersion = [string]$Context.HttpVersion
167-
PerPage = [int]$Context.PerPage
168-
ClientID = [string]$Context.ClientID
168+
DisplayName = [string]$using:Context.DisplayName
169+
ApiBaseUri = [string]$using:Context.ApiBaseUri
170+
ApiVersion = [string]$using:Context.ApiVersion
171+
HostName = [string]$using:Context.HostName
172+
HttpVersion = [string]$using:Context.HttpVersion
173+
PerPage = [int]$using:Context.PerPage
174+
ClientID = [string]$using:Context.ClientID
169175
InstallationID = [string]$installation.ID
170176
Permissions = [GitHubPermission[]]$installation.Permissions
171177
Events = [string[]]$installation.Events
@@ -188,6 +194,14 @@
188194
$contextParams['Enterprise'] = [string]$installation.Target.Name
189195
}
190196
}
197+
$contextParams
198+
}
199+
$contextObjects.AddRange($contextParamList)
200+
$null = $selectedInstallations.Clear()
201+
}
202+
203+
end {
204+
foreach ($contextParams in $contextObjects) {
191205
Write-Verbose 'Logging in using a managed installation access token...'
192206
$contextParams | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
193207
$contextObj = [GitHubAppInstallationContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default))
@@ -204,9 +218,6 @@
204218
}
205219
$contextParams.Clear()
206220
}
207-
}
208-
209-
end {
210221
Write-Debug "[$stackPath] - End"
211222
}
212223
}

0 commit comments

Comments
 (0)