From 496d4fae867e13f691b523a7f5e0cb36b42c80e9 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 11 Dec 2024 21:59:48 +0100 Subject: [PATCH 01/30] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Enhance=20GitHu?= =?UTF-8?q?b=20account=20connection=20details=20and=20improve=20context=20?= =?UTF-8?q?handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../public/Auth/Connect-GitHubAccount.ps1 | 31 ++++++++++++------- tests/GitHub.Tests.ps1 | 4 --- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index ab62822de..ffac8c5ac 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -182,22 +182,29 @@ $authType = 'Token' $Token = $gitHubToken $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json - $TargetType = $gitHubEvent.repository.owner.type - $Enterprise = $gitHubEvent.enterprise.slug - $TargetName = $env:GITHUB_REPOSITORY_OWNER - $Owner = $env:GITHUB_REPOSITORY_OWNER - $Repo = $env:GITHUB_REPOSITORY_NAME + '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 } } $context = @{ - ApiBaseUri = $ApiBaseUri - ApiVersion = $ApiVersion - HostName = $HostName - AuthType = $authType - Enterprise = $Enterprise - Owner = $Owner - Repo = $Repo + ApiBaseUri = [string]$ApiBaseUri + ApiVersion = [string]$ApiVersion + HostName = [string]$HostName + AuthType = [string]$authType + Enterprise = [string]$Enterprise + Owner = [string]$Owner + Repo = [string]$Repo } Write-Verbose ($context | Format-Table | Out-String) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 0f09d7d4a..8faeacb6a 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -92,10 +92,6 @@ Describe 'GitHub' { Get-GitHubConfig -Name 'DefaultContext' | Should -Be 'github.com/github-actions/Organization/PSModule' } - # It 'Can be called with a GitHub App Installation Access Token' { - # { Connect-GitHubAccount -Token $env:TEST_APP_INSTALLATION_ACCESS_TOKEN } | Should -Not -Throw - # } - It 'Get-GitHubViewer can be called' { Get-GitHubViewer | Should -Not -BeNullOrEmpty } From 270f4c58cb9c0fca5078169c929008003ae81264 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 11 Dec 2024 23:38:27 +0100 Subject: [PATCH 02/30] Get app stuff... --- .../public/Auth/Connect-GitHubAccount.ps1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index ffac8c5ac..95dd40075 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -182,12 +182,12 @@ $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 + 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) $Enterprise = [string]$gitHubEvent.enterprise.slug $TargetType = [string]$gitHubEvent.repository.owner.type @@ -301,8 +301,8 @@ $context += @{ Token = ConvertTo-SecureString -AsPlainText $Token TokenType = $tokenType - TargetType = $TargetType - TargetName = $TargetName + TargetType = [string]$TargetType + TargetName = [string]$TargetName } $context['AuthType'] = 'IAT' } From 4c8496e6e6e1ea9524f03eaf226dfecc07e3d238 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 11 Dec 2024 23:39:21 +0100 Subject: [PATCH 03/30] qwe --- src/functions/public/Auth/Connect-GitHubAccount.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 1c7d80dbb..95dd40075 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -188,6 +188,7 @@ Write-Verbose ('Repository Owner: ' + $gitHubEvent.repository.owner.login) Write-Verbose ('Repository Owner Type: ' + $gitHubEvent.repository.owner.type) Write-Verbose ('Sender: ' + $gitHubEvent.sender.login) + $Enterprise = [string]$gitHubEvent.enterprise.slug $TargetType = [string]$gitHubEvent.repository.owner.type $TargetName = [string]$gitHubEvent.repository.owner.login From dc65b0438e0335fe0848a18f67a3728a92b663ef Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:40:32 +0000 Subject: [PATCH 04/30] Auto-generated changes --- Coverage.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Coverage.md b/Coverage.md index 984361d8a..5dc35128d 100644 --- a/Coverage.md +++ b/Coverage.md @@ -5,7 +5,7 @@ - + @@ -13,11 +13,11 @@ - + - +
Available functions985980
Covered functions
Missing functions830825
Coverage15.74%15.82%
@@ -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: | | From e5b5513c7335cc189ad3f475e1768baea6e529f2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 00:24:08 +0100 Subject: [PATCH 05/30] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20GitHub?= =?UTF-8?q?=20authentication=20context=20and=20workflow=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/Process-PSModule.yml | 2 +- .../public/Auth/Connect-GitHubAccount.ps1 | 19 ++---------------- .../public/Auth/Context/Set-GitHubContext.ps1 | 20 +++++++++++++++---- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 0739b4def..006d43c84 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -35,6 +35,6 @@ jobs: uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v3 secrets: inherit with: - SkipTests: SourceCode + SkipTests: SourceCode, Module Debug: true Verbose: true diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 95dd40075..9d35ef727 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -181,19 +181,6 @@ if (-not $customTokenProvided -and $gitHubTokenPresent) { $authType = 'Token' $Token = $gitHubToken - $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json - 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) - - $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 } } @@ -299,10 +286,8 @@ 'ghs' { Write-Verbose 'Logging in using an installation access token...' $context += @{ - Token = ConvertTo-SecureString -AsPlainText $Token - TokenType = $tokenType - TargetType = [string]$TargetType - TargetName = [string]$TargetName + Token = ConvertTo-SecureString -AsPlainText $Token + TokenType = $tokenType } $context['AuthType'] = 'IAT' } diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 590e5983f..22c1b39a3 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -55,7 +55,7 @@ function Set-GitHubContext { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - $login = [string]$viewer.login + $login = [string]$viewer.login -Replace '\[bot\]' $Context += @{ DisplayName = [string]$viewer.name Username = $login @@ -71,10 +71,22 @@ function Set-GitHubContext { } } 'IAT' { - $ContextName = "$($Context['HostName'])/$login/$($Context.TargetType)/$($Context.TargetName)" -Replace '\[bot\]' + $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json + 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/$appSlup/$($Context.TargetName)" $Context += @{ - Name = $ContextName - Type = 'Installation' + Name = $ContextName + Type = 'Installation' + $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 } } 'App' { From 3bfda65133dd156abf88bfb0f850f189778ba243 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 00:40:42 +0100 Subject: [PATCH 06/30] Enhance GitHub context setup by including app details and refining context name structure --- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 22c1b39a3..cea37d015 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -72,15 +72,19 @@ function Set-GitHubContext { } 'IAT' { $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json + $app = Get-GitHubApp -AppSlug $login + $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/$appSlup/$($Context.TargetName)" + $ContextName = "$($Context['HostName'])/$login/$targetType/$targetName" $Context += @{ Name = $ContextName + DisplayName = [string]$app.name Type = 'Installation' $Enterprise = [string]$gitHubEvent.enterprise.slug $TargetType = [string]$gitHubEvent.repository.owner.type From 597c8883fcd155dc9ec2fb5cd0abd3e454653361 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 00:48:40 +0100 Subject: [PATCH 07/30] Fix Context on Get-GitHubApp --- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index cea37d015..b30239898 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -72,7 +72,7 @@ function Set-GitHubContext { } 'IAT' { $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json - $app = Get-GitHubApp -AppSlug $login + $app = Get-GitHubApp -AppSlug $login -Context $Context $targetType = $gitHubEvent.repository.owner.type $targetName = $gitHubEvent.repository.owner.login Write-Verbose ('Enterprise: ' + $gitHubEvent.enterprise.slug) From 7b9dc4554f5bb3890ef1955c3eb587d77edcace2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 01:02:26 +0100 Subject: [PATCH 08/30] Refactor GitHub context setup to use direct assignment for context properties --- .../public/Auth/Context/Set-GitHubContext.ps1 | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index b30239898..97c503506 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -56,19 +56,15 @@ function Set-GitHubContext { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } $login = [string]$viewer.login -Replace '\[bot\]' - $Context += @{ - DisplayName = [string]$viewer.name - Username = $login - NodeID = [string]$viewer.id - DatabaseID = [string]$viewer.databaseId - } + $Context['DisplayName'] = [string]$viewer.name + $Context['Username'] = $login + $Context['NodeID'] = [string]$viewer.id + $Context['DatabaseID'] = [string]$viewer.databaseId } 'PAT|UAT' { $ContextName = "$($Context['HostName'])/$login" - $Context += @{ - Name = $ContextName - Type = 'User' - } + $Context['Name'] = $ContextName + $Context['Type'] = 'User' } 'IAT' { $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json @@ -82,16 +78,14 @@ function Set-GitHubContext { Write-Verbose ('Repository Owner Type: ' + $gitHubEvent.repository.owner.type) Write-Verbose ('Sender: ' + $gitHubEvent.sender.login) $ContextName = "$($Context['HostName'])/$login/$targetType/$targetName" - $Context += @{ - Name = $ContextName - DisplayName = [string]$app.name - Type = 'Installation' - $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 - } + $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 } 'App' { $app = Get-GitHubApp -Context $Context From ae24721a79d7c9e57052fe158fccfa4b2b3911bf Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 01:06:44 +0100 Subject: [PATCH 09/30] Refactor Set-GitHubContext to use direct assignment for context properties --- .../public/Auth/Context/Set-GitHubContext.ps1 | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 97c503506..927a16010 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -90,18 +90,16 @@ function Set-GitHubContext { 'App' { $app = Get-GitHubApp -Context $Context $ContextName = "$($Context['HostName'])/$($app.slug)" - $Context += @{ - Name = $ContextName - DisplayName = [string]$app.name - Username = [string]$app.slug - NodeID = [string]$app.node_id - DatabaseID = [string]$app.id - Permissions = [string]$app.permissions - Events = [string]$app.events - OwnerName = [string]$app.owner.login - OwnerType = [string]$app.owner.type - Type = 'App' - } + $Context['Name'] = $ContextName + $Context['DisplayName'] = [string]$app.name + $Context['Username'] = [string]$app.slug + $Context['NodeID'] = [string]$app.node_id + $Context['DatabaseID'] = [string]$app.id + $Context['Permissions'] = [string]$app.permissions + $Context['Events'] = [string]$app.events + $Context['OwnerName'] = [string]$app.owner.login + $Context['OwnerType'] = [string]$app.owner.type + $Context['Type'] = 'App' } default { throw 'Failed to get info on the context. Unknown logon type.' From 1be02d23e66badd96ecdb86c70211dace6a12200 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 01:25:06 +0100 Subject: [PATCH 10/30] Refactor Connect-GitHubApp to improve context handling and streamline installation filtering --- .github/workflows/Process-PSModule.yml | 2 +- .../public/Auth/Connect-GitHubApp.ps1 | 144 +++++++++--------- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 006d43c84..0739b4def 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -35,6 +35,6 @@ jobs: uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v3 secrets: inherit with: - SkipTests: SourceCode, Module + SkipTests: SourceCode Debug: true Verbose: true diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index c6751d7da..833ef4f14 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -71,89 +71,89 @@ [switch] $Shallow ) - $commandName = $MyInvocation.MyCommand.Name - Write-Verbose "[$commandName] - Start" - - $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' - } + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + } - $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 } + process { + try { + $Context = $Context | Resolve-GitHubContext + $Context | Assert-GitHubContext -AuthType 'App' + + $defaultContextData = @{ + ApiBaseUri = $Context.ApiBaseUri + ApiVersion = $Context.ApiVersion + HostName = $Context.HostName + ClientID = $Context.ClientID + AuthType = 'IAT' + TokenType = 'ghs' } - '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 + 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 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 + $contextParams['Token'] = $token.Token + $contextParams['TokenExpirationDate'] = $token.ExpiresAt + $contextParams['Permissions'] = $installation.permissions + $contextParams['Events'] = $installation.events + $contextParams['TargetType'] = $installation.target_type + + 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 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" + } } + } 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" + } } From 665a603f579366656442cd8d3612ee936d193693 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 09:28:43 +0100 Subject: [PATCH 11/30] test --- .github/workflows/Process-PSModule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 0739b4def..006d43c84 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -35,6 +35,6 @@ jobs: uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v3 secrets: inherit with: - SkipTests: SourceCode + SkipTests: SourceCode, Module Debug: true Verbose: true From cd83fa1a3a50b43b29af4ed753b33e5f70ab5af6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 09:41:38 +0100 Subject: [PATCH 12/30] Update Process-PSModule workflow to skip only SourceCode tests --- .github/workflows/Process-PSModule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 006d43c84..0739b4def 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -35,6 +35,6 @@ jobs: uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v3 secrets: inherit with: - SkipTests: SourceCode, Module + SkipTests: SourceCode Debug: true Verbose: true From 5157db13ce1bf71b9e6fae39944e6eb5b0787e3f Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 10:31:50 +0100 Subject: [PATCH 13/30] Fixes --- .../public/Auth/Connect-GitHubAccount.ps1 | 16 ++---- .../public/Auth/Connect-GitHubApp.ps1 | 42 ++++++--------- .../public/Auth/Context/Set-GitHubContext.ps1 | 53 +++++++++++-------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 9d35ef727..725b6338e 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -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()] @@ -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 { diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 833ef4f14..b5de7fb5e 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -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 { @@ -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 @@ -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' { diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 927a16010..706d94149 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -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 From 3800936636593bb86531235fff3ea7ba0666874e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 10:54:31 +0100 Subject: [PATCH 14/30] Refactor GitHub.Tests to use parameter splatting for Connect-GitHubAccount and improve context count assertions --- tests/GitHub.Tests.ps1 | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 8faeacb6a..996a2da15 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -50,21 +50,26 @@ Describe 'GitHub' { } It 'Can be called with a GitHub App' { - { Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY } | Should -Not -Throw - { Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY } | Should -Not -Throw - Write-Verbose (Get-GitHubContext | Out-String) -Verbose - } - - It 'Can list all contexts' { - Write-Verbose (Get-GitHubContext -ListAvailable | Out-String) -Verbose + $params = @{ + ClientID = $env:TEST_APP_CLIENT_ID + PrivateKey = $env:TEST_APP_PRIVATE_KEY + } + { Connect-GitHubAccount @params } | Should -Not -Throw + (Get-GitHubContext -ListAvailable).Count | Should -Be 3 + } + It 'Can be called with a GitHub App and autoload installations' { + $params = @{ + ClientID = $env:TEST_APP_CLIENT_ID + PrivateKey = $env:TEST_APP_PRIVATE_KEY + } + { Connect-GitHubAccount @params -AutoloadInstallations } | Should -Not -Throw (Get-GitHubContext -ListAvailable).Count | Should -Be 7 } It 'Can disconnect a specific context' { { Disconnect-GitHubAccount -Context 'github.com/github-actions/Organization/PSModule' -Silent } | Should -Not -Throw (Get-GitHubContext -ListAvailable).Count | Should -Be 6 - Connect-GitHubAccount - Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY + Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY -AutoloadInstallations (Get-GitHubContext -ListAvailable).Count | Should -Be 7 } From 6d27b5d60eefcc62ad0059c29049161b7169e756 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 12:03:50 +0100 Subject: [PATCH 15/30] Fix --- .../GitHubContext/AppGitHubContext.ps1 | 2 +- .../InstallationGitHubContext.ps1 | 2 +- .../public/Auth/Connect-GitHubApp.ps1 | 32 +++++++++---------- .../public/Auth/Context/Set-GitHubContext.ps1 | 6 ++-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 index 3bd58725f..6e7029960 100644 --- a/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 @@ -9,7 +9,7 @@ [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 diff --git a/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 index d4a97368c..d7a874295 100644 --- a/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 @@ -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 diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index b5de7fb5e..9885e1996 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -77,16 +77,6 @@ $Context = $Context | Resolve-GitHubContext $Context | Assert-GitHubContext -AuthType 'App' - $defaultContextData = @{ - 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 Write-Verbose "Found [$($installations.Count)] installations." switch ($PSCmdlet.ParameterSetName) { @@ -107,15 +97,23 @@ Write-Verbose "Found [$($installations.Count)] installations for the target type." $installations | ForEach-Object { $installation = $_ - $contextParams = @{} + $defaultContextData.Clone() $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 + $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 = [string]$installation.permissions + Events = [string]$installation.events + TargetType = [string]$installation.target_type + Token = [string]$token.Token + TokenExpirationDate = [string]$token.ExpiresAt + } switch ($installation.target_type) { 'User' { diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 706d94149..231c40493 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -55,7 +55,7 @@ function Set-GitHubContext { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - $login = [string]$viewer.login -Replace '\[bot\]' + $login = [string]($viewer.login -Replace '\[bot\]') $Context['DisplayName'] = [string]$viewer.name $Context['Username'] = $login $Context['NodeID'] = [string]$viewer.id @@ -104,8 +104,8 @@ function Set-GitHubContext { $Context['Username'] = [string]$app.slug $Context['NodeID'] = [string]$app.node_id $Context['DatabaseID'] = [string]$app.id - $Context['Permissions'] = [string]$app.permissions - $Context['Events'] = [string]$app.events + $Context['Permissions'] = [PSCustomObject]$app.permissions + $Context['Events'] = [string[]]$app.events $Context['OwnerName'] = [string]$app.owner.login $Context['OwnerType'] = [string]$app.owner.type $Context['Type'] = 'App' From e90c72b8aa56485ecefa8c15b3d9beda382671f1 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 13:29:57 +0100 Subject: [PATCH 16/30] Refactor Connect-GitHubApp to use appropriate types for Permissions, Events, and Token --- src/functions/public/Auth/Connect-GitHubApp.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 9885e1996..6248213ea 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -108,10 +108,10 @@ HostName = [string]$Context.HostName ClientID = [string]$Context.ClientID InstallationID = [string]$installation.id - Permissions = [string]$installation.permissions - Events = [string]$installation.events + Permissions = [pscustomobject]$installation.permissions + Events = [string[]]$installation.events TargetType = [string]$installation.target_type - Token = [string]$token.Token + Token = [securestring]$token.Token TokenExpirationDate = [string]$token.ExpiresAt } From 4ffe53d375ede039d0fe5dadb474683a72a27e78 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 15:41:59 +0100 Subject: [PATCH 17/30] Add XML format definitions for GitHubContext and UserGitHubContext --- .../formats/AppGitHubContext.Format.ps1xml | 0 examples/formats/GitHubContext.Format.ps1xml | 113 ++++++++++++++++++ .../InstallationGitHubContext.Format.ps1xml | 113 ++++++++++++++++++ .../formats/UserGitHubContext.Format.ps1xml | 113 ++++++++++++++++++ 4 files changed, 339 insertions(+) rename src/formats/GitHubContext.Format.ps1xml => examples/formats/AppGitHubContext.Format.ps1xml (100%) create mode 100644 examples/formats/GitHubContext.Format.ps1xml create mode 100644 examples/formats/InstallationGitHubContext.Format.ps1xml create mode 100644 examples/formats/UserGitHubContext.Format.ps1xml diff --git a/src/formats/GitHubContext.Format.ps1xml b/examples/formats/AppGitHubContext.Format.ps1xml similarity index 100% rename from src/formats/GitHubContext.Format.ps1xml rename to examples/formats/AppGitHubContext.Format.ps1xml diff --git a/examples/formats/GitHubContext.Format.ps1xml b/examples/formats/GitHubContext.Format.ps1xml new file mode 100644 index 000000000..638e3d673 --- /dev/null +++ b/examples/formats/GitHubContext.Format.ps1xml @@ -0,0 +1,113 @@ + + + + + + GitHubContextTableView + + GitHubContext + + + + + + + + + + + + + + + + + + + + + Name + + + AuthType + + + TokenType + + + TokenExpirationDate + + + + + + + + + + GitHubContextListView + + GitHubContext + + + + + + + HostName + + + UserName + + + AuthType + + + TokenType + + + TokenExpirationDate + + + Repo + + + Scope + + + ApiBaseUri + + + ApiVersion + + + AuthClientID + + + ClientID + + + DeviceFlowType + + + NodeID + + + DatabaseID + + + ID + + + Owner + + + RefreshTokenExpirationDate + + + + + + + + diff --git a/examples/formats/InstallationGitHubContext.Format.ps1xml b/examples/formats/InstallationGitHubContext.Format.ps1xml new file mode 100644 index 000000000..638e3d673 --- /dev/null +++ b/examples/formats/InstallationGitHubContext.Format.ps1xml @@ -0,0 +1,113 @@ + + + + + + GitHubContextTableView + + GitHubContext + + + + + + + + + + + + + + + + + + + + + Name + + + AuthType + + + TokenType + + + TokenExpirationDate + + + + + + + + + + GitHubContextListView + + GitHubContext + + + + + + + HostName + + + UserName + + + AuthType + + + TokenType + + + TokenExpirationDate + + + Repo + + + Scope + + + ApiBaseUri + + + ApiVersion + + + AuthClientID + + + ClientID + + + DeviceFlowType + + + NodeID + + + DatabaseID + + + ID + + + Owner + + + RefreshTokenExpirationDate + + + + + + + + diff --git a/examples/formats/UserGitHubContext.Format.ps1xml b/examples/formats/UserGitHubContext.Format.ps1xml new file mode 100644 index 000000000..638e3d673 --- /dev/null +++ b/examples/formats/UserGitHubContext.Format.ps1xml @@ -0,0 +1,113 @@ + + + + + + GitHubContextTableView + + GitHubContext + + + + + + + + + + + + + + + + + + + + + Name + + + AuthType + + + TokenType + + + TokenExpirationDate + + + + + + + + + + GitHubContextListView + + GitHubContext + + + + + + + HostName + + + UserName + + + AuthType + + + TokenType + + + TokenExpirationDate + + + Repo + + + Scope + + + ApiBaseUri + + + ApiVersion + + + AuthClientID + + + ClientID + + + DeviceFlowType + + + NodeID + + + DatabaseID + + + ID + + + Owner + + + RefreshTokenExpirationDate + + + + + + + + From f5150963f4569332a0ae0e36aef0f2311f147eea Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 15:42:17 +0100 Subject: [PATCH 18/30] Add parameterless constructors to GitHubContext classes --- src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 | 3 +++ .../public/Context/GitHubContext/InstallationGitHubContext.ps1 | 3 +++ src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 index 6e7029960..68a010afd 100644 --- a/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/AppGitHubContext.ps1 @@ -14,6 +14,9 @@ # 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) { diff --git a/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 index d7a874295..ed02de33c 100644 --- a/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1 @@ -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) { diff --git a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 index d0fa35ebf..345aaf867 100644 --- a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 @@ -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) { From efba876344a04bc80a94f69a33d0e9ec8f7b9710 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 15:43:31 +0100 Subject: [PATCH 19/30] Improve Set-GitHubContext to conditionally update context properties based on existing values --- .../public/Auth/Context/Set-GitHubContext.ps1 | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 231c40493..6816bb781 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -55,11 +55,19 @@ function Set-GitHubContext { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - $login = [string]($viewer.login -Replace '\[bot\]') - $Context['DisplayName'] = [string]$viewer.name - $Context['Username'] = $login - $Context['NodeID'] = [string]$viewer.id - $Context['DatabaseID'] = [string]$viewer.databaseId + if ($null -ne $Context['DisplayName']) { + $Context['DisplayName'] = [string]$viewer.name + } + if ($null -ne $Context['Username']) { + $login = [string]($viewer.login -Replace '\[bot\]') + $Context['Username'] = $login + } + if ($null -ne $Context['NodeID']) { + $Context['NodeID'] = [string]$viewer.id + } + if ($null -ne $Context['DatabaseID']) { + $Context['DatabaseID'] = [string]$viewer.databaseId + } } 'PAT|UAT' { $contextName = "$($Context['HostName'])/$login" @@ -87,19 +95,17 @@ function Set-GitHubContext { Write-Verbose "Sender: $gh_sender" $Context['Enterprise'] = [string]$enterprise $Context['TargetType'] = [string]$targetType - $Context['TargetName'] = [string]$owner + $Context['TargetName'] = [string]$targetName $Context['Owner'] = [string]$owner $Context['Repo'] = [string]$repo - $contextName = "$($Context['HostName'])/$login/$targetType/$targetName" - $Context['Name'] = $contextName + $Context['Name'] = "$($Context['HostName'])/$($Context['Username'])/$($Context['TargetType'])/$($Context['TargetName'])" } else { - $login = $Context['Username'] + $Context['Name'] = "$($Context['HostName'])/$($Context['Username'])/$($Context['TargetType'])/$($Context['TargetName'])" } } 'App' { $app = Get-GitHubApp -Context $Context - $contextName = "$($Context['HostName'])/$($app.slug)" - $Context['Name'] = $contextName + $Context['Name'] = "$($Context['HostName'])/$($app.slug)" $Context['DisplayName'] = [string]$app.name $Context['Username'] = [string]$app.slug $Context['NodeID'] = [string]$app.node_id From f02d11abb2ac3cbbe4116d638712190d83195f93 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 15:44:26 +0100 Subject: [PATCH 20/30] Refactor Connect-GitHubApp to use context in Get-GitHubAppInstallation and New-GitHubAppInstallationAccessToken calls --- src/functions/public/Auth/Connect-GitHubApp.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 6248213ea..dc9663ac0 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -77,7 +77,7 @@ $Context = $Context | Resolve-GitHubContext $Context | Assert-GitHubContext -AuthType 'App' - $installations = Get-GitHubAppInstallation + $installations = Get-GitHubAppInstallation -Context $Context Write-Verbose "Found [$($installations.Count)] installations." switch ($PSCmdlet.ParameterSetName) { 'User' { @@ -94,15 +94,15 @@ } } - Write-Verbose "Found [$($installations.Count)] installations for the target type." + Write-Verbose "Found [$($installations.Count)] installations for the target." $installations | ForEach-Object { $installation = $_ - $token = New-GitHubAppInstallationAccessToken -InstallationID $installation.id + $token = New-GitHubAppInstallationAccessToken -Context $Context -InstallationID $installation.id $contextParams = @{ AuthType = [string]'IAT' TokenType = [string]'ghs' - DisplayNAme = [string]$Context.DisplayName + DisplayName = [string]$Context.DisplayName ApiBaseUri = [string]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName From bcbf4b51167274688b5470f79816fc0bbe8234ad Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 16:43:37 +0100 Subject: [PATCH 21/30] Fix Set-GitHubContext to set context properties only if they are null --- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 6816bb781..855536961 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -55,17 +55,17 @@ function Set-GitHubContext { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - if ($null -ne $Context['DisplayName']) { + if ($null -eq $Context['DisplayName']) { $Context['DisplayName'] = [string]$viewer.name } - if ($null -ne $Context['Username']) { + if ($null -eq $Context['Username']) { $login = [string]($viewer.login -Replace '\[bot\]') $Context['Username'] = $login } - if ($null -ne $Context['NodeID']) { + if ($null -eq $Context['NodeID']) { $Context['NodeID'] = [string]$viewer.id } - if ($null -ne $Context['DatabaseID']) { + if ($null -eq $Context['DatabaseID']) { $Context['DatabaseID'] = [string]$viewer.databaseId } } From f0e753634e586f07eb9d5be079e35db7f3789d32 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 16:49:44 +0100 Subject: [PATCH 22/30] Add conditional assignment for DisplayName in Set-GitHubContext to handle null values --- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 855536961..bd777f249 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -76,7 +76,14 @@ function Set-GitHubContext { } 'IAT' { $Context['Type'] = 'Installation' - $Context['DisplayName'] = [string]$app.name + if ($null -eq $Context['DisplayName']) { + try { + $app = Get-GitHubApp -AppSlug $Context['Name'] -Context $Context + } catch { + Write-Warning "Failed to get the GitHub App with the slug: [$($Context['Name'])]." + } + $Context['DisplayName'] = [string]$app.name + } if ($script:GitHub.EnvironmentType -eq 'GHA') { $gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json From dd180d627006a04cf36ded3f4f9a85e649114565 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 17:36:07 +0100 Subject: [PATCH 23/30] Update Set-GitHubContext to use IsNullOrEmpty for better null handling of context properties --- .../public/Auth/Context/Set-GitHubContext.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index bd777f249..83063837a 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -55,17 +55,17 @@ function Set-GitHubContext { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $Context $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - if ($null -eq $Context['DisplayName']) { + if ([string]::IsNullOrEmpty($Context['DisplayName'])) { $Context['DisplayName'] = [string]$viewer.name } - if ($null -eq $Context['Username']) { + if ([string]::IsNullOrEmpty($Context['Username'])) { $login = [string]($viewer.login -Replace '\[bot\]') $Context['Username'] = $login } - if ($null -eq $Context['NodeID']) { + if ([string]::IsNullOrEmpty($Context['NodeID'])) { $Context['NodeID'] = [string]$viewer.id } - if ($null -eq $Context['DatabaseID']) { + if ([string]::IsNullOrEmpty($Context['DatabaseID'])) { $Context['DatabaseID'] = [string]$viewer.databaseId } } @@ -76,9 +76,9 @@ function Set-GitHubContext { } 'IAT' { $Context['Type'] = 'Installation' - if ($null -eq $Context['DisplayName']) { + if ([string]::IsNullOrEmpty($Context['DisplayName'])) { try { - $app = Get-GitHubApp -AppSlug $Context['Name'] -Context $Context + $app = Get-GitHubApp -AppSlug $Context['Name'] -Context $Context } catch { Write-Warning "Failed to get the GitHub App with the slug: [$($Context['Name'])]." } From 81051b4555282df9184c5ae18804907e4acf4346 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 18:15:04 +0100 Subject: [PATCH 24/30] Refactor Set-GitHubContext to use a copy of context parameters and improve verbose logging for better clarity --- .../public/Auth/Connect-GitHubApp.ps1 | 4 +- .../public/Auth/Context/Set-GitHubContext.ps1 | 95 ++++++++++--------- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index dc9663ac0..cbf7d0145 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -126,9 +126,9 @@ $contextParams['TargetName'] = $installation.account.slug } } - Write-Verbose 'Logging in using an installation access token...' + Write-Verbose 'Logging in using a managed installation access token...' Write-Verbose ($contextParams | Format-Table | Out-String) - $tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams -PassThru)) + $tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Copy() -PassThru)) Write-Verbose ($tmpContext | Format-List | Out-String) if (-not $Silent) { $name = $tmpContext.name diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 83063837a..952d604d0 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -42,47 +42,48 @@ function Set-GitHubContext { $commandName = $MyInvocation.MyCommand.Name Write-Verbose "[$commandName] - Start" $null = Get-GitHubConfig + $contextObj = @{} + $Context } process { Write-Verbose 'Context:' - $Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ } + $contextObj | Out-String -Stream | ForEach-Object { Write-Verbose $_ } # Run functions to get info on the temporary context. try { - Write-Verbose "Getting info on the context [$($Context['AuthType'])]." - switch -Regex ($($Context['AuthType'])) { + Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]." + switch -Regex ($($contextObj['AuthType'])) { 'PAT|UAT|IAT' { - $viewer = Get-GitHubViewer -Context $Context + $viewer = Get-GitHubViewer -Context $contextObj $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - if ([string]::IsNullOrEmpty($Context['DisplayName'])) { - $Context['DisplayName'] = [string]$viewer.name + if ([string]::IsNullOrEmpty($contextObj['DisplayName'])) { + $contextObj['DisplayName'] = [string]$viewer.name } - if ([string]::IsNullOrEmpty($Context['Username'])) { + if ([string]::IsNullOrEmpty($contextObj['Username'])) { $login = [string]($viewer.login -Replace '\[bot\]') - $Context['Username'] = $login + $contextObj['Username'] = $login } - if ([string]::IsNullOrEmpty($Context['NodeID'])) { - $Context['NodeID'] = [string]$viewer.id + if ([string]::IsNullOrEmpty($contextObj['NodeID'])) { + $contextObj['NodeID'] = [string]$viewer.id } - if ([string]::IsNullOrEmpty($Context['DatabaseID'])) { - $Context['DatabaseID'] = [string]$viewer.databaseId + if ([string]::IsNullOrEmpty($contextObj['DatabaseID'])) { + $contextObj['DatabaseID'] = [string]$viewer.databaseId } } 'PAT|UAT' { - $contextName = "$($Context['HostName'])/$login" - $Context['Name'] = $contextName - $Context['Type'] = 'User' + $contextName = "$($contextObj['HostName'])/$login" + $contextObj['Name'] = $contextName + $contextObj['Type'] = 'User' } 'IAT' { - $Context['Type'] = 'Installation' - if ([string]::IsNullOrEmpty($Context['DisplayName'])) { + $contextObj['Type'] = 'Installation' + if ([string]::IsNullOrEmpty($contextObj['DisplayName'])) { try { - $app = Get-GitHubApp -AppSlug $Context['Name'] -Context $Context + $app = Get-GitHubApp -AppSlug $contextObj['Name'] -Context $contextObj } catch { - Write-Warning "Failed to get the GitHub App with the slug: [$($Context['Name'])]." + Write-Warning "Failed to get the GitHub App with the slug: [$($contextObj['Name'])]." } - $Context['DisplayName'] = [string]$app.name + $contextObj['DisplayName'] = [string]$app.name } if ($script:GitHub.EnvironmentType -eq 'GHA') { @@ -100,52 +101,54 @@ function Set-GitHubContext { 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]$targetName - $Context['Owner'] = [string]$owner - $Context['Repo'] = [string]$repo - $Context['Name'] = "$($Context['HostName'])/$($Context['Username'])/$($Context['TargetType'])/$($Context['TargetName'])" + $contextObj['Enterprise'] = [string]$enterprise + $contextObj['TargetType'] = [string]$targetType + $contextObj['TargetName'] = [string]$targetName + $contextObj['Owner'] = [string]$owner + $contextObj['Repo'] = [string]$repo + $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/$($contextObj['TargetType'])/$($contextObj['TargetName'])" } else { - $Context['Name'] = "$($Context['HostName'])/$($Context['Username'])/$($Context['TargetType'])/$($Context['TargetName'])" + $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/$($contextObj['TargetType'])/$($contextObj['TargetName'])" } } 'App' { - $app = Get-GitHubApp -Context $Context - $Context['Name'] = "$($Context['HostName'])/$($app.slug)" - $Context['DisplayName'] = [string]$app.name - $Context['Username'] = [string]$app.slug - $Context['NodeID'] = [string]$app.node_id - $Context['DatabaseID'] = [string]$app.id - $Context['Permissions'] = [PSCustomObject]$app.permissions - $Context['Events'] = [string[]]$app.events - $Context['OwnerName'] = [string]$app.owner.login - $Context['OwnerType'] = [string]$app.owner.type - $Context['Type'] = 'App' + $app = Get-GitHubApp -Context $contextObj + $contextObj['Name'] = "$($contextObj['HostName'])/$($app.slug)" + $contextObj['DisplayName'] = [string]$app.name + $contextObj['Username'] = [string]$app.slug + $contextObj['NodeID'] = [string]$app.node_id + $contextObj['DatabaseID'] = [string]$app.id + $contextObj['Permissions'] = [PSCustomObject]$app.permissions + $contextObj['Events'] = [string[]]$app.events + $contextObj['OwnerName'] = [string]$app.owner.login + $contextObj['OwnerType'] = [string]$app.owner.type + $contextObj['Type'] = 'App' } default { throw 'Failed to get info on the context. Unknown logon type.' } } - Write-Verbose "Found [$($Context['Type'])] with login: [$($Context['Name'])]" - $Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ } + Write-Verbose "Found [$($contextObj['Type'])] with login: [$($contextObj['Name'])]" + $contextObj | Out-String -Stream | ForEach-Object { Write-Verbose $_ } Write-Verbose '----------------------------------------------------' if ($PSCmdlet.ShouldProcess('Context', 'Set')) { - Write-Verbose "Saving context: [$($script:GitHub.Config.ID)/$($Context['Name'])]" - Set-Context -ID "$($script:GitHub.Config.ID)/$($Context['Name'])" -Context $Context + Write-Verbose "Saving context: [$($script:GitHub.Config.ID)/$($contextObj['Name'])]" + Set-Context -ID "$($script:GitHub.Config.ID)/$($contextObj['Name'])" -Context $contextObj if ($Default) { - Set-GitHubDefaultContext -Context $Context['Name'] - if ($Context['AuthType'] -eq 'IAT' -and $script:GitHub.EnvironmentType -eq 'GHA') { - Set-GitHubGitConfig -Context $Context['Name'] + Set-GitHubDefaultContext -Context $contextObj['Name'] + if ($contextObj['AuthType'] -eq 'IAT' -and $script:GitHub.EnvironmentType -eq 'GHA') { + Set-GitHubGitConfig -Context $contextObj['Name'] } } if ($PassThru) { - Get-GitHubContext -Context $($Context['Name']) + Get-GitHubContext -Context $($contextObj['Name']) } } } catch { Write-Error $_ | Select-Object * throw 'Failed to set the GitHub context.' + } finally { + $contextObj.Clear() } } From 076ed44fac1f5b48f867dc8036e8b07204071e86 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 18:31:22 +0100 Subject: [PATCH 25/30] Refactor Connect-GitHubApp to use Clone method for context parameters in Set-GitHubContext --- src/functions/public/Auth/Connect-GitHubApp.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index cbf7d0145..3b6ee43e8 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -128,7 +128,7 @@ } Write-Verbose 'Logging in using a managed installation access token...' Write-Verbose ($contextParams | Format-Table | Out-String) - $tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Copy() -PassThru)) + $tmpContext = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru)) Write-Verbose ($tmpContext | Format-List | Out-String) if (-not $Silent) { $name = $tmpContext.name From ba7660ca22f6149075e87939c18746c00a47f086 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 18:49:10 +0100 Subject: [PATCH 26/30] test --- src/functions/public/Auth/Connect-GitHubApp.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 3b6ee43e8..498ab089b 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -97,6 +97,7 @@ 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 = @{ @@ -134,6 +135,7 @@ $name = $tmpContext.name Write-Host "Connected $name" } + $contextParams.Clear() } } catch { Write-Error $_ From 92b31d8d86e6dade02c46ea0bca4ce0f8ee5461a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 20:52:21 +0100 Subject: [PATCH 27/30] Fix context parameter usage in Set-GitHubContext and enhance test verbosity for clarity --- .../public/Auth/Context/Set-GitHubContext.ps1 | 13 +++++++------ tests/GitHub.Tests.ps1 | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 952d604d0..f70277d58 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -79,13 +79,12 @@ function Set-GitHubContext { $contextObj['Type'] = 'Installation' if ([string]::IsNullOrEmpty($contextObj['DisplayName'])) { try { - $app = Get-GitHubApp -AppSlug $contextObj['Name'] -Context $contextObj + $app = Get-GitHubApp -AppSlug $contextObj['Username'] -Context $contextObj + $contextObj['DisplayName'] = [string]$app.name } catch { - Write-Warning "Failed to get the GitHub App with the slug: [$($contextObj['Name'])]." + Write-Warning "Failed to get the GitHub App with the slug: [$($contextObj['Username'])]." } - $contextObj['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 @@ -106,9 +105,11 @@ function Set-GitHubContext { $contextObj['TargetName'] = [string]$targetName $contextObj['Owner'] = [string]$owner $contextObj['Repo'] = [string]$repo - $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/$($contextObj['TargetType'])/$($contextObj['TargetName'])" + $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" + + "$($contextObj['TargetType'])/$($contextObj['TargetName'])" } else { - $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/$($contextObj['TargetType'])/$($contextObj['TargetName'])" + $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" + + "$($contextObj['TargetType'])/$($contextObj['TargetName'])" } } 'App' { diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 996a2da15..ef99aa76a 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -55,7 +55,9 @@ Describe 'GitHub' { PrivateKey = $env:TEST_APP_PRIVATE_KEY } { Connect-GitHubAccount @params } | Should -Not -Throw - (Get-GitHubContext -ListAvailable).Count | Should -Be 3 + $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + Write-Verbose ($contexts | Out-String) -Verbose + ($contexts).Count | Should -Be 3 } It 'Can be called with a GitHub App and autoload installations' { $params = @{ @@ -63,14 +65,20 @@ Describe 'GitHub' { PrivateKey = $env:TEST_APP_PRIVATE_KEY } { Connect-GitHubAccount @params -AutoloadInstallations } | Should -Not -Throw - (Get-GitHubContext -ListAvailable).Count | Should -Be 7 + $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + Write-Verbose ($contexts | Out-String) -Verbose + ($contexts).Count | Should -Be 7 } It 'Can disconnect a specific context' { { Disconnect-GitHubAccount -Context 'github.com/github-actions/Organization/PSModule' -Silent } | Should -Not -Throw - (Get-GitHubContext -ListAvailable).Count | Should -Be 6 + $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + Write-Verbose ($contexts | Out-String) -Verbose + ($contexts).Count | Should -Be 6 Connect-GitHubAccount -ClientID $env:TEST_APP_CLIENT_ID -PrivateKey $env:TEST_APP_PRIVATE_KEY -AutoloadInstallations - (Get-GitHubContext -ListAvailable).Count | Should -Be 7 + $contexts = Get-GitHubContext -ListAvailable -Verbose:$false + Write-Verbose ($contexts | Out-String) -Verbose + ($contexts).Count | Should -Be 7 } It 'Can get the authenticated GitHubApp' { From d7ce1407d712572b0244455570fa14fb56f6b4d7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 21:13:33 +0100 Subject: [PATCH 28/30] test --- .../public/Auth/Context/Set-GitHubContext.ps1 | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index f70277d58..cb420df53 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -100,11 +100,21 @@ function Set-GitHubContext { Write-Verbose "Repository Owner: $owner" Write-Verbose "Repository Owner Type: $targetType" Write-Verbose "Sender: $gh_sender" - $contextObj['Enterprise'] = [string]$enterprise - $contextObj['TargetType'] = [string]$targetType - $contextObj['TargetName'] = [string]$targetName - $contextObj['Owner'] = [string]$owner - $contextObj['Repo'] = [string]$repo + if ([string]::IsNullOrEmpty($contextObj['Enterprise'])) { + $contextObj['Enterprise'] = [string]$enterprise + } + if ([string]::IsNullOrEmpty($contextObj['Owner'])) { + $contextObj['Owner'] = [string]$owner + } + if ([string]::IsNullOrEmpty($contextObj['Repo'])) { + $contextObj['Repo'] = [string]$repo + } + if ([string]::IsNullOrEmpty($contextObj['TargetType'])) { + $contextObj['TargetType'] = [string]$targetType + } + if ([string]::IsNullOrEmpty($contextObj['TargetName'])) { + $contextObj['TargetName'] = [string]$targetName + } $contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" + "$($contextObj['TargetType'])/$($contextObj['TargetName'])" } else { From 6be98737142b157249a8f627ab936077f8c1ac4b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 21:34:38 +0100 Subject: [PATCH 29/30] Fix tests --- tests/GitHub.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index ef99aa76a..7916d4b19 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -71,7 +71,7 @@ Describe 'GitHub' { } It 'Can disconnect a specific context' { - { Disconnect-GitHubAccount -Context 'github.com/github-actions/Organization/PSModule' -Silent } | Should -Not -Throw + { Disconnect-GitHubAccount -Context 'github.com/psmodule-test-app/Organization/PSModule' -Silent } | Should -Not -Throw $contexts = Get-GitHubContext -ListAvailable -Verbose:$false Write-Verbose ($contexts | Out-String) -Verbose ($contexts).Count | Should -Be 6 From 0e2067b947fac26a9b72bb13c4e3c02aa85f8373 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 12 Dec 2024 22:02:42 +0100 Subject: [PATCH 30/30] Re-intro GitHubContext --- .../formats/AppGitHubContext.Format.ps1xml | 113 ------------------ .../InstallationGitHubContext.Format.ps1xml | 113 ------------------ .../formats/UserGitHubContext.Format.ps1xml | 113 ------------------ .../formats/GitHubContext.Format.ps1xml | 0 4 files changed, 339 deletions(-) delete mode 100644 examples/formats/AppGitHubContext.Format.ps1xml delete mode 100644 examples/formats/InstallationGitHubContext.Format.ps1xml delete mode 100644 examples/formats/UserGitHubContext.Format.ps1xml rename {examples => src}/formats/GitHubContext.Format.ps1xml (100%) diff --git a/examples/formats/AppGitHubContext.Format.ps1xml b/examples/formats/AppGitHubContext.Format.ps1xml deleted file mode 100644 index 638e3d673..000000000 --- a/examples/formats/AppGitHubContext.Format.ps1xml +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - GitHubContextTableView - - GitHubContext - - - - - - - - - - - - - - - - - - - - - Name - - - AuthType - - - TokenType - - - TokenExpirationDate - - - - - - - - - - GitHubContextListView - - GitHubContext - - - - - - - HostName - - - UserName - - - AuthType - - - TokenType - - - TokenExpirationDate - - - Repo - - - Scope - - - ApiBaseUri - - - ApiVersion - - - AuthClientID - - - ClientID - - - DeviceFlowType - - - NodeID - - - DatabaseID - - - ID - - - Owner - - - RefreshTokenExpirationDate - - - - - - - - diff --git a/examples/formats/InstallationGitHubContext.Format.ps1xml b/examples/formats/InstallationGitHubContext.Format.ps1xml deleted file mode 100644 index 638e3d673..000000000 --- a/examples/formats/InstallationGitHubContext.Format.ps1xml +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - GitHubContextTableView - - GitHubContext - - - - - - - - - - - - - - - - - - - - - Name - - - AuthType - - - TokenType - - - TokenExpirationDate - - - - - - - - - - GitHubContextListView - - GitHubContext - - - - - - - HostName - - - UserName - - - AuthType - - - TokenType - - - TokenExpirationDate - - - Repo - - - Scope - - - ApiBaseUri - - - ApiVersion - - - AuthClientID - - - ClientID - - - DeviceFlowType - - - NodeID - - - DatabaseID - - - ID - - - Owner - - - RefreshTokenExpirationDate - - - - - - - - diff --git a/examples/formats/UserGitHubContext.Format.ps1xml b/examples/formats/UserGitHubContext.Format.ps1xml deleted file mode 100644 index 638e3d673..000000000 --- a/examples/formats/UserGitHubContext.Format.ps1xml +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - GitHubContextTableView - - GitHubContext - - - - - - - - - - - - - - - - - - - - - Name - - - AuthType - - - TokenType - - - TokenExpirationDate - - - - - - - - - - GitHubContextListView - - GitHubContext - - - - - - - HostName - - - UserName - - - AuthType - - - TokenType - - - TokenExpirationDate - - - Repo - - - Scope - - - ApiBaseUri - - - ApiVersion - - - AuthClientID - - - ClientID - - - DeviceFlowType - - - NodeID - - - DatabaseID - - - ID - - - Owner - - - RefreshTokenExpirationDate - - - - - - - - diff --git a/examples/formats/GitHubContext.Format.ps1xml b/src/formats/GitHubContext.Format.ps1xml similarity index 100% rename from examples/formats/GitHubContext.Format.ps1xml rename to src/formats/GitHubContext.Format.ps1xml