Skip to content

Commit c851625

Browse files
🩹 [Patch]: Update Connect-GitHubApp to support installation ID parameter (#525)
## Description This pull request adds support for connecting to a GitHub App installation using an explicit installation ID, improving flexibility and usability in authentication scenarios. The main changes involve updating the `Connect-GitHubApp` function to accept installation IDs directly and adding corresponding tests to ensure the new functionality works as expected. **Enhancements to authentication logic:** * Updated the `Connect-GitHubApp.ps1` function to accept an `-ID` parameter, allowing users to connect directly to installations by their ID. This replaces the previous pipeline-based `Installation` parameter with a more flexible approach. * Added logic to process the new `InstallationID` parameter set, including verbose logging and warnings when an installation ID is not found. **Testing improvements:** * Added a new test case in `Apps.Tests.ps1` to verify that `Connect-GitHubApp` successfully connects using the `-ID` parameter for a single installation, checking key properties of the returned context object. * Updated test setup to retrieve the installation object for use in the new test scenario. **General improvements:** * Refactored code to ensure installations are only retrieved when necessary, improving efficiency and clarity. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [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 586dd4d commit c851625

File tree

2 files changed

+196
-108
lines changed

2 files changed

+196
-108
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@
5959
[string[]] $Enterprise,
6060

6161
# Installation objects from pipeline for parallel processing.
62-
[Parameter(Mandatory, ParameterSetName = 'Installation', ValueFromPipeline)]
62+
[Parameter(Mandatory, ParameterSetName = 'Installation object', ValueFromPipeline)]
6363
[GitHubAppInstallation[]] $Installation,
6464

65+
# The installation ID(s) to connect to directly.
66+
# Accepts input from the pipeline by property name (e.g. objects with an ID property)
67+
[Parameter(Mandatory, ParameterSetName = 'Installation ID', ValueFromPipelineByPropertyName)]
68+
[Alias('InstallationID')]
69+
[int[]] $ID,
70+
6571
# Passes the context object to the pipeline.
6672
[Parameter()]
6773
[switch] $PassThru,
@@ -89,36 +95,58 @@
8995
}
9096

9197
process {
92-
$installations = Get-GitHubAppInstallation -Context $Context
93-
$selectedInstallations = @()
94-
Write-Verbose "Found [$($installations.Count)] installations."
98+
$selectedInstallations = [System.Collections.ArrayList]::new()
9599
switch ($PSCmdlet.ParameterSetName) {
96100
'Filtered' {
101+
$installations = Get-GitHubAppInstallation -Context $Context
102+
Write-Verbose "Found [$($installations.Count)] installations."
97103
$User | ForEach-Object {
98104
$userItem = $_
99105
Write-Verbose "User filter: [$userItem]."
100-
$selectedInstallations += $installations | Where-Object {
101-
$_.Type -eq 'User' -and $_.Target.Name -like $userItem
106+
$installations | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like $userItem } | ForEach-Object {
107+
$null = $selectedInstallations.Add($_)
102108
}
103109
}
104110
$Organization | ForEach-Object {
105111
$organizationItem = $_
106112
Write-Verbose "Organization filter: [$organizationItem]."
107-
$selectedInstallations += $installations | Where-Object {
108-
$_.Type -eq 'Organization' -and $_.Target.Name -like $organizationItem
113+
$installations | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like $organizationItem } | ForEach-Object {
114+
$null = $selectedInstallations.Add($_)
109115
}
110116
}
111117
$Enterprise | ForEach-Object {
112118
$enterpriseItem = $_
113119
Write-Verbose "Enterprise filter: [$enterpriseItem]."
114-
$selectedInstallations += $installations | Where-Object {
115-
$_.Type -eq 'Enterprise' -and $_.Target.Name -like $enterpriseItem
120+
$installations | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like $enterpriseItem } | ForEach-Object {
121+
$null = $selectedInstallations.Add($_)
122+
}
123+
}
124+
break
125+
}
126+
'Installation ID' {
127+
Write-Verbose 'Selecting installations by explicit ID.'
128+
foreach ($installationId in $ID) {
129+
Write-Verbose "Looking up installation ID [$installationId]"
130+
$found = Get-GitHubAppInstallation -ID $installationId -Context $Context
131+
if (-not $found) {
132+
Write-Warning "No installation found for ID [$installationId]."
133+
continue
116134
}
135+
$null = $selectedInstallations.Add($found)
136+
}
137+
break
138+
}
139+
'Installation object' {
140+
Write-Verbose 'Selecting installations from the pipeline.'
141+
foreach ($installationObject in $Installation) {
142+
$null = $selectedInstallations.Add($installationObject)
117143
}
144+
break
118145
}
119146
default {
120147
Write-Verbose 'No target specified. Connecting to all installations.'
121-
$selectedInstallations = $installations
148+
$selectedInstallations.AddRange((Get-GitHubAppInstallation -Context $Context))
149+
Write-Verbose "Found [$($selectedInstallations.Count)] installations."
122150
}
123151
}
124152

0 commit comments

Comments
 (0)