Skip to content

Commit

Permalink
Refactor GitHub context handling and improve verbosity in authenticat…
Browse files Browse the repository at this point in the history
…ion scripts
  • Loading branch information
MariusStorhaug committed Dec 9, 2024
1 parent ba4e784 commit 5eb2475
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 57 deletions.
5 changes: 4 additions & 1 deletion src/functions/private/Auth/Context/Resolve-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Verbose 'Context:'
$Context | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$Context | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
}

process {
Expand All @@ -59,6 +59,9 @@
# }
# }
# }

Write-Verbose 'Resolved Context:'
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
}

end {
Expand Down
4 changes: 0 additions & 4 deletions src/functions/public/Apps/Get-GitHubAppJSONWebToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,4 @@ function Get-GitHubAppJSONWebToken {
ExpiresAt = $exp
Issuer = $ClientId
}
Remove-Variable -Name jwt -ErrorAction SilentlyContinue
Remove-Variable -Name rsa -ErrorAction SilentlyContinue
Remove-Variable -Name signature -ErrorAction SilentlyContinue
[System.GC]::Collect()
}
6 changes: 0 additions & 6 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@
Write-Error $_
Write-Error (Get-PSCallStack | Format-Table | Out-String)
throw 'Failed to connect to GitHub.'
} finally {
Remove-Variable -Name tokenResponse -ErrorAction SilentlyContinue
Remove-Variable -Name context -ErrorAction SilentlyContinue
Remove-Variable -Name contextData -ErrorAction SilentlyContinue
Remove-Variable -Name Token -ErrorAction SilentlyContinue
[System.GC]::Collect()
}
Write-Verbose "[$commandName] - End"
}
35 changes: 0 additions & 35 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,11 @@
$name = $tmpContext.name
Write-Host "Connected $name"
}
Remove-Variable -Name tmpContext -ErrorAction SilentlyContinue
Remove-Variable -Name token -ErrorAction SilentlyContinue
}
} catch {
Write-Error $_
Write-Error (Get-PSCallStack | Format-Table | Out-String)
throw 'Failed to connect to GitHub using a GitHub App.'
} finally {
} clean {
Remove-Variable -Name tmpContext -ErrorAction SilentlyContinue
Remove-Variable -Name token -ErrorAction SilentlyContinue
[System.GC]::Collect()
}
Write-Verbose "[$commandName] - End"
}

Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'User' -and $_.account.login -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.slug, $_.account.slug, 'ParameterValue', $_.account.slug)
}
}
4 changes: 2 additions & 2 deletions src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ function Set-GitHubContext {
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
Set-Context -ID "$($script:GitHub.Config.ID)/$($context['Name'])" -Context $context
if ($Default) {
Set-GitHubDefaultContext -Context $($context['Name'])
Set-GitHubDefaultContext -Context $context['Name']
if ($Context['AuthType'] -eq 'IAT' -and $script:GitHub.EnvironmentType -eq 'GHA') {
Set-GitHubGitConfig -Context $($context['Name'])
Set-GitHubGitConfig -Context $context['Name']
}
}
if ($PassThru) {
Expand Down
25 changes: 16 additions & 9 deletions src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
#>
[CmdletBinding(SupportsShouldProcess)]
param(
# The context to set as the default.
[Parameter(Mandatory)]
[Alias('Name')]
[string] $Context
# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
[object] $Context = (Get-GitHubContext)
)

$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

if ($PSCmdlet.ShouldProcess("$Context", 'Set default context')) {
Set-GitHubConfig -Name 'DefaultContext' -Value $Context
process {
if ($PSCmdlet.ShouldProcess("$Context", 'Set default context')) {
Set-GitHubConfig -Name 'DefaultContext' -Value $Context.Name
}
}

Write-Verbose "[$commandName] - End"
end {
Write-Verbose "[$commandName] - End"
}
}
28 changes: 28 additions & 0 deletions src/functions/public/Auth/completers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'User' -and $_.account.login -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.login, $_.account.login, 'ParameterValue', $_.account.login)
}
}
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter

Get-GitHubAppInstallation -Verbose:$false | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.account.slug, $_.account.slug, 'ParameterValue', $_.account.slug)
}
}

0 comments on commit 5eb2475

Please sign in to comment.