Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/classes/public/Config/GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
# The environment type, which is used to determine the context of the GitHub API calls.
[string] $EnvironmentType

# The completion mode for argument completers. Options: 'StartsWith', 'Contains'.
[string] $CompletionMode

# Simple parameterless constructor
GitHubConfig() {}

Expand Down
12 changes: 6 additions & 6 deletions src/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters

$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters

$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters

$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
}
}
3 changes: 3 additions & 0 deletions src/formats/GitHubConfig.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<ListItem>
<PropertyName>RetryInterval</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CompletionMode</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
Expand Down
52 changes: 36 additions & 16 deletions src/functions/private/Config/Initialize-GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,68 @@
Write-Debug "Force: [$Force]"
if ($Force) {
Write-Debug 'Forcing initialization of GitHubConfig.'
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
$script:GitHub.Config = [GitHubConfig]$context
$config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
$script:GitHub.Config = [GitHubConfig]$config
return
}

Write-Debug "GitHubConfig ID: [$($script:GitHub.Config.ID)]"
if ($null -ne $script:GitHub.Config) {
Write-Debug 'GitHubConfig already initialized and available in memory.'
return
}

Write-Debug 'Attempt to load the stored GitHubConfig from ContextVault'
$context = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault
if ($context) {
$config = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault
if ($config) {
Write-Debug 'GitHubConfig loaded into memory.'

Write-Debug 'Checking if new default properties are available in the stored context.'
Write-Debug 'Synchronizing stored context with GitHubConfig class definition.'
$needsUpdate = $false
$defaultProperties = $script:GitHub.DefaultConfig.PSObject.Properties.Name
foreach ($propName in $defaultProperties) {
if (-not $context.PSObject.Properties.Name.Contains($propName)) {
$validProperties = [GitHubConfig].GetProperties().Name
$storedProperties = $config.PSObject.Properties.Name

# Add missing properties from DefaultConfig
foreach ($propName in $validProperties) {
Write-Debug "Validating property [$propName]"
if (-not $storedProperties.Contains($propName)) {
Write-Debug "Adding missing property [$propName] from DefaultConfig"
$context | Add-Member -MemberType NoteProperty -Name $propName -Value $script:GitHub.DefaultConfig.$propName
$defaultValue = $script:GitHub.DefaultConfig.$propName
$config | Add-Member -MemberType NoteProperty -Name $propName -Value $defaultValue
$needsUpdate = $true
}
}

# Remove obsolete properties that are no longer supported
$propertiesToRemove = @()
foreach ($propName in $storedProperties) {
Write-Debug "Checking property [$propName] for obsolescence"
if (-not $validProperties.Contains($propName)) {
Write-Debug "Removing obsolete property [$propName] from stored context"
$propertiesToRemove += $propName
$needsUpdate = $true
}
}

# Remove the obsolete properties
foreach ($propName in $propertiesToRemove) {
$config.PSObject.Properties.Remove($propName)
}

if ($needsUpdate) {
Write-Debug 'Updating stored context with new default properties'
$context = Set-Context -Context $context -Vault $script:GitHub.ContextVault -PassThru
Write-Debug 'Updating stored context with synchronized properties'
$config = Set-Context -Context $config -Vault $script:GitHub.ContextVault -PassThru
}

$script:GitHub.Config = [GitHubConfig]$context
$script:GitHub.Config = [GitHubConfig]$config
return
}
Write-Debug 'Initializing GitHubConfig from defaults'
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
$script:GitHub.Config = [GitHubConfig]$context
$config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
$script:GitHub.Config = [GitHubConfig]$config
}

end {
Write-Debug "[$stackPath] - End"
}
}
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.1.3' }

3 changes: 2 additions & 1 deletion src/functions/public/Auth/Context/Get-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
# The name of the context.
[Parameter(
Mandatory,
ParameterSetName = 'Get a named context'
ParameterSetName = 'Get a named context',
Position = 0
)]
[Alias('Name')]
[string] $Context,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/public/Auth/Context/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Context -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters

$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$contexts = @()
$hasAnonymousParameter = $false
$command = Get-Command -Name $commandName -ErrorAction SilentlyContinue
Expand All @@ -14,7 +14,7 @@

$contexts += Get-GitHubContext -ListAvailable -Verbose:$false -Debug:$false
$contexts = $contexts | Sort-Object -Property Name
$contexts | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
$contexts | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}
14 changes: 13 additions & 1 deletion src/functions/public/Config/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
Register-ArgumentCompleter -CommandName Set-GitHubConfig, Get-GitHubConfig, Remove-GitHubConfig -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
([GitHubConfig]).GetProperties().Name | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
([GitHubConfig]).GetProperties().Name | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_ )
}
}

Register-ArgumentCompleter -CommandName Set-GitHubConfig -ParameterName Value -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
if ($fakeBoundParameters.Name -eq 'CompletionMode') {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
@('StartsWith', 'Contains') | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
6 changes: 4 additions & 2 deletions src/functions/public/Environments/completers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Where-Object { $_ -like '*GitHubEnvironment' }) -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Owner = $fakeBoundParameters.Owner
Repository = $fakeBoundParameters.Repository
Expand All @@ -10,14 +11,15 @@
Debug = $false
}
$params | Remove-HashtableEntry -NullOrEmptyValues
Get-GitHubEnvironment @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubEnvironment @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}

Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Environment -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Owner = $fakeBoundParameters.Owner
Repository = $fakeBoundParameters.Repository
Expand All @@ -26,7 +28,7 @@ Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport)
Debug = $false
}
$params | Remove-HashtableEntry -NullOrEmptyValues
Get-GitHubEnvironment @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubEnvironment @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}
3 changes: 2 additions & 1 deletion src/functions/public/Gitignore/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Register-ArgumentCompleter -CommandName Get-GitHubGitignore -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubGitignore @params | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubGitignore @params | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
3 changes: 2 additions & 1 deletion src/functions/public/License/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Register-ArgumentCompleter -CommandName Get-GitHubLicense -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubLicense @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubLicense @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}
9 changes: 6 additions & 3 deletions src/functions/public/Organization/completers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@
Where-Object { $_ -like '*GitHubOrganization' }) -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubOrganization @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubOrganization @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}

Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Owner -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubOrganization @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubOrganization @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}

Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Organization -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$params = @{
Context = $fakeBoundParameters.Context
Verbose = $false
Debug = $false
}
Get-GitHubOrganization @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
Get-GitHubOrganization @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}
}
12 changes: 8 additions & 4 deletions src/functions/public/Permission/completers.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
Register-ArgumentCompleter -CommandName Get-GitHubPermissionDefinition -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$script:GitHub.Permissions.Name | Sort-Object -Unique | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$script:GitHub.Permissions.Name | Sort-Object -Unique | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}

Register-ArgumentCompleter -CommandName Get-GitHubPermissionDefinition -ParameterName DisplayName -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$script:GitHub.Permissions.DisplayName | Sort-Object -Unique | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$script:GitHub.Permissions.DisplayName | Sort-Object -Unique | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}

Register-ArgumentCompleter -CommandName Get-GitHubPermissionDefinition -ParameterName Type -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$script:GitHub.Permissions.Type | Sort-Object -Unique | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$script:GitHub.Permissions.Type | Sort-Object -Unique | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}

Register-ArgumentCompleter -CommandName Get-GitHubPermissionDefinition -ParameterName Scope -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
$script:GitHub.Permissions.Scope | Sort-Object -Unique | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
$script:GitHub.Permissions.Scope | Sort-Object -Unique | Where-Object { $_ -like $pattern } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Loading