Skip to content

Commit

Permalink
🩹 [Patch]: Change how to get logged on entity info (#154)
Browse files Browse the repository at this point in the history
## Description

- To get info about GitHub app installations, I have added
Get-GitHubViewer that uses GraphQL for UAT,PAT,IAT logon types to get
name, id and databaseid. This should result in having the correct name
when running Connect-GitHubAccount. i.e. `✓ Logged in as
github-actions[bot]!`. App type of login (using PEM and clientID) still
uses Get-GitHubApp to get info about itself.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [x] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Nov 10, 2024
1 parent 68f62b5 commit 5a54835
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
31 changes: 13 additions & 18 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
}

$context = @{
Name = 'default'
Name = 'tmp'
ApiBaseUri = $ApiBaseUri
ApiVersion = $ApiVersion
HostName = $HostName
Expand Down Expand Up @@ -280,7 +280,6 @@
Secret = ConvertTo-SecureString -AsPlainText $Token
SecretType = $secretType
}
$context['Name'] = 'system'
$context['AuthType'] = 'IAT'
}
default {
Expand All @@ -291,30 +290,24 @@
}
}
}
Write-Verbose ($context | Format-Table | Out-String)
Set-GitHubContext @context
Set-GitHubContext @context # Needed so we can use the next authenticated functions (API calls).
try {
switch ($context['AuthType']) {
'PAT' {
$user = Get-GitHubUser
$context['Name'] = $user.login
$context['ID'] = $user.id
}
'UAT' {
$user = Get-GitHubUser
$context['Name'] = $user.login
$context['ID'] = $user.id
}
'IAT' {
$context['Name'] = 'installation'
switch -Regex ($context['AuthType']) {
'PAT|UAT|IAT' {
$viewer = Get-GitHubViewer
$context['Name'] = $viewer.login
$context['NodeID'] = $viewer.id
$context['DatabaseID'] = $viewer.databaseId
}
'App' {
$app = Get-GitHubApp
$context['Name'] = $app.slug
$context['ID'] = $app.id
$context['NodeID'] = $app.node_id
$context['DatabaseID'] = $app.id
}
default {
$context['Name'] = 'unknown'
$context['ID'] = 'unknown'
}
}
Set-GitHubContext @context
Expand All @@ -323,6 +316,8 @@
Write-Verbose 'Failed to set the user name'
}

Write-Verbose ($context | Format-Table | Out-String)

if (-not $Silent) {
$name = $(Get-GitHubConfig -Name Name)
Write-Host '' -ForegroundColor Green -NoNewline
Expand Down
31 changes: 31 additions & 0 deletions src/functions/public/Auth/Get-GitHubViewer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Get-GitHubViewer {
<#
.SYNOPSIS
Gets the currently authenticated user.
.DESCRIPTION
Gets the currently authenticated user.
.EXAMPLE
Get-GithubViewer
Gets the currently authenticated user.
.NOTES
[GraphQL API - Queries - Viewer](https://docs.github.com/en/graphql/reference/queries#viewer)
#>
[CmdletBinding()]
param(
[string[]] $Fields = @('login', 'id', 'databaseId')
)

$query = @"
query {
viewer {
$($Fields -join "`n")
}
}
"@
$results = Invoke-GitHubGraphQLQuery -Query $query
return $results.data.viewer
}
11 changes: 8 additions & 3 deletions src/functions/public/Config/Set-GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ function Set-GitHubConfig {
[Parameter()]
[string] $SecretType,

# The ID of the context.
# The Node ID of the context.
[Parameter()]
[string] $ID,
[string] $NodeID,

# The Database ID of the context.
[Parameter()]
[string] $DatabaseID,

# Set the access token.
[Parameter()]
Expand Down Expand Up @@ -108,7 +112,8 @@ function Set-GitHubConfig {
ClientID = $ClientID
DeviceFlowType = $DeviceFlowType
HostName = $HostName
ID = $ID
NodeID = $NodeID
DatabaseID = $DatabaseID
Name = $Name
Owner = $Owner
Repo = $Repo
Expand Down
11 changes: 8 additions & 3 deletions src/functions/public/Config/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ function Set-GitHubContext {
[Parameter(Mandatory)]
[string] $Name,

# The ID of the context.
# The Node ID of the context.
[Parameter()]
[string] $ID,
[string] $NodeID,

# The Database ID of the context.
[Parameter()]
[string] $DatabaseID,

# Set the access token type.
[Parameter(Mandatory)]
Expand Down Expand Up @@ -103,7 +107,8 @@ function Set-GitHubContext {
ClientID = $ClientID # Client ID for GitHub Apps
DeviceFlowType = $DeviceFlowType # GitHubApp / OAuthApp
HostName = $HostName # github.com / msx.ghe.com / github.local
ID = $ID # User ID / app ID
NodeID = $NodeID # User ID / app ID (GraphQL Node ID)
DatabaseID = $DatabaseID # Database ID
Name = $Name # Username / app slug
Owner = $Owner # Owner name
Repo = $Repo # Repo name
Expand Down
8 changes: 8 additions & 0 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ Describe 'GitHub' {
{ Get-GitHubConfig } | Should -Not -Throw
}
}
Context 'Get-GitHubViewer' {
It 'Get-GitHubViewer function exists' {
Get-Command Get-GitHubViewer | Should -Not -BeNullOrEmpty
}
It 'Get-GiTubViewer can be called' {
Get-GitHubViewer | Should -Not -BeNullOrEmpty
}
}
}

Describe 'Commands' {
Expand Down

0 comments on commit 5a54835

Please sign in to comment.