Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Dec 12, 2024
1 parent cd83fa1 commit 5157db1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 59 deletions.
16 changes: 5 additions & 11 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,11 @@
)]
[string] $PrivateKey,

# Skip loading GitHub App contexts.
# Automatically load installations for the GitHub App.
[Parameter(
ParameterSetName = 'App'
)]
[switch] $SkipAppAutoload,

# Do not load credentials for the GitHub App Installations, just metadata.
[Parameter(
ParameterSetName = 'App'
)]
[switch] $Shallow,
[switch] $AutoloadInstallations,

# The default enterprise to use in commands.
[Parameter()]
Expand Down Expand Up @@ -307,9 +301,9 @@
Write-Host "Logged in as $name!"
}

if ($authType -eq 'App' -and -not $SkipAppAutoload) {
Write-Verbose 'Loading GitHub App contexts...'
Connect-GitHubApp -Shallow:$Shallow
if ($authType -eq 'App' -and $AutoloadInstallations) {
Write-Verbose 'Loading GitHub App Installation contexts...'
Connect-GitHubApp
}

} catch {
Expand Down
42 changes: 16 additions & 26 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@
# 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),

# Do not load credentials for the GitHub App Installations, just metadata.
[Parameter()]
[switch] $Shallow
[object] $Context = (Get-GitHubContext)
)

begin {
Expand All @@ -82,12 +78,13 @@
$Context | Assert-GitHubContext -AuthType 'App'

$defaultContextData = @{
ApiBaseUri = $Context.ApiBaseUri
ApiVersion = $Context.ApiVersion
HostName = $Context.HostName
ClientID = $Context.ClientID
AuthType = 'IAT'
TokenType = 'ghs'
ApiBaseUri = [string]$Context.ApiBaseUri
ApiVersion = [string]$Context.ApiVersion
HostName = [string]$Context.HostName
ClientID = [string]$Context.ClientID
AuthType = [string]'IAT'
TokenType = [string]'ghs'
DisplayNAme = [string]$Context.DisplayName
}

$installations = Get-GitHubAppInstallation
Expand All @@ -111,21 +108,14 @@
$installations | ForEach-Object {
$installation = $_
$contextParams = @{} + $defaultContextData.Clone()
if ($Shallow) {
$token = [PSCustomObject]@{
Token = [securestring]::new()
ExpiresAt = [datetime]::MinValue
}
} else {
$token = New-GitHubAppInstallationAccessToken -InstallationID $installation.id
}

$contextParams['InstallationID'] = $installation.id
$contextParams['Token'] = $token.Token
$contextParams['TokenExpirationDate'] = $token.ExpiresAt
$contextParams['Permissions'] = $installation.permissions
$contextParams['Events'] = $installation.events
$contextParams['TargetType'] = $installation.target_type
$token = New-GitHubAppInstallationAccessToken -InstallationID $installation.id

$contextParams['InstallationID'] = [string]$installation.id
$contextParams['Token'] = [string]$token.Token
$contextParams['TokenExpirationDate'] = [string]$token.ExpiresAt
$contextParams['Permissions'] = [string]$installation.permissions
$contextParams['Events'] = [string]$installation.events
$contextParams['TargetType'] = [string]$installation.target_type

switch ($installation.target_type) {
'User' {
Expand Down
53 changes: 31 additions & 22 deletions src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,44 @@ function Set-GitHubContext {
$Context['DatabaseID'] = [string]$viewer.databaseId
}
'PAT|UAT' {
$ContextName = "$($Context['HostName'])/$login"
$Context['Name'] = $ContextName
$contextName = "$($Context['HostName'])/$login"
$Context['Name'] = $contextName
$Context['Type'] = 'User'
}
'IAT' {
$gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
$app = Get-GitHubApp -AppSlug $login -Context $Context
$targetType = $gitHubEvent.repository.owner.type
$targetName = $gitHubEvent.repository.owner.login
Write-Verbose ('Enterprise: ' + $gitHubEvent.enterprise.slug)
Write-Verbose ('Organization: ' + $gitHubEvent.organization.login)
Write-Verbose ('Repository: ' + $gitHubEvent.repository.name)
Write-Verbose ('Repository Owner: ' + $gitHubEvent.repository.owner.login)
Write-Verbose ('Repository Owner Type: ' + $gitHubEvent.repository.owner.type)
Write-Verbose ('Sender: ' + $gitHubEvent.sender.login)
$ContextName = "$($Context['HostName'])/$login/$targetType/$targetName"
$Context['Name'] = $ContextName
$Context['DisplayName'] = [string]$app.name
$Context['Type'] = 'Installation'
$Context['Enterprise'] = [string]$gitHubEvent.enterprise.slug
$Context['TargetType'] = [string]$gitHubEvent.repository.owner.type
$Context['TargetName'] = [string]$gitHubEvent.repository.owner.login
$Context['Owner'] = [string]$gitHubEvent.repository.owner.login
$Context['Repo'] = [string]$gitHubEvent.repository.name
$Context['DisplayName'] = [string]$app.name

if ($script:GitHub.EnvironmentType -eq 'GHA') {
$gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
$targetType = $gitHubEvent.repository.owner.type
$targetName = $gitHubEvent.repository.owner.login
$enterprise = $gitHubEvent.enterprise.slug
$organization = $gitHubEvent.organization.login
$owner = $gitHubEvent.repository.owner.login
$repo = $gitHubEvent.repository.name
$gh_sender = $gitHubEvent.sender.login # sender is an automatic variable in Powershell
Write-Verbose "Enterprise: $enterprise"
Write-Verbose "Organization: $organization"
Write-Verbose "Repository: $repo"
Write-Verbose "Repository Owner: $owner"
Write-Verbose "Repository Owner Type: $targetType"
Write-Verbose "Sender: $gh_sender"
$Context['Enterprise'] = [string]$enterprise
$Context['TargetType'] = [string]$targetType
$Context['TargetName'] = [string]$owner
$Context['Owner'] = [string]$owner
$Context['Repo'] = [string]$repo
$contextName = "$($Context['HostName'])/$login/$targetType/$targetName"
$Context['Name'] = $contextName
} else {
$login = $Context['Username']
}
}
'App' {
$app = Get-GitHubApp -Context $Context
$ContextName = "$($Context['HostName'])/$($app.slug)"
$Context['Name'] = $ContextName
$contextName = "$($Context['HostName'])/$($app.slug)"
$Context['Name'] = $contextName
$Context['DisplayName'] = [string]$app.name
$Context['Username'] = [string]$app.slug
$Context['NodeID'] = [string]$app.node_id
Expand Down

0 comments on commit 5157db1

Please sign in to comment.