diff --git a/examples/Apps/AppManagement.ps1 b/examples/Apps/AppManagement.ps1 index 677f9e0c3..0e66b83ac 100644 --- a/examples/Apps/AppManagement.ps1 +++ b/examples/Apps/AppManagement.ps1 @@ -5,6 +5,6 @@ $appIDs = @( $orgs = Get-GitHubOrganization -Enterprise 'msx' foreach ($org in $orgs) { foreach ($appID in $appIDs) { - Install-GitHubAppOnEnterpriseOrganization -Enterprise msx -Organization $org.login -ClientID $appID -RepositorySelection all + Install-GitHubApp -Enterprise msx -Organization $org.login -ClientID $appID -Selection all } } diff --git a/examples/Apps/EnterpriseApps.ps1 b/examples/Apps/EnterpriseApps.ps1 index 2526b7b2d..65a069fea 100644 --- a/examples/Apps/EnterpriseApps.ps1 +++ b/examples/Apps/EnterpriseApps.ps1 @@ -24,7 +24,7 @@ filter Install-GithubApp { $orgs = $installableOrgs | Where-Object { $_.login -like $organization } foreach ($org in $orgs) { foreach ($appIDitem in $AppID) { - Install-GitHubApp -Enterprise $Enterprise -Organization $org.login -ClientID $appIDitem -RepositorySelection all | ForEach-Object { + Install-GitHubApp -Enterprise $Enterprise -Organization $org.login -ClientID $appIDitem -Selection all | ForEach-Object { [PSCustomObject]@{ Organization = $org.login AppID = $appIDitem diff --git a/src/classes/public/App/GitHubAppInstallation.ps1 b/src/classes/public/App/GitHubAppInstallation.ps1 index 6684d77f4..84507311d 100644 --- a/src/classes/public/App/GitHubAppInstallation.ps1 +++ b/src/classes/public/App/GitHubAppInstallation.ps1 @@ -12,7 +12,7 @@ [string] $Type # The type of repository selection. - [string] $RepositorySelection + [string] $Selection # The permissions that the app has on the target. [pscustomobject] $Permissions @@ -51,7 +51,7 @@ ) $this.Target = [GitHubOwner]::new($Object.account) $this.Type = $Object.target_type - $this.RepositorySelection = $Object.repository_selection + $this.Selection = $Object.repository_selection $this.Permissions = $Object.permissions $this.Events = , ($Object.events) $this.FilePaths = $Object.single_file_paths diff --git a/src/functions/private/Apps/GitHub Apps/Install-GitHubAppOnEnterpriseOrganization.ps1 b/src/functions/private/Apps/GitHub Apps/Install-GitHubAppOnEnterpriseOrganization.ps1 index 86ad14af4..b4490cc19 100644 --- a/src/functions/private/Apps/GitHub Apps/Install-GitHubAppOnEnterpriseOrganization.ps1 +++ b/src/functions/private/Apps/GitHub Apps/Install-GitHubAppOnEnterpriseOrganization.ps1 @@ -9,7 +9,9 @@ The authenticated GitHub App must be installed on the enterprise and be granted the Enterprise/organization_installations (write) permission. .EXAMPLE - Install-GitHubAppOnEnterpriseOrganization -Enterprise 'msx' -Organization 'org' -ClientID '123456' + Install-GitHubAppOnEnterpriseOrganization -Enterprise 'msx' -Organization 'org' -ClientID '123456' -Selection All + + Install the GitHub App on the organization 'org' in the enterprise 'msx' with the client ID '123456' and the selection 'All'. #> [CmdletBinding()] param( @@ -30,7 +32,7 @@ # - selected - select specific repositories. [Parameter(Mandatory)] [ValidateSet('all', 'selected')] - [string] $RepositorySelection, + [string] $Selection, # The names of the repositories to which the installation will be granted access. [Parameter()] @@ -51,7 +53,7 @@ process { $body = @{ client_id = $ClientID - repository_selection = $RepositorySelection + repository_selection = $Selection repositories = $Repositories } $body | Remove-HashtableEntry -NullOrEmptyValues diff --git a/src/functions/public/Apps/GitHub App Installations/Update-GitHubAppInstallationRepositoryAccess.ps1 b/src/functions/public/Apps/GitHub App Installations/Update-GitHubAppInstallationRepositoryAccess.ps1 index cd6b9905b..4456bd46f 100644 --- a/src/functions/public/Apps/GitHub App Installations/Update-GitHubAppInstallationRepositoryAccess.ps1 +++ b/src/functions/public/Apps/GitHub App Installations/Update-GitHubAppInstallationRepositoryAccess.ps1 @@ -7,7 +7,7 @@ Update repository access for a GitHub App installation between all repositories and selected repositories. .EXAMPLE - Update-GitHubAppInstallationRepositoryAccess -Enterprise 'msx' -Organization 'PSModule' -InstallationID 12345678 -RepositorySelection 'all' + Update-GitHubAppInstallationRepositoryAccess -Enterprise 'msx' -Organization 'PSModule' -InstallationID 12345678 Update the repository access for the GitHub App installation with the ID '12345678' to all repositories on the organization 'PSModule' in the enterprise 'msx'. @@ -17,7 +17,6 @@ Enterprise = 'msx' Organization = 'PSModule' InstallationID = 12345678 - RepositorySelection = 'selected' Repositories = 'repo1', 'repo2' } Update-GitHubAppInstallationRepositoryAccess @params @@ -28,6 +27,7 @@ .LINK https://psmodule.io/GitHub/Functions/Apps/GitHub%20App%20Installations/Update-GitHubAppInstallationRepositoryAccess #> + [CmdletBinding(SupportsShouldProcess)] param( # The enterprise slug or ID. @@ -52,13 +52,6 @@ [Alias('installation_id', 'InstallationID')] [int] $ID, - # The repository selection for the GitHub App. Can be one of: - # - all - all repositories that the authenticated GitHub App installation can access. - # - selected - select specific repositories. - [Parameter(Mandatory)] - [ValidateSet('all', 'selected')] - [string] $RepositorySelection, - # The names of the repositories to which the installation will be granted access. [Parameter()] [string[]] $Repositories = @(), @@ -80,7 +73,7 @@ process { $body = @{ - repository_selection = $RepositorySelection + repository_selection = $Repositories.Count -gt 0 ? 'selected' : 'all' repositories = $Repositories } $body | Remove-HashtableEntry -NullOrEmptyValues diff --git a/src/functions/public/Apps/GitHub App/Install-GitHubApp.ps1 b/src/functions/public/Apps/GitHub App/Install-GitHubApp.ps1 index d30dafe98..1dd9febfb 100644 --- a/src/functions/public/Apps/GitHub App/Install-GitHubApp.ps1 +++ b/src/functions/public/Apps/GitHub App/Install-GitHubApp.ps1 @@ -7,54 +7,39 @@ Installs the provided GitHub App on the specified target. .EXAMPLE - Install-GitHubApp -Enterprise 'msx' -Organization 'org' -ClientID '123456' -RepositorySelection 'selected' -Repositories 'repo1', 'repo2' + Install-GitHubApp -Enterprise 'msx' -Organization 'org' -ClientID '123456' Install the GitHub App with - the client ID '123456' - - the repository selection 'selected' - - the repositories 'repo1' and 'repo2' + - the repository selection 'all' on the organization 'org' in the enterprise 'msx'. .EXAMPLE - Install-GitHubApp -Enterprise 'msx' -Organization 'org' -ClientID '123456' -RepositorySelection 'all' + Install-GitHubApp -Enterprise 'msx' -Organization 'org' -ClientID '123456' -Repositories 'repo1', 'repo2' Install the GitHub App with - the client ID '123456' - - the repository selection 'all' + - the repository selection 'selected' + - the repositories 'repo1' and 'repo2' on the organization 'org' in the enterprise 'msx'. .LINK https://psmodule.io/GitHub/Functions/Apps/GitHub%20App/Install-GitHubApp #> - [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] + [CmdletBinding()] param( # The enterprise slug or ID. - [Parameter( - Mandatory, - ParameterSetName = 'EnterpriseOrganization', - ValueFromPipelineByPropertyName - )] + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string] $Enterprise, # The organization name. The name is not case sensitive. - [Parameter( - Mandatory, - ParameterSetName = 'EnterpriseOrganization', - ValueFromPipelineByPropertyName - )] + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string] $Organization, # The client ID of the GitHub App to install. [Parameter(Mandatory)] [string] $ClientID, - # The repository selection for the GitHub App. Can be one of: - # - all - all repositories that the authenticated GitHub App installation can access. - # - selected - select specific repositories. - [Parameter()] - [ValidateSet('all', 'selected')] - [string] $RepositorySelection = 'selected', - # The names of the repositories to which the installation will be granted access. [Parameter()] [string[]] $Repositories = @(), @@ -74,19 +59,15 @@ } process { - switch ($PSCmdlet.ParameterSetName) { - 'EnterpriseOrganization' { - $params = @{ - Enterprise = $Enterprise - Organization = $Organization - ClientID = $ClientID - RepositorySelection = $RepositorySelection - Repositories = $Repositories - Context = $Context - } - Install-GitHubAppOnEnterpriseOrganization @params - } + $params = @{ + Enterprise = $Enterprise + Organization = $Organization + ClientID = $ClientID + Selection = $Repository.Count -gt 0 ? 'selected' : 'all' + Repositories = $Repositories + Context = $Context } + Install-GitHubAppOnEnterpriseOrganization @params } end { diff --git a/src/functions/public/Apps/GitHub App/New-GitHubAppInstallationAccessToken.ps1 b/src/functions/public/Apps/GitHub App/New-GitHubAppInstallationAccessToken.ps1 index 2291149ad..9535aeb97 100644 --- a/src/functions/public/Apps/GitHub App/New-GitHubAppInstallationAccessToken.ps1 +++ b/src/functions/public/Apps/GitHub App/New-GitHubAppInstallationAccessToken.ps1 @@ -84,10 +84,10 @@ Invoke-GitHubAPI @inputObject | ForEach-Object { [pscustomobject]@{ - Token = $_.Response.token | ConvertTo-SecureString -AsPlainText -Force - ExpiresAt = $_.Response.expires_at.ToLocalTime() - Permissions = $_.Response.permissions - RepositorySelection = $_.Response.repository_selection + Token = $_.Response.token | ConvertTo-SecureString -AsPlainText -Force + ExpiresAt = $_.Response.expires_at.ToLocalTime() + Permissions = $_.Response.permissions + Selection = $_.Response.repository_selection } } } diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index dd11923d2..447028477 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -85,33 +85,47 @@ } process { - $installations = Get-GitHubAppInstallation -Context $Context $selectedInstallations = @() Write-Verbose "Found [$($installations.Count)] installations." + $installations | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ } switch ($PSCmdlet.ParameterSetName) { 'Filtered' { - $User | ForEach-Object { - $userItem = $_ - Write-Verbose "User filter: [$userItem]." - $selectedInstallations += $installations | Where-Object { + foreach ($userItem in $User) { + $userInstallations = $installations | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like $userItem } + $selectedInstallations += $userInstallations } - $Organization | ForEach-Object { - $organizationItem = $_ - Write-Verbose "Organization filter: [$organizationItem]." - $selectedInstallations += $installations | Where-Object { + foreach ($organizationItem in $Organization) { + $organizationInstallations = $installations | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like $organizationItem } + $selectedInstallations += $organizationInstallations } - $Enterprise | ForEach-Object { - $enterpriseItem = $_ - Write-Verbose "Enterprise filter: [$enterpriseItem]." - $selectedInstallations += $installations | Where-Object { + foreach ($enterpriseItem in $Enterprise) { + $enterpriseInstallations = $installations | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like $enterpriseItem } + $selectedInstallations += $enterpriseInstallations } + $( + [pscustomobject]@{ + Type = 'User' + Filter = $userItem + Count = $userInstallations.Count + } + [pscustomobject]@{ + Type = 'Organization' + Filter = $organizationItem + Count = $organizationInstallations.Count + } + [pscustomobject]@{ + Type = 'Enterprise' + Filter = $enterpriseItem + Count = $enterpriseInstallations.Count + } + ) | Format-Table -AutoSize | Out-String -Stream | ForEach-Object { Write-Verbose $_ } } default { Write-Verbose 'No target specified. Connecting to all installations.' @@ -120,8 +134,7 @@ } Write-Verbose "Found [$($selectedInstallations.Count)] installations for the target." - $selectedInstallations | ForEach-Object { - $installation = $_ + foreach ($installation in $selectedInstallations) { Write-Verbose "Processing installation [$($installation.Target.Name)] [$($installation.id)]" $token = New-GitHubAppInstallationAccessToken -Context $Context -InstallationID $installation.id @@ -153,8 +166,8 @@ $contextParams['Owner'] = [string]$installation.Target.Name } 'Enterprise' { - $contextParams['InstallationName'] = [string]$installation.account.slug - $contextParams['Enterprise'] = [string]$installation.account.slug + $contextParams['InstallationName'] = [string]$installation.Target.Name + $contextParams['Enterprise'] = [string]$installation.Target.Name } } Write-Verbose 'Logging in using a managed installation access token...'