Skip to content

Commit

Permalink
Refactor GitHub context handling to improve type consistency and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Dec 13, 2024
1 parent 929261b commit 197450a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/classes/public/Context/GitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class GitHubContext : Context {
class GitHubContext : Context {
# The GitHub Context Name.
# HostName/Username or HostName/AppSlug
# github.com/Octocat
Expand All @@ -18,7 +18,7 @@ class GitHubContext : Context {

# The API base URI.
# https://api.github.com
[uri] $ApiBaseUri
[string] $ApiBaseUri

# The GitHub API version.
# 2022-11-28
Expand Down
26 changes: 18 additions & 8 deletions src/functions/private/Apps/Get-GitHubAppByName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@
[object] $Context = (Get-GitHubContext)
)

$Context = Resolve-GitHubContext -Context $Context

$inputObject = @{
Context = $Context
APIEndpoint = "/apps/$AppSlug"
Method = 'GET'
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
process {
$inputObject = @{
Context = $Context
APIEndpoint = "/apps/$AppSlug"
Method = 'GET'
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
end {
Write-Verbose "[$commandName] - End"
}
}
1 change: 0 additions & 1 deletion src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
)

$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App

switch ($PSCmdlet.ParameterSetName) {
'BySlug' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)

$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App

$inputObject = @{
Context = $Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
)

$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App

$inputObject = @{
Context = $Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
)

$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App

$inputObject = @{
Context = $Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)

$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType App

$inputObject = @{
Context = $Context
Expand Down
12 changes: 6 additions & 6 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Connect-GitHubApp {
function Connect-GitHubApp {
<#
.SYNOPSIS
Connects to GitHub as a installation using a GitHub App.
Expand Down Expand Up @@ -104,7 +104,7 @@ function Connect-GitHubApp {
AuthType = [string]'IAT'
TokenType = [string]'ghs'
DisplayName = [string]$Context.DisplayName
ApiBaseUri = [uri]$Context.ApiBaseUri
ApiBaseUri = [string]$Context.ApiBaseUri
ApiVersion = [string]$Context.ApiVersion
HostName = [string]$Context.HostName
ClientID = [string]$Context.ClientID
Expand All @@ -113,18 +113,18 @@ function Connect-GitHubApp {
Events = [string[]]$installation.events
TargetType = [string]$installation.target_type
Token = [securestring]$token.Token
TokenExpirationDate = [datetime]$token.ExpiresAt
TokenExpirationDate = [string]$token.ExpiresAt
}

switch ($installation.target_type) {
'User' {
$contextParams['TargetName'] = [string]$installation.account.login
$contextParams['TargetName'] = $installation.account.login
}
'Organization' {
$contextParams['TargetName'] = [string]$installation.account.login
$contextParams['TargetName'] = $installation.account.login
}
'Enterprise' {
$contextParams['TargetName'] = [string]$installation.account.slug
$contextParams['TargetName'] = $installation.account.slug
}
}
Write-Verbose 'Logging in using a managed installation access token...'
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function Set-GitHubContext {
# Run functions to get info on the temporary context.
try {
Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]."
switch -Regex ($contextObj['AuthType']) {
switch -Regex ($($contextObj['AuthType'])) {
'PAT|UAT|IAT' {
$viewer = Get-GitHubViewer -Context $contextObj
$viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
Expand Down

0 comments on commit 197450a

Please sign in to comment.