Skip to content

Commit 0fc891e

Browse files
Milemarker 51
1 parent d893b88 commit 0fc891e

File tree

3 files changed

+70
-222
lines changed

3 files changed

+70
-222
lines changed

src/classes/public/App/GitHubAppInstallation.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
$this.SuspendedAt = $Object.suspended_at
7474
$this.SuspendedBy = [GitHubUser]::new($Object.suspended_by)
7575
$this.Url = $Object.html_url
76-
$this.Status = 'Unknown'
76+
$this.Status = $Object.Status ?? 'Unknown'
7777
}
7878

7979
GitHubAppInstallation([PSCustomObject] $Object, [GitHubApp] $App) {

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

Lines changed: 22 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
.DESCRIPTION
77
Connects to GitHub using a GitHub App to generate installation access tokens and create contexts for targets.
8-
This function supports recursive processing and parallel connections to multiple installations.
98
109
Available target types:
1110
- User
@@ -15,7 +14,7 @@
1514
.EXAMPLE
1615
Connect-GitHubApp
1716
18-
Connects to GitHub as all available targets using the logged in GitHub App in parallel.
17+
Connects to GitHub as all available targets using the logged in GitHub App.
1918
2019
.EXAMPLE
2120
Connect-GitHubApp -User 'octocat'
@@ -32,16 +31,6 @@
3231
3332
Connects to GitHub as the enterprise 'msx' using the logged in GitHub App.
3433
35-
.EXAMPLE
36-
Get-GitHubAppInstallation | Connect-GitHubApp -ThrottleLimit 4
37-
38-
Gets all app installations and connects to them in parallel with a maximum of 4 concurrent connections.
39-
40-
.EXAMPLE
41-
Connect-GitHubApp -User '*' -Organization 'psmodule', 'github' -ThrottleLimit 8
42-
43-
Connects to all users and the specified organizations in parallel with a maximum of 8 concurrent connections.
44-
4534
.NOTES
4635
[Authenticating to the REST API](https://docs.github.com/rest/overview/other-authentication-methods#authenticating-for-saml-sso)
4736
@@ -55,29 +44,24 @@
5544
[CmdletBinding(DefaultParameterSetName = 'All Installations')]
5645
param(
5746
# The user account to connect to.
58-
[Parameter(ParameterSetName = 'Filtered', ValueFromPipelineByPropertyName)]
47+
[Parameter(ParameterSetName = 'Filtered')]
5948
[SupportsWildcards()]
6049
[string[]] $User,
6150

6251
# The organization to connect to.
63-
[Parameter(ParameterSetName = 'Filtered', ValueFromPipelineByPropertyName)]
52+
[Parameter(ParameterSetName = 'Filtered')]
6453
[SupportsWildcards()]
6554
[string[]] $Organization,
6655

6756
# The enterprise to connect to.
68-
[Parameter(ParameterSetName = 'Filtered', ValueFromPipelineByPropertyName)]
57+
[Parameter(ParameterSetName = 'Filtered')]
6958
[SupportsWildcards()]
7059
[string[]] $Enterprise,
7160

7261
# Installation objects from pipeline for parallel processing.
7362
[Parameter(Mandatory, ParameterSetName = 'Installation object', ValueFromPipeline)]
7463
[GitHubAppInstallation[]] $Installation,
7564

76-
# The maximum number of parallel operations to run at once.
77-
[Parameter(ParameterSetName = 'Filtered')]
78-
[Parameter(ParameterSetName = 'Installation')]
79-
[uint] $ThrottleLimit = ([Environment]::ProcessorCount),
80-
8165
# The installation ID(s) to connect to directly.
8266
# Accepts input from the pipeline by property name (e.g. objects with an ID property)
8367
[Parameter(Mandatory, ParameterSetName = 'Installation ID', ValueFromPipelineByPropertyName)]
@@ -108,115 +92,14 @@
10892
Write-Debug "[$stackPath] - Start"
10993
$Context = Resolve-GitHubContext -Context $Context
11094
Assert-GitHubContext -Context $Context -AuthType App
111-
$selectedInstallations = @()
112-
$moduleVersion = $script:PSModuleInfo.ModuleVersion
11395
}
11496

11597
process {
11698
$selectedInstallations = [System.Collections.ArrayList]::new()
11799
switch ($PSCmdlet.ParameterSetName) {
118-
'Installation' {
119-
if ($Installation.Count -eq 1) {
120-
Write-Verbose "Processing installation [$($Installation.Target.Name)] [$($Installation.ID)]"
121-
$token = New-GitHubAppInstallationAccessToken -Context $Context -ID $Installation.ID
122-
123-
$contextParams = @{
124-
AuthType = [string]'IAT'
125-
TokenType = [string]'ghs'
126-
DisplayName = [string]$Context.DisplayName
127-
ApiBaseUri = [string]$Context.ApiBaseUri
128-
ApiVersion = [string]$Context.ApiVersion
129-
HostName = [string]$Context.HostName
130-
HttpVersion = [string]$Context.HttpVersion
131-
PerPage = [int]$Context.PerPage
132-
ClientID = [string]$Context.ClientID
133-
InstallationID = [string]$Installation.ID
134-
Permissions = [GitHubPermission[]]$Installation.Permissions
135-
Events = [string[]]$Installation.Events
136-
InstallationType = [string]$Installation.Type
137-
Token = [securestring]$token.Token
138-
TokenExpiresAt = [datetime]$token.ExpiresAt
139-
}
140-
141-
switch ($Installation.Type) {
142-
'User' {
143-
$contextParams['InstallationName'] = [string]$Installation.Target.Name
144-
$contextParams['Owner'] = [string]$Installation.Target.Name
145-
}
146-
'Organization' {
147-
$contextParams['InstallationName'] = [string]$Installation.Target.Name
148-
$contextParams['Owner'] = [string]$Installation.Target.Name
149-
}
150-
'Enterprise' {
151-
$contextParams['InstallationName'] = [string]$Installation.Target.Name
152-
$contextParams['Enterprise'] = [string]$Installation.Target.Name
153-
}
154-
}
155-
Write-Verbose 'Logging in using a managed installation access token...'
156-
$contextParams | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
157-
$attempts = 0
158-
while ($true) {
159-
try {
160-
$contextObj = [GitHubAppInstallationContext]::new(
161-
(Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default)
162-
)
163-
break
164-
} catch {
165-
if ($attempts -lt 3) {
166-
$attempts++
167-
Write-Warning "Failed to create context. Retrying... [$attempts]"
168-
Start-Sleep -Seconds (1 * $attempts)
169-
} else {
170-
throw $_
171-
}
172-
}
173-
}
174-
if ($VerbosePreference -eq 'Continue') {
175-
$contextObj | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
176-
}
177-
if (-not $Silent) {
178-
$name = $contextObj.Name
179-
$green = $PSStyle.Foreground.BrightGreen
180-
$reset = $PSStyle.Reset
181-
Write-Host "$green$reset Connected $name!"
182-
}
183-
if ($PassThru) {
184-
Write-Debug "Passing context [$contextObj] to the pipeline."
185-
Write-Output $contextObj
186-
}
187-
return
188-
}
189-
190-
$Installation | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
191-
$attempts = 0
192-
while ($true) {
193-
try {
194-
Import-Module -Name 'GitHub' -RequiredVersion $using:moduleVersion
195-
$params = @{
196-
Installation = $_
197-
Context = $using:Context
198-
PassThru = $using:PassThru
199-
Silent = $using:Silent
200-
Default = $using:Default
201-
}
202-
Connect-GitHubApp @params
203-
break
204-
} catch {
205-
if ($attempts -lt 3) {
206-
$attempts++
207-
Start-Sleep -Seconds (1 * $attempts)
208-
} else {
209-
throw $_
210-
}
211-
}
212-
}
213-
}
214-
return
215-
}
216100
'Filtered' {
217101
$installations = Get-GitHubAppInstallation -Context $Context
218102
Write-Verbose "Found [$($installations.Count)] installations."
219-
220103
$User | ForEach-Object {
221104
$userItem = $_
222105
Write-Verbose "User filter: [$userItem]."
@@ -238,31 +121,6 @@
238121
$null = $selectedInstallations.Add($_)
239122
}
240123
}
241-
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
242-
$attempts = 0
243-
while ($true) {
244-
try {
245-
Import-Module -Name 'GitHub' -RequiredVersion $using:moduleVersion
246-
$params = @{
247-
Installation = $_
248-
Context = $using:Context
249-
PassThru = $using:PassThru
250-
Silent = $using:Silent
251-
Default = $using:Default
252-
}
253-
Connect-GitHubApp @params
254-
break
255-
} catch {
256-
if ($attempts -lt 3) {
257-
$attempts++
258-
Start-Sleep -Seconds (1 * $attempts)
259-
} else {
260-
throw $_
261-
}
262-
}
263-
}
264-
}
265-
return
266124
break
267125
}
268126
'Installation ID' {
@@ -285,35 +143,10 @@
285143
}
286144
break
287145
}
288-
'All Installations' {
146+
default {
289147
Write-Verbose 'No target specified. Connecting to all installations.'
290-
$selectedInstallations = Get-GitHubAppInstallation -Context $Context
291-
$selectedInstallations | ForEach-Object -ThrottleLimit $ThrottleLimit -UseNewRunspace -Parallel {
292-
$attempts = 0
293-
while ($true) {
294-
try {
295-
Import-Module -Name 'GitHub' -RequiredVersion $using:moduleVersion
296-
$params = @{
297-
Installation = $_
298-
Context = $using:Context
299-
PassThru = $using:PassThru
300-
Silent = $using:Silent
301-
Default = $using:Default
302-
}
303-
Connect-GitHubApp @params
304-
break
305-
} catch {
306-
if ($attempts -lt 3) {
307-
$attempts++
308-
Start-Sleep -Seconds (1 * $attempts)
309-
} else {
310-
throw $_
311-
}
312-
}
313-
}
314-
$selectedInstallations.AddRange((Get-GitHubAppInstallation -Context $Context))
315-
Write-Verbose "Found [$($selectedInstallations.Count)] installations."
316-
}
148+
$selectedInstallations.AddRange((Get-GitHubAppInstallation -Context $Context))
149+
Write-Verbose "Found [$($selectedInstallations.Count)] installations."
317150
}
318151
}
319152

@@ -355,6 +188,21 @@
355188
$contextParams['Enterprise'] = [string]$installation.Target.Name
356189
}
357190
}
191+
Write-Verbose 'Logging in using a managed installation access token...'
192+
$contextParams | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
193+
$contextObj = [GitHubAppInstallationContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default))
194+
$contextObj | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
195+
if (-not $Silent) {
196+
$name = $contextObj.Name
197+
$green = $PSStyle.Foreground.Green
198+
$reset = $PSStyle.Reset
199+
Write-Host "$green$reset Connected $name!"
200+
}
201+
if ($PassThru) {
202+
Write-Debug "Passing context [$contextObj] to the pipeline."
203+
Write-Output $contextObj
204+
}
205+
$contextParams.Clear()
358206
}
359207
}
360208

tests/Apps.Tests.ps1

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ Describe 'Apps' {
3939
Write-Host ('-' * 60)
4040
}
4141

42-
Context 'Non-GitHubApp' {
43-
It 'Get-GitHubApp - Get an app by slug' -Skip:($AuthType -eq 'APP') {
44-
$app = Get-GitHubApp -Slug 'github-actions'
45-
LogGroup 'App by slug' {
46-
Write-Host ($app | Format-List | Out-String)
47-
}
48-
$app | Should -Not -BeNullOrEmpty
49-
}
50-
}
42+
# Context 'Non-GitHubApp' {
43+
# It 'Get-GitHubApp - Get an app by slug' -Skip:($AuthType -eq 'APP') {
44+
# $app = Get-GitHubApp -Slug 'github-actions'
45+
# LogGroup 'App by slug' {
46+
# Write-Host ($app | Format-List | Out-String)
47+
# }
48+
# $app | Should -Not -BeNullOrEmpty
49+
# }
50+
# }
5151

5252
Context 'GitHubApp' -Skip:($AuthType -ne 'APP') {
5353
BeforeAll {
@@ -174,47 +174,47 @@ Describe 'Apps' {
174174
$installation.Status | Should -BeIn @('Ok', 'Outdated')
175175
}
176176

177-
Context 'Webhooks' -Skip:($AuthType -ne 'APP') {
178-
It 'Get-GitHubAppWebhookConfiguration - Can get the webhook configuration' {
179-
$webhookConfig = Get-GitHubAppWebhookConfiguration
180-
LogGroup 'Webhook config' {
181-
Write-Host ($webhookConfig | Format-Table | Out-String)
182-
}
183-
$webhookConfig | Should -Not -BeNullOrEmpty
184-
}
177+
# Context 'Webhooks' -Skip:($AuthType -ne 'APP') {
178+
# It 'Get-GitHubAppWebhookConfiguration - Can get the webhook configuration' {
179+
# $webhookConfig = Get-GitHubAppWebhookConfiguration
180+
# LogGroup 'Webhook config' {
181+
# Write-Host ($webhookConfig | Format-Table | Out-String)
182+
# }
183+
# $webhookConfig | Should -Not -BeNullOrEmpty
184+
# }
185185

186-
It 'Update-GitHubAppWebhookConfiguration - Can update the webhook configuration' {
187-
{ Update-GitHubAppWebhookConfiguration -ContentType 'form' } | Should -Not -Throw
188-
$webhookConfig = Get-GitHubAppWebhookConfiguration
189-
LogGroup 'Webhook config - form' {
190-
Write-Host ($webhookConfig | Format-Table | Out-String)
191-
}
192-
{ Update-GitHubAppWebhookConfiguration -ContentType 'json' } | Should -Not -Throw
193-
$webhookConfig = Get-GitHubAppWebhookConfiguration
194-
LogGroup 'Webhook config - json' {
195-
Write-Host ($webhookConfig | Format-Table | Out-String)
196-
}
197-
}
186+
# It 'Update-GitHubAppWebhookConfiguration - Can update the webhook configuration' {
187+
# { Update-GitHubAppWebhookConfiguration -ContentType 'form' } | Should -Not -Throw
188+
# $webhookConfig = Get-GitHubAppWebhookConfiguration
189+
# LogGroup 'Webhook config - form' {
190+
# Write-Host ($webhookConfig | Format-Table | Out-String)
191+
# }
192+
# { Update-GitHubAppWebhookConfiguration -ContentType 'json' } | Should -Not -Throw
193+
# $webhookConfig = Get-GitHubAppWebhookConfiguration
194+
# LogGroup 'Webhook config - json' {
195+
# Write-Host ($webhookConfig | Format-Table | Out-String)
196+
# }
197+
# }
198198

199-
It 'Get-GitHubAppWebhookDelivery - Can get webhook deliveries' {
200-
$deliveries = Get-GitHubAppWebhookDelivery
201-
LogGroup 'Deliveries' {
202-
Write-Host ($deliveries | Format-Table | Out-String)
203-
}
204-
$deliveries | Should -Not -BeNullOrEmpty
205-
}
199+
# It 'Get-GitHubAppWebhookDelivery - Can get webhook deliveries' {
200+
# $deliveries = Get-GitHubAppWebhookDelivery
201+
# LogGroup 'Deliveries' {
202+
# Write-Host ($deliveries | Format-Table | Out-String)
203+
# }
204+
# $deliveries | Should -Not -BeNullOrEmpty
205+
# }
206206

207-
It 'Get-GitHubAppWebhookDelivery - Can redeliver a webhook delivery' {
208-
$deliveries = Get-GitHubAppWebhookDelivery | Select-Object -First 1
209-
LogGroup 'Delivery - redeliver' {
210-
Write-Host ($deliveries | Format-Table | Out-String)
211-
}
212-
{ Invoke-GitHubAppWebhookReDelivery -ID $deliveries.id } | Should -Not -Throw
213-
LogGroup 'Delivery - redeliver' {
214-
Write-Host ($deliveries | Format-Table | Out-String)
215-
}
216-
}
217-
}
207+
# It 'Get-GitHubAppWebhookDelivery - Can redeliver a webhook delivery' {
208+
# $deliveries = Get-GitHubAppWebhookDelivery | Select-Object -First 1
209+
# LogGroup 'Delivery - redeliver' {
210+
# Write-Host ($deliveries | Format-Table | Out-String)
211+
# }
212+
# { Invoke-GitHubAppWebhookReDelivery -ID $deliveries.id } | Should -Not -Throw
213+
# LogGroup 'Delivery - redeliver' {
214+
# Write-Host ($deliveries | Format-Table | Out-String)
215+
# }
216+
# }
217+
# }
218218

219219
Context 'Installation' -Skip:($AuthType -ne 'APP') {
220220
BeforeAll {

0 commit comments

Comments
 (0)