Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
496d4fa
🩹 [Patch]: Enhance GitHub account connection details and improve cont…
MariusStorhaug Dec 11, 2024
270f4c5
Get app stuff...
MariusStorhaug Dec 11, 2024
b67d87e
Merge branch 'main' of https://github.com/PSModule/GitHub into apps3
MariusStorhaug Dec 11, 2024
4c8496e
qwe
MariusStorhaug Dec 11, 2024
dc65b04
Auto-generated changes
github-actions[bot] Dec 11, 2024
e5b5513
🩹 [Patch]: Update GitHub authentication context and workflow settings
MariusStorhaug Dec 11, 2024
3bfda65
Enhance GitHub context setup by including app details and refining co…
MariusStorhaug Dec 11, 2024
597c888
Fix Context on Get-GitHubApp
MariusStorhaug Dec 11, 2024
7b9dc45
Refactor GitHub context setup to use direct assignment for context pr…
MariusStorhaug Dec 12, 2024
ae24721
Refactor Set-GitHubContext to use direct assignment for context prope…
MariusStorhaug Dec 12, 2024
1be02d2
Refactor Connect-GitHubApp to improve context handling and streamline…
MariusStorhaug Dec 12, 2024
665a603
test
MariusStorhaug Dec 12, 2024
cd83fa1
Update Process-PSModule workflow to skip only SourceCode tests
MariusStorhaug Dec 12, 2024
5157db1
Fixes
MariusStorhaug Dec 12, 2024
3800936
Refactor GitHub.Tests to use parameter splatting for Connect-GitHubAc…
MariusStorhaug Dec 12, 2024
6d27b5d
Fix
MariusStorhaug Dec 12, 2024
e90c72b
Refactor Connect-GitHubApp to use appropriate types for Permissions, …
MariusStorhaug Dec 12, 2024
4ffe53d
Add XML format definitions for GitHubContext and UserGitHubContext
MariusStorhaug Dec 12, 2024
f515096
Add parameterless constructors to GitHubContext classes
MariusStorhaug Dec 12, 2024
efba876
Improve Set-GitHubContext to conditionally update context properties …
MariusStorhaug Dec 12, 2024
f02d11a
Refactor Connect-GitHubApp to use context in Get-GitHubAppInstallatio…
MariusStorhaug Dec 12, 2024
bcbf4b5
Fix Set-GitHubContext to set context properties only if they are null
MariusStorhaug Dec 12, 2024
f0e7536
Add conditional assignment for DisplayName in Set-GitHubContext to ha…
MariusStorhaug Dec 12, 2024
dd180d6
Update Set-GitHubContext to use IsNullOrEmpty for better null handlin…
MariusStorhaug Dec 12, 2024
81051b4
Refactor Set-GitHubContext to use a copy of context parameters and im…
MariusStorhaug Dec 12, 2024
076ed44
Refactor Connect-GitHubApp to use Clone method for context parameters…
MariusStorhaug Dec 12, 2024
ba7660c
test
MariusStorhaug Dec 12, 2024
92b31d8
Fix context parameter usage in Set-GitHubContext and enhance test ver…
MariusStorhaug Dec 12, 2024
d7ce140
test
MariusStorhaug Dec 12, 2024
6be9873
Fix tests
MariusStorhaug Dec 12, 2024
0e2067b
Re-intro GitHubContext
MariusStorhaug Dec 12, 2024
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
11 changes: 3 additions & 8 deletions Coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<table>
<tr>
<td>Available functions</td>
<td>985</td>
<td>980</td>
</tr>
<tr>
<td>Covered functions</td>
<td>155</td>
</tr>
<tr>
<td>Missing functions</td>
<td>830</td>
<td>825</td>
</tr>
<tr>
<td>Coverage</td>
<td>15.74%</td>
<td>15.82%</td>
</tr>
</table>

Expand Down Expand Up @@ -52,13 +52,8 @@
| `/codes_of_conduct` | | :x: | | | |
| `/codes_of_conduct/{key}` | | :x: | | | |
| `/emojis` | | :white_check_mark: | | | |
| `/enterprises/{enterprise}/copilot/billing/seats` | | :x: | | | |
| `/enterprises/{enterprise}/copilot/metrics` | | :x: | | | |
| `/enterprises/{enterprise}/copilot/usage` | | :x: | | | |
| `/enterprises/{enterprise}/dependabot/alerts` | | :x: | | | |
| `/enterprises/{enterprise}/secret-scanning/alerts` | | :x: | | | |
| `/enterprises/{enterprise}/team/{team_slug}/copilot/metrics` | | :x: | | | |
| `/enterprises/{enterprise}/team/{team_slug}/copilot/usage` | | :x: | | | |
| `/events` | | :x: | | | |
| `/feeds` | | :x: | | | |
| `/gists` | | :x: | | :x: | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
[string] $OwnerType

# The permissions that the app is requesting on the target
[string[]] $Permissions
[pscustomobject] $Permissions

# The events that the app is subscribing to once installed
[string[]] $Events

# Simple parameterless constructor
AppGitHubContext() {}

# Creates a context object from a hashtable of key-vaule pairs.
AppGitHubContext([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[int] $InstallationID

# The permissions that the app is requesting on the target
[string[]] $Permissions
[pscustomobject] $Permissions

# The events that the app is subscribing to once installed
[string[]] $Events
Expand All @@ -21,6 +21,9 @@
# The target login of the installation.
[string] $TargetName

# Simple parameterless constructor
InstallationGitHubContext() {}

# Creates a context object from a hashtable of key-vaule pairs.
InstallationGitHubContext([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# 2024-01-01-00:00:00
[datetime] $RefreshTokenExpirationDate

# Simple parameterless constructor
UserGitHubContext() {}

# Creates a context object from a hashtable of key-vaule pairs.
UserGitHubContext([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
Expand Down
35 changes: 7 additions & 28 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 @@ -181,19 +175,6 @@
if (-not $customTokenProvided -and $gitHubTokenPresent) {
$authType = 'Token'
$Token = $gitHubToken
$gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
'Enterprise: ' + $gitHubEvent.enterprise.slug
'Organization: ' + $gitHubEvent.organization.login
'Repository: ' + $gitHubEvent.repository.name
'Repository Owner: ' + $gitHubEvent.repository.owner.login
'Repository Owner Type: ' + $gitHubEvent.repository.owner.type
'Sender: ' + $gitHubEvent.sender.login

$Enterprise = [string]$gitHubEvent.enterprise.slug
$TargetType = [string]$gitHubEvent.repository.owner.type
$TargetName = [string]$gitHubEvent.repository.owner.login
$Owner = [string]$gitHubEvent.repository.owner.login
$Repo = [string]$gitHubEvent.repository.name
}
}

Expand Down Expand Up @@ -299,10 +280,8 @@
'ghs' {
Write-Verbose 'Logging in using an installation access token...'
$context += @{
Token = ConvertTo-SecureString -AsPlainText $Token
TokenType = $tokenType
TargetType = $TargetType
TargetName = $TargetName
Token = ConvertTo-SecureString -AsPlainText $Token
TokenType = $tokenType
}
$context['AuthType'] = 'IAT'
}
Expand All @@ -322,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
146 changes: 68 additions & 78 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,96 +64,86 @@
# 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)
)

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

$Context = $Context | Resolve-GitHubContext
$Context | Assert-GitHubContext -AuthType 'App'
process {
try {
$Context = $Context | Resolve-GitHubContext
$Context | Assert-GitHubContext -AuthType 'App'

try {
$defaultContextData = @{
ApiBaseUri = $Context.ApiBaseUri
ApiVersion = $Context.ApiVersion
HostName = $Context.HostName
ClientID = $Context.ClientID
AuthType = 'IAT'
TokenType = 'ghs'
}

$installations = Get-GitHubAppInstallation
Write-Verbose "Found [$($installations.Count)] installations."
switch ($PSCmdlet.ParameterSetName) {
'User' {
Write-Verbose "Filtering installations for user [$User]."
$installations = $installations | Where-Object { $_.target_type -eq 'User' -and $_.account.login -in $User }
}
'Organization' {
Write-Verbose "Filtering installations for organization [$Organization]."
$installations = $installations | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -in $Organization }
}
'Enterprise' {
Write-Verbose "Filtering installations for enterprise [$Enterprise]."
$installations = $installations | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -in $Enterprise }
}
}

Write-Verbose "Found [$($installations.Count)] installations for the target type."
$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
Token = $token.Token
TokenExpirationDate = $token.ExpiresAt
Permissions = $installation.permissions
Events = $installation.events
TargetType = $installation.target_type
}
switch ($installation.target_type) {
$installations = Get-GitHubAppInstallation -Context $Context
Write-Verbose "Found [$($installations.Count)] installations."
switch ($PSCmdlet.ParameterSetName) {
'User' {
$contextParams += @{
TargetName = $installation.account.login
}
Write-Verbose "Filtering installations for user [$User]."
$installations = $installations | Where-Object { $_.target_type -eq 'User' -and $_.account.login -in $User }
}
'Organization' {
$contextParams += @{
TargetName = $installation.account.login
}
Write-Verbose "Filtering installations for organization [$Organization]."
$installations = $installations | Where-Object { $_.target_type -eq 'Organization' -and $_.account.login -in $Organization }
}
'Enterprise' {
$contextParams += @{
TargetName = $installation.account.slug
}
Write-Verbose "Filtering installations for enterprise [$Enterprise]."
$installations = $installations | Where-Object { $_.target_type -eq 'Enterprise' -and $_.account.slug -in $Enterprise }
}
}
Write-Verbose 'Logging in using an installation access token...'
Write-Verbose ($contextParams | Format-Table | Out-String)
$tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams -PassThru))
Write-Verbose ($tmpContext | Format-List | Out-String)
if (-not $Silent) {
$name = $tmpContext.name
Write-Host "Connected $name"

Write-Verbose "Found [$($installations.Count)] installations for the target."
$installations | ForEach-Object {
$installation = $_
Write-Verbose "Processing installation [$($installation.account.login)] [$($installation.id)]"
$token = New-GitHubAppInstallationAccessToken -Context $Context -InstallationID $installation.id

$contextParams = @{
AuthType = [string]'IAT'
TokenType = [string]'ghs'
DisplayName = [string]$Context.DisplayName
ApiBaseUri = [string]$Context.ApiBaseUri
ApiVersion = [string]$Context.ApiVersion
HostName = [string]$Context.HostName
ClientID = [string]$Context.ClientID
InstallationID = [string]$installation.id
Permissions = [pscustomobject]$installation.permissions
Events = [string[]]$installation.events
TargetType = [string]$installation.target_type
Token = [securestring]$token.Token
TokenExpirationDate = [string]$token.ExpiresAt
}

switch ($installation.target_type) {
'User' {
$contextParams['TargetName'] = $installation.account.login
}
'Organization' {
$contextParams['TargetName'] = $installation.account.login
}
'Enterprise' {
$contextParams['TargetName'] = $installation.account.slug
}
}
Write-Verbose 'Logging in using a managed installation access token...'
Write-Verbose ($contextParams | Format-Table | Out-String)
$tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru))
Write-Verbose ($tmpContext | Format-List | Out-String)
if (-not $Silent) {
$name = $tmpContext.name
Write-Host "Connected $name"
}
$contextParams.Clear()
}
} catch {
Write-Error $_
Write-Error (Get-PSCallStack | Format-Table | Out-String)
throw 'Failed to connect to GitHub using a GitHub App.'
}
} catch {
Write-Error $_
Write-Error (Get-PSCallStack | Format-Table | Out-String)
throw 'Failed to connect to GitHub using a GitHub App.'
}
Write-Verbose "[$commandName] - End"
end {
Write-Verbose "[$commandName] - End"
}
}
Loading