From b452f313ff724e1569ff7d0f927c873dae9fae9c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 01:00:21 +0100 Subject: [PATCH 01/18] examples --- examples/Get-AllTeams.ps1 | 12 ++++++++ examples/Get-WebhookDelivery.ps1 | 34 ++++++++++++++++++++++ examples/Install-App.ps1 | 48 ++++++++++++++++++++++++++++++++ examples/New-Teams.ps1 | 23 +++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 examples/Get-AllTeams.ps1 create mode 100644 examples/Get-WebhookDelivery.ps1 create mode 100644 examples/Install-App.ps1 create mode 100644 examples/New-Teams.ps1 diff --git a/examples/Get-AllTeams.ps1 b/examples/Get-AllTeams.ps1 new file mode 100644 index 00000000..7e4955b1 --- /dev/null +++ b/examples/Get-AllTeams.ps1 @@ -0,0 +1,12 @@ +$orgs = Get-GitHubOrganization +$teams = $orgs | ForEach-Object { + $org = $_.login + Get-GitHubTeamListByOrg -Organization $org | ForEach-Object { + [pscustomobject]@{ + Organization = $org + Team = $_.name + TeamId = $_.id + } + } +} +$teams diff --git a/examples/Get-WebhookDelivery.ps1 b/examples/Get-WebhookDelivery.ps1 new file mode 100644 index 00000000..43f563f7 --- /dev/null +++ b/examples/Get-WebhookDelivery.ps1 @@ -0,0 +1,34 @@ +Connect-GitHub -ClientID $ClientID -PrivateKey $PrivateKey -HostName 'msx.ghe.com' + +$Return = Invoke-GitHubAPI -ApiEndpoint '/app/hook/deliveries' +$Webhooks = $Return.Response | ForEach-Object { + [psCustomObject]@{ + Success = ($_.status_code -lt 300) -and ($_.status_code -ge 200) + StatusCode = $_.status_code + Status = $_.status + ID = $_.id + GUID = $_.guid + Date = $_.delivered_at + Duration = $_.duration + Redelivery = $_.redelivery + Event = $_.event + Action = $_.action + InstallationID = $_.installation.id + RepositoryID = $_.repository.id + URL = $_.url + ThrottledAt = $_.throttled_at + } +} + +$Webhooks | Where-Object { $_.Event -eq 'team' } | Format-Table -AutoSize + + + + +$Return.Response | Format-Table -AutoSize + +Set-GitHubDefaultContext -Context 'msx.ghe.com/Marius-Storhaug' + +1..10 | ForEach-Object { + New-GitHubTeam -Organization 'my-org' -Name "Test$_" +} diff --git a/examples/Install-App.ps1 b/examples/Install-App.ps1 new file mode 100644 index 00000000..c41e8b00 --- /dev/null +++ b/examples/Install-App.ps1 @@ -0,0 +1,48 @@ +$appIDs = @( + 'qweqweqwe', + 'qweqweqweqwe' +) + +$organization = '*' +filter Install-GithubApp { + param( + [Parameter()] + [string] $Enterprise = 'msx', + + [Parameter()] + [string] $Organization = '*', + + [Parameter( + Mandatory, + ValueFromPipeline + )] + [string] $AppID + ) + + begin { + + } + + process { + $installableOrgs = Get-GitHubEnterpriseInstallableOrganization -Enterprise $Enterprise -Debug -Verbose + $orgs = $installableOrgs | Where-Object { $_.login -like $organization } + foreach ($org in $orgs) { + foreach ($appIDitem in $AppID) { + Install-GitHubAppOnEnterpriseOrganization -Enterprise $Enterprise -Organization $org.login -ClientID $appIDitem -RepositorySelection all | ForEach-Object { + [PSCustomObject]@{ + Organization = $org.login + AppID = $appIDitem + } + } + } + } + } + + end { + + } +} + +$appIDs | Install-GithubApp -Organization $organization -Debug -Verbose + +$installation = Get-GitHubAppInstallation diff --git a/examples/New-Teams.ps1 b/examples/New-Teams.ps1 new file mode 100644 index 00000000..1d449c55 --- /dev/null +++ b/examples/New-Teams.ps1 @@ -0,0 +1,23 @@ +$filter = 'my-org' + +$allOrgs = Get-GitHubOrganization $filter -Verbose +$orgs = $allOrgs | Where-Object { $_.login -like $filter } + +$orgs | Select-Object login, id, disk_usage +foreach ($org in $orgs) { + 1..100 | ForEach-Object { + New-GitHubTeam -Organization $org.login -Name "Team$_" -Description "Team $_" -Verbose + } +} + +$Teams = Get-GitHubTeamListByOrg -Organization $filter | ForEach-Object { + [pscustomobject]@{ + Organization = $filter + Name = $_.name + Id = $_.id + } +} + +$Teams | Where-Object { $_.Name -like 'Team*' } | ForEach-Object { + Remove-GitHubTeam -Organization $_.Organization -Name $_.Name -Verbose +} From 885d09a5a71d33f086668805569007d141b41af7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:11:14 +0100 Subject: [PATCH 02/18] Move examples --- examples/{ => Actions}/GitHubOutput.ps1 | 0 examples/{ => Apps}/AppManagement.ps1 | 0 .../{Install-App.ps1 => Apps/EnterpriseApps.ps1} | 0 examples/{ => Apps}/Get-WebhookDelivery.ps1 | 7 +++---- examples/Get-AllTeams.ps1 | 12 ------------ examples/Teams/Get-AllTeams.ps1 | 1 + examples/{New-Teams.ps1 => Teams/Teams.ps1} | 16 ++++++++++++++++ 7 files changed, 20 insertions(+), 16 deletions(-) rename examples/{ => Actions}/GitHubOutput.ps1 (100%) rename examples/{ => Apps}/AppManagement.ps1 (100%) rename examples/{Install-App.ps1 => Apps/EnterpriseApps.ps1} (100%) rename examples/{ => Apps}/Get-WebhookDelivery.ps1 (80%) delete mode 100644 examples/Get-AllTeams.ps1 create mode 100644 examples/Teams/Get-AllTeams.ps1 rename examples/{New-Teams.ps1 => Teams/Teams.ps1} (68%) diff --git a/examples/GitHubOutput.ps1 b/examples/Actions/GitHubOutput.ps1 similarity index 100% rename from examples/GitHubOutput.ps1 rename to examples/Actions/GitHubOutput.ps1 diff --git a/examples/AppManagement.ps1 b/examples/Apps/AppManagement.ps1 similarity index 100% rename from examples/AppManagement.ps1 rename to examples/Apps/AppManagement.ps1 diff --git a/examples/Install-App.ps1 b/examples/Apps/EnterpriseApps.ps1 similarity index 100% rename from examples/Install-App.ps1 rename to examples/Apps/EnterpriseApps.ps1 diff --git a/examples/Get-WebhookDelivery.ps1 b/examples/Apps/Get-WebhookDelivery.ps1 similarity index 80% rename from examples/Get-WebhookDelivery.ps1 rename to examples/Apps/Get-WebhookDelivery.ps1 index 43f563f7..b1363790 100644 --- a/examples/Get-WebhookDelivery.ps1 +++ b/examples/Apps/Get-WebhookDelivery.ps1 @@ -1,8 +1,7 @@ Connect-GitHub -ClientID $ClientID -PrivateKey $PrivateKey -HostName 'msx.ghe.com' -$Return = Invoke-GitHubAPI -ApiEndpoint '/app/hook/deliveries' -$Webhooks = $Return.Response | ForEach-Object { - [psCustomObject]@{ +$deliveries = Get-GitHubAppWebhookDelivery | ForEach-Object { + [pscustomobject]@{ Success = ($_.status_code -lt 300) -and ($_.status_code -ge 200) StatusCode = $_.status_code Status = $_.status @@ -20,7 +19,7 @@ $Webhooks = $Return.Response | ForEach-Object { } } -$Webhooks | Where-Object { $_.Event -eq 'team' } | Format-Table -AutoSize +$deliveries | Where-Object { $_.Event -eq 'team' } | Format-Table -AutoSize diff --git a/examples/Get-AllTeams.ps1 b/examples/Get-AllTeams.ps1 deleted file mode 100644 index 7e4955b1..00000000 --- a/examples/Get-AllTeams.ps1 +++ /dev/null @@ -1,12 +0,0 @@ -$orgs = Get-GitHubOrganization -$teams = $orgs | ForEach-Object { - $org = $_.login - Get-GitHubTeamListByOrg -Organization $org | ForEach-Object { - [pscustomobject]@{ - Organization = $org - Team = $_.name - TeamId = $_.id - } - } -} -$teams diff --git a/examples/Teams/Get-AllTeams.ps1 b/examples/Teams/Get-AllTeams.ps1 new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/examples/Teams/Get-AllTeams.ps1 @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/New-Teams.ps1 b/examples/Teams/Teams.ps1 similarity index 68% rename from examples/New-Teams.ps1 rename to examples/Teams/Teams.ps1 index 1d449c55..a5f3f7e1 100644 --- a/examples/New-Teams.ps1 +++ b/examples/Teams/Teams.ps1 @@ -10,6 +10,7 @@ foreach ($org in $orgs) { } } + $Teams = Get-GitHubTeamListByOrg -Organization $filter | ForEach-Object { [pscustomobject]@{ Organization = $filter @@ -18,6 +19,21 @@ $Teams = Get-GitHubTeamListByOrg -Organization $filter | ForEach-Object { } } + +$orgs = Get-GitHubOrganization +$teams = $orgs | ForEach-Object { + $org = $_.login + Get-GitHubTeamListByOrg -Organization $org | ForEach-Object { + [pscustomobject]@{ + Organization = $org + Team = $_.name + TeamId = $_.id + } + } +} +$teams + + $Teams | Where-Object { $_.Name -like 'Team*' } | ForEach-Object { Remove-GitHubTeam -Organization $_.Organization -Name $_.Name -Verbose } From 531bca2cd283b655a545ec0c95a5ede1f657dd1e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:11:33 +0100 Subject: [PATCH 03/18] Add a GitHub AuthType Enum --- src/classes/public/Context/GitHubAuthType.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/classes/public/Context/GitHubAuthType.ps1 diff --git a/src/classes/public/Context/GitHubAuthType.ps1 b/src/classes/public/Context/GitHubAuthType.ps1 new file mode 100644 index 00000000..d9c41ed4 --- /dev/null +++ b/src/classes/public/Context/GitHubAuthType.ps1 @@ -0,0 +1,6 @@ +enum GitHubAuthType { + PAT + UAT + APP + IAT +} From 16835a7ad09e79bebdcbc94cb9ccb0ba37da5a22 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:12:16 +0100 Subject: [PATCH 04/18] Add GitHubAuthType data type on GitHubContext.AuthType --- src/classes/public/Context/GitHubContext.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index e4117572..2cb777a8 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -26,7 +26,7 @@ # The authentication type. # UAT / PAT / App / IAT - [string] $AuthType + [GitHubAuthType] $AuthType # User ID / App ID as GraphQL Node ID [string] $NodeID From ba990449bc3c11bb888ba211e8f53cb93337f878 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:12:42 +0100 Subject: [PATCH 05/18] Use GitHubAuthType on Assert-GitHubContext --- src/functions/public/Auth/Assert-GitHubContext.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index e53b67ae..76776d86 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -22,7 +22,7 @@ # The required authtypes for the command. [Parameter(Mandatory)] - [string[]] $AuthType + [GitHubAuthType[]] $AuthType ) $command = (Get-PSCallStack)[1].Command From d534a614be14d9d8152bcdf3f575cd022d9db849 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:13:14 +0100 Subject: [PATCH 06/18] Add Assert-GitHubContext on Get-GitHubAppWebhookConfiguration --- .../public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 64b8dbdf..67e7913f 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -27,6 +27,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + $Context | Assert-GitHubContext -AuthType 'App' $inputObject = @{ Context = $Context From 7d93e877bcbdded1d5258c63de632e68ee813a88 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:47:27 +0100 Subject: [PATCH 07/18] Rename AuthType --- .../Context/{GitHubAuthType.ps1 => GitHubContextAuthType.ps1} | 2 +- src/functions/public/Auth/Assert-GitHubContext.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/classes/public/Context/{GitHubAuthType.ps1 => GitHubContextAuthType.ps1} (51%) diff --git a/src/classes/public/Context/GitHubAuthType.ps1 b/src/classes/public/Context/GitHubContextAuthType.ps1 similarity index 51% rename from src/classes/public/Context/GitHubAuthType.ps1 rename to src/classes/public/Context/GitHubContextAuthType.ps1 index d9c41ed4..72efed45 100644 --- a/src/classes/public/Context/GitHubAuthType.ps1 +++ b/src/classes/public/Context/GitHubContextAuthType.ps1 @@ -1,4 +1,4 @@ -enum GitHubAuthType { +enum GitHubContextAuthType { PAT UAT APP diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index 76776d86..fcd6ea24 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -22,7 +22,7 @@ # The required authtypes for the command. [Parameter(Mandatory)] - [GitHubAuthType[]] $AuthType + [GitHubContextAuthType[]] $AuthType ) $command = (Get-PSCallStack)[1].Command From a72d31c816bed8649e5bcc13c623be1187deabed Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:54:07 +0100 Subject: [PATCH 08/18] Introduce UserGitHubContextAuthAppType enum and update DeviceFlowType to use it --- .../public/Context/GitHubContext/UserGitHubContext.ps1 | 2 +- .../Context/GitHubContext/UserGitHubContextAuthAppType.ps1 | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 diff --git a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 index 345aaf86..0f98d5e9 100644 --- a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 @@ -5,7 +5,7 @@ # The device flow type. # GitHubApp / OAuthApp - [string] $DeviceFlowType + [UserGitHubContextAuthAppType] $DeviceFlowType # The scope when authenticating with OAuth. # 'gist read:org repo workflow' diff --git a/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 b/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 new file mode 100644 index 00000000..456fe841 --- /dev/null +++ b/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 @@ -0,0 +1,4 @@ +enum UserGitHubContextAuthAppType { + GitHubApp + OAuthApp +} From 00c9b1066a16e357e8ed8f00a19ce35bc313cecb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:54:43 +0100 Subject: [PATCH 09/18] Change ApiBaseUri type from string to uri in GitHubContext class --- src/classes/public/Context/GitHubContext.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index 2cb777a8..b1b4d9aa 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -1,4 +1,4 @@ -class GitHubContext : Context { +class GitHubContext : Context { # The GitHub Context Name. # HostName/Username or HostName/AppSlug # github.com/Octocat @@ -18,7 +18,7 @@ # The API base URI. # https://api.github.com - [string] $ApiBaseUri + [uri] $ApiBaseUri # The GitHub API version. # 2022-11-28 From 4728a22cd8c917a7d57d3b8ee574a47dc1f35e4d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 10:54:49 +0100 Subject: [PATCH 10/18] Refactor GitHubContext to use enums for Type, AuthType, and TokenType; update related functions for consistency --- src/classes/public/Context/GitHubContext.ps1 | 8 ++++---- .../public/Context/GitHubContextTokenType.ps1 | 8 ++++++++ src/classes/public/Context/GitHubContextType.ps1 | 5 +++++ .../Webhooks/Get-GitHubAppWebhookConfiguration.ps1 | 2 +- .../Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 1 + src/functions/public/Auth/Connect-GitHubApp.ps1 | 12 ++++++------ .../public/Auth/Context/Set-GitHubContext.ps1 | 4 ++-- 7 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/classes/public/Context/GitHubContextTokenType.ps1 create mode 100644 src/classes/public/Context/GitHubContextType.ps1 diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index b1b4d9aa..301ed183 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -1,4 +1,4 @@ -class GitHubContext : Context { +class GitHubContext : Context { # The GitHub Context Name. # HostName/Username or HostName/AppSlug # github.com/Octocat @@ -10,7 +10,7 @@ class GitHubContext : Context { # The context type # User / App / Installation - [string] $Type + [GitHubContextType] $Type # The API hostname. # github.com / msx.ghe.com / github.local @@ -26,7 +26,7 @@ class GitHubContext : Context { # The authentication type. # UAT / PAT / App / IAT - [GitHubAuthType] $AuthType + [GitHubContextAuthType] $AuthType # User ID / App ID as GraphQL Node ID [string] $NodeID @@ -42,7 +42,7 @@ class GitHubContext : Context { # The token type. # ghu / gho / ghp / github_pat / PEM / ghs / - [string] $TokenType + [GitHubContextTokenType] $TokenType # The default value for the Enterprise parameter. [string] $Enterprise diff --git a/src/classes/public/Context/GitHubContextTokenType.ps1 b/src/classes/public/Context/GitHubContextTokenType.ps1 new file mode 100644 index 00000000..accc7a60 --- /dev/null +++ b/src/classes/public/Context/GitHubContextTokenType.ps1 @@ -0,0 +1,8 @@ +enum GitHubContextTokenType { + ghu + gho + ghp + github_pat + PEM + ghs +} diff --git a/src/classes/public/Context/GitHubContextType.ps1 b/src/classes/public/Context/GitHubContextType.ps1 new file mode 100644 index 00000000..ff7fec82 --- /dev/null +++ b/src/classes/public/Context/GitHubContextType.ps1 @@ -0,0 +1,5 @@ +enum GitHubContextType { + Application + Installation + User +} diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 67e7913f..8e917b45 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -27,7 +27,7 @@ ) $Context = Resolve-GitHubContext -Context $Context - $Context | Assert-GitHubContext -AuthType 'App' + $Context | Assert-GitHubContext -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index b2ad5b3a..4ee7ed70 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -26,6 +26,7 @@ ) $Context = Resolve-GitHubContext -Context $Context + $Context | Assert-GitHubContext -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 498ab089..72a7097b 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -101,10 +101,10 @@ $token = New-GitHubAppInstallationAccessToken -Context $Context -InstallationID $installation.id $contextParams = @{ - AuthType = [string]'IAT' + AuthType = [GitHubContextAuthType]'IAT' TokenType = [string]'ghs' DisplayName = [string]$Context.DisplayName - ApiBaseUri = [string]$Context.ApiBaseUri + ApiBaseUri = [uri]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName ClientID = [string]$Context.ClientID @@ -113,18 +113,18 @@ Events = [string[]]$installation.events TargetType = [string]$installation.target_type Token = [securestring]$token.Token - TokenExpirationDate = [string]$token.ExpiresAt + TokenExpirationDate = [datetime]$token.ExpiresAt } switch ($installation.target_type) { 'User' { - $contextParams['TargetName'] = $installation.account.login + $contextParams['TargetName'] = [string]$installation.account.login } 'Organization' { - $contextParams['TargetName'] = $installation.account.login + $contextParams['TargetName'] = [string]$installation.account.login } 'Enterprise' { - $contextParams['TargetName'] = $installation.account.slug + $contextParams['TargetName'] = [string]$installation.account.slug } } Write-Verbose 'Logging in using a managed installation access token...' diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index cb420df5..b7971063 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -73,7 +73,7 @@ function Set-GitHubContext { 'PAT|UAT' { $contextName = "$($contextObj['HostName'])/$login" $contextObj['Name'] = $contextName - $contextObj['Type'] = 'User' + $contextObj['Type'] = [GitHubContextType]'User' } 'IAT' { $contextObj['Type'] = 'Installation' @@ -133,7 +133,7 @@ function Set-GitHubContext { $contextObj['Events'] = [string[]]$app.events $contextObj['OwnerName'] = [string]$app.owner.login $contextObj['OwnerType'] = [string]$app.owner.type - $contextObj['Type'] = 'App' + $contextObj['Type'] = [GitHubContextType]'App' } default { throw 'Failed to get info on the context. Unknown logon type.' From c45a1c8d76da22b261dd46029074d063fceca7dd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 11:49:53 +0100 Subject: [PATCH 11/18] Add .ToString() for enum values --- src/functions/public/API/Invoke-GitHubAPI.ps1 | 8 ++++---- src/functions/public/Auth/Context/Get-GitHubContext.ps1 | 6 +++--- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index 3ec9dcff..9270d6d9 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -98,14 +98,14 @@ Write-Debug "Token : [$Token]" if ([string]::IsNullOrEmpty($TokenType)) { - $TokenType = $Context.TokenType + $TokenType = $Context.TokenType.ToString() } - Write-Debug "TokenType : [$($Context.TokenType)]" + Write-Debug "TokenType : [$($Context.TokenType.ToString())]" if ([string]::IsNullOrEmpty($ApiBaseUri)) { - $ApiBaseUri = $Context.ApiBaseUri + $ApiBaseUri = $Context.ApiBaseUri.ToString() } - Write-Debug "ApiBaseUri : [$($Context.ApiBaseUri)]" + Write-Debug "ApiBaseUri : [$($Context.ApiBaseUri.ToString())]" if ([string]::IsNullOrEmpty($ApiVersion)) { $ApiVersion = $Context.ApiVersion diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index ef228537..7817b6fa 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -70,8 +70,8 @@ function Get-GitHubContext { Write-Verbose 'Context:' $contextObj | Select-Object * | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - Write-Verbose "Converting to: [$($contextObj.Type)GitHubContext]" - switch ($contextObj.Type) { + Write-Verbose "Converting to: [$($contextObj.Type.ToString())GitHubContext]" + switch ($contextObj.Type.ToString()) { 'User' { [UserGitHubContext]::new($contextObj) } @@ -82,7 +82,7 @@ function Get-GitHubContext { [InstallationGitHubContext]::new($contextObj) } default { - throw "Unknown context type: [$($contextObj.Type)]" + throw "Unknown context type: [$($contextObj.Type.ToString())]" } } } diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index b7971063..6525c8a4 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -51,8 +51,8 @@ function Set-GitHubContext { # Run functions to get info on the temporary context. try { - Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]." - switch -Regex ($($contextObj['AuthType'])) { + Write-Verbose "Getting info on the context [$($contextObj['AuthType'].ToString())]." + switch -Regex ($($contextObj['AuthType'].ToString())) { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $contextObj $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } From 18f050b34dd9d90cea8951bc3380c6256323c658 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 12:11:23 +0100 Subject: [PATCH 12/18] Fix --- src/functions/public/Auth/Context/Get-GitHubContext.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index 7817b6fa..f570db11 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -70,8 +70,8 @@ function Get-GitHubContext { Write-Verbose 'Context:' $contextObj | Select-Object * | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - Write-Verbose "Converting to: [$($contextObj.Type.ToString())GitHubContext]" - switch ($contextObj.Type.ToString()) { + Write-Verbose "Converting to: [$([GitHubContextType].GetEnumName($contextObj.Type))GitHubContext]" + switch ([GitHubContextType].GetEnumName($contextObj.Type)) { 'User' { [UserGitHubContext]::new($contextObj) } @@ -82,7 +82,7 @@ function Get-GitHubContext { [InstallationGitHubContext]::new($contextObj) } default { - throw "Unknown context type: [$($contextObj.Type.ToString())]" + throw "Unknown context type: [$($contextObj.Type)]" } } } From 2027edb3785f54d9f65b9b7c6734ce249ec5bc63 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 12:50:38 +0100 Subject: [PATCH 13/18] Refactor GitHub App scripts to use Assert-GitHubContext for App authentication --- src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 | 3 ++- .../public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 | 3 ++- .../Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 | 3 ++- .../public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 | 2 +- .../public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 index 9055ec5e..ff81067b 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 @@ -39,7 +39,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context + $Context = $Context | Resolve-GitHubContext + $Context | Assert-GitHubContext -AuthType App switch ($PSCmdlet.ParameterSetName) { 'BySlug' { diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 index 79f05113..b452593a 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 @@ -25,7 +25,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context + $Context = $Context | Resolve-GitHubContext + $Context | Assert-GitHubContext -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 index d4d3667e..c802e9e9 100644 --- a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 +++ b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 @@ -65,7 +65,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context + $Context = $Context | Resolve-GitHubContext + $Context | Assert-GitHubContext -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 8e917b45..53f2ae8d 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -26,7 +26,7 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context + $Context = $Context | Resolve-GitHubContext $Context | Assert-GitHubContext -AuthType App $inputObject = @{ diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index 4ee7ed70..36537fa2 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -25,7 +25,7 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context + $Context = $Context | Resolve-GitHubContext $Context | Assert-GitHubContext -AuthType App $inputObject = @{ From b24bfd62d83bf7109e58b0cac7c87e355c26e146 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 12:56:13 +0100 Subject: [PATCH 14/18] revert --- src/classes/public/Context/GitHubContext.ps1 | 8 ++++---- .../public/Context/GitHubContext/UserGitHubContext.ps1 | 2 +- .../GitHubContext/UserGitHubContextAuthAppType.ps1 | 4 ---- src/classes/public/Context/GitHubContextAuthType.ps1 | 6 ------ src/classes/public/Context/GitHubContextTokenType.ps1 | 8 -------- src/classes/public/Context/GitHubContextType.ps1 | 5 ----- src/functions/public/API/Invoke-GitHubAPI.ps1 | 8 ++++---- src/functions/public/Auth/Assert-GitHubContext.ps1 | 2 +- src/functions/public/Auth/Connect-GitHubApp.ps1 | 4 ++-- 9 files changed, 12 insertions(+), 35 deletions(-) delete mode 100644 src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 delete mode 100644 src/classes/public/Context/GitHubContextAuthType.ps1 delete mode 100644 src/classes/public/Context/GitHubContextTokenType.ps1 delete mode 100644 src/classes/public/Context/GitHubContextType.ps1 diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index 301ed183..736d8fdb 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -1,4 +1,4 @@ -class GitHubContext : Context { +class GitHubContext : Context { # The GitHub Context Name. # HostName/Username or HostName/AppSlug # github.com/Octocat @@ -10,7 +10,7 @@ # The context type # User / App / Installation - [GitHubContextType] $Type + [string] $Type # The API hostname. # github.com / msx.ghe.com / github.local @@ -26,7 +26,7 @@ # The authentication type. # UAT / PAT / App / IAT - [GitHubContextAuthType] $AuthType + [string] $AuthType # User ID / App ID as GraphQL Node ID [string] $NodeID @@ -42,7 +42,7 @@ # The token type. # ghu / gho / ghp / github_pat / PEM / ghs / - [GitHubContextTokenType] $TokenType + [string] $TokenType # The default value for the Enterprise parameter. [string] $Enterprise diff --git a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 index 0f98d5e9..345aaf86 100644 --- a/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext/UserGitHubContext.ps1 @@ -5,7 +5,7 @@ # The device flow type. # GitHubApp / OAuthApp - [UserGitHubContextAuthAppType] $DeviceFlowType + [string] $DeviceFlowType # The scope when authenticating with OAuth. # 'gist read:org repo workflow' diff --git a/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 b/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 deleted file mode 100644 index 456fe841..00000000 --- a/src/classes/public/Context/GitHubContext/UserGitHubContextAuthAppType.ps1 +++ /dev/null @@ -1,4 +0,0 @@ -enum UserGitHubContextAuthAppType { - GitHubApp - OAuthApp -} diff --git a/src/classes/public/Context/GitHubContextAuthType.ps1 b/src/classes/public/Context/GitHubContextAuthType.ps1 deleted file mode 100644 index 72efed45..00000000 --- a/src/classes/public/Context/GitHubContextAuthType.ps1 +++ /dev/null @@ -1,6 +0,0 @@ -enum GitHubContextAuthType { - PAT - UAT - APP - IAT -} diff --git a/src/classes/public/Context/GitHubContextTokenType.ps1 b/src/classes/public/Context/GitHubContextTokenType.ps1 deleted file mode 100644 index accc7a60..00000000 --- a/src/classes/public/Context/GitHubContextTokenType.ps1 +++ /dev/null @@ -1,8 +0,0 @@ -enum GitHubContextTokenType { - ghu - gho - ghp - github_pat - PEM - ghs -} diff --git a/src/classes/public/Context/GitHubContextType.ps1 b/src/classes/public/Context/GitHubContextType.ps1 deleted file mode 100644 index ff7fec82..00000000 --- a/src/classes/public/Context/GitHubContextType.ps1 +++ /dev/null @@ -1,5 +0,0 @@ -enum GitHubContextType { - Application - Installation - User -} diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index 9270d6d9..3ec9dcff 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -98,14 +98,14 @@ Write-Debug "Token : [$Token]" if ([string]::IsNullOrEmpty($TokenType)) { - $TokenType = $Context.TokenType.ToString() + $TokenType = $Context.TokenType } - Write-Debug "TokenType : [$($Context.TokenType.ToString())]" + Write-Debug "TokenType : [$($Context.TokenType)]" if ([string]::IsNullOrEmpty($ApiBaseUri)) { - $ApiBaseUri = $Context.ApiBaseUri.ToString() + $ApiBaseUri = $Context.ApiBaseUri } - Write-Debug "ApiBaseUri : [$($Context.ApiBaseUri.ToString())]" + Write-Debug "ApiBaseUri : [$($Context.ApiBaseUri)]" if ([string]::IsNullOrEmpty($ApiVersion)) { $ApiVersion = $Context.ApiVersion diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index fcd6ea24..e53b67ae 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -22,7 +22,7 @@ # The required authtypes for the command. [Parameter(Mandatory)] - [GitHubContextAuthType[]] $AuthType + [string[]] $AuthType ) $command = (Get-PSCallStack)[1].Command diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 72a7097b..0fa7235f 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -1,4 +1,4 @@ -function Connect-GitHubApp { +function Connect-GitHubApp { <# .SYNOPSIS Connects to GitHub as a installation using a GitHub App. @@ -101,7 +101,7 @@ $token = New-GitHubAppInstallationAccessToken -Context $Context -InstallationID $installation.id $contextParams = @{ - AuthType = [GitHubContextAuthType]'IAT' + AuthType = [string]'IAT' TokenType = [string]'ghs' DisplayName = [string]$Context.DisplayName ApiBaseUri = [uri]$Context.ApiBaseUri From f1b4506852a161243f7fc56357757b9d24335e7b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 13:33:52 +0100 Subject: [PATCH 15/18] Refactor Get-GitHubContext and Set-GitHubContext to use string literals for context types --- src/functions/public/Auth/Context/Get-GitHubContext.ps1 | 4 ++-- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index f570db11..ef228537 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -70,8 +70,8 @@ function Get-GitHubContext { Write-Verbose 'Context:' $contextObj | Select-Object * | Out-String -Stream | ForEach-Object { Write-Verbose $_ } - Write-Verbose "Converting to: [$([GitHubContextType].GetEnumName($contextObj.Type))GitHubContext]" - switch ([GitHubContextType].GetEnumName($contextObj.Type)) { + Write-Verbose "Converting to: [$($contextObj.Type)GitHubContext]" + switch ($contextObj.Type) { 'User' { [UserGitHubContext]::new($contextObj) } diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 6525c8a4..922df15d 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -73,7 +73,7 @@ function Set-GitHubContext { 'PAT|UAT' { $contextName = "$($contextObj['HostName'])/$login" $contextObj['Name'] = $contextName - $contextObj['Type'] = [GitHubContextType]'User' + $contextObj['Type'] = 'User' } 'IAT' { $contextObj['Type'] = 'Installation' @@ -133,7 +133,7 @@ function Set-GitHubContext { $contextObj['Events'] = [string[]]$app.events $contextObj['OwnerName'] = [string]$app.owner.login $contextObj['OwnerType'] = [string]$app.owner.type - $contextObj['Type'] = [GitHubContextType]'App' + $contextObj['Type'] = 'App' } default { throw 'Failed to get info on the context. Unknown logon type.' From e2872c7215a5883f30c6d98809cda992e6f7b35e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 14:37:11 +0100 Subject: [PATCH 16/18] Refactor GitHub App scripts to streamline context resolution and authentication --- src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 | 4 ++-- .../public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 | 4 ++-- .../Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 | 4 ++-- .../Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 | 4 ++-- .../public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 index ff81067b..bbcd89b3 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 @@ -39,8 +39,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = $Context | Resolve-GitHubContext - $Context | Assert-GitHubContext -AuthType App + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App switch ($PSCmdlet.ParameterSetName) { 'BySlug' { diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 index b452593a..fb8c1aef 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 @@ -25,8 +25,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = $Context | Resolve-GitHubContext - $Context | Assert-GitHubContext -AuthType App + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 index c802e9e9..34bf8658 100644 --- a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 +++ b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 @@ -65,8 +65,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = $Context | Resolve-GitHubContext - $Context | Assert-GitHubContext -AuthType App + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 53f2ae8d..163b65fd 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -26,8 +26,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = $Context | Resolve-GitHubContext - $Context | Assert-GitHubContext -AuthType App + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index 36537fa2..e5a25609 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -25,8 +25,8 @@ [object] $Context = (Get-GitHubContext) ) - $Context = $Context | Resolve-GitHubContext - $Context | Assert-GitHubContext -AuthType App + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context From 929261bbe3d173fa2369a0abb4b19a7704f333a7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 15:10:59 +0100 Subject: [PATCH 17/18] Refactor Set-GitHubContext to improve context type handling and verbosity logging --- src/functions/public/Auth/Context/Set-GitHubContext.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 922df15d..01e0217c 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -51,8 +51,8 @@ function Set-GitHubContext { # Run functions to get info on the temporary context. try { - Write-Verbose "Getting info on the context [$($contextObj['AuthType'].ToString())]." - switch -Regex ($($contextObj['AuthType'].ToString())) { + Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]." + switch -Regex ($contextObj['AuthType']) { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $contextObj $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ } From 197450a04a28df91c324ec8d4ba5268ee12bf7af Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 22:44:40 +0100 Subject: [PATCH 18/18] Refactor GitHub context handling to improve type consistency and logging --- src/classes/public/Context/GitHubContext.ps1 | 4 +-- .../private/Apps/Get-GitHubAppByName.ps1 | 26 +++++++++++++------ .../public/Apps/GitHub Apps/Get-GitHubApp.ps1 | 1 - .../GitHub Apps/Get-GitHubAppInstallation.ps1 | 1 - .../New-GitHubAppInstallationAccessToken.ps1 | 1 - .../Get-GitHubAppWebhookConfiguration.ps1 | 1 - .../Webhooks/Get-GitHubAppWebhookDelivery.ps1 | 1 - .../public/Auth/Connect-GitHubApp.ps1 | 12 ++++----- .../public/Auth/Context/Set-GitHubContext.ps1 | 2 +- 9 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/classes/public/Context/GitHubContext.ps1 b/src/classes/public/Context/GitHubContext.ps1 index 736d8fdb..e4117572 100644 --- a/src/classes/public/Context/GitHubContext.ps1 +++ b/src/classes/public/Context/GitHubContext.ps1 @@ -1,4 +1,4 @@ -class GitHubContext : Context { +class GitHubContext : Context { # The GitHub Context Name. # HostName/Username or HostName/AppSlug # github.com/Octocat @@ -18,7 +18,7 @@ class GitHubContext : Context { # The API base URI. # https://api.github.com - [uri] $ApiBaseUri + [string] $ApiBaseUri # The GitHub API version. # 2022-11-28 diff --git a/src/functions/private/Apps/Get-GitHubAppByName.ps1 b/src/functions/private/Apps/Get-GitHubAppByName.ps1 index 83ce4356..cf9ac29d 100644 --- a/src/functions/private/Apps/Get-GitHubAppByName.ps1 +++ b/src/functions/private/Apps/Get-GitHubAppByName.ps1 @@ -30,15 +30,25 @@ [object] $Context = (Get-GitHubContext) ) - $Context = Resolve-GitHubContext -Context $Context - - $inputObject = @{ - Context = $Context - APIEndpoint = "/apps/$AppSlug" - Method = 'GET' + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT } - Invoke-GitHubAPI @inputObject | ForEach-Object { - Write-Output $_.Response + process { + $inputObject = @{ + Context = $Context + APIEndpoint = "/apps/$AppSlug" + Method = 'GET' + } + + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } + end { + Write-Verbose "[$commandName] - End" } } diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 index bbcd89b3..9055ec5e 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1 @@ -40,7 +40,6 @@ ) $Context = Resolve-GitHubContext -Context $Context - Assert-GitHubContext -Context $Context -AuthType App switch ($PSCmdlet.ParameterSetName) { 'BySlug' { diff --git a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 index fb8c1aef..79f05113 100644 --- a/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 +++ b/src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1 @@ -26,7 +26,6 @@ ) $Context = Resolve-GitHubContext -Context $Context - Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 index 34bf8658..d4d3667e 100644 --- a/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 +++ b/src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1 @@ -66,7 +66,6 @@ ) $Context = Resolve-GitHubContext -Context $Context - Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 index 163b65fd..64b8dbdf 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1 @@ -27,7 +27,6 @@ ) $Context = Resolve-GitHubContext -Context $Context - Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 index e5a25609..b2ad5b3a 100644 --- a/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 +++ b/src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1 @@ -26,7 +26,6 @@ ) $Context = Resolve-GitHubContext -Context $Context - Assert-GitHubContext -Context $Context -AuthType App $inputObject = @{ Context = $Context diff --git a/src/functions/public/Auth/Connect-GitHubApp.ps1 b/src/functions/public/Auth/Connect-GitHubApp.ps1 index 0fa7235f..498ab089 100644 --- a/src/functions/public/Auth/Connect-GitHubApp.ps1 +++ b/src/functions/public/Auth/Connect-GitHubApp.ps1 @@ -1,4 +1,4 @@ -function Connect-GitHubApp { +function Connect-GitHubApp { <# .SYNOPSIS Connects to GitHub as a installation using a GitHub App. @@ -104,7 +104,7 @@ function Connect-GitHubApp { AuthType = [string]'IAT' TokenType = [string]'ghs' DisplayName = [string]$Context.DisplayName - ApiBaseUri = [uri]$Context.ApiBaseUri + ApiBaseUri = [string]$Context.ApiBaseUri ApiVersion = [string]$Context.ApiVersion HostName = [string]$Context.HostName ClientID = [string]$Context.ClientID @@ -113,18 +113,18 @@ function Connect-GitHubApp { Events = [string[]]$installation.events TargetType = [string]$installation.target_type Token = [securestring]$token.Token - TokenExpirationDate = [datetime]$token.ExpiresAt + TokenExpirationDate = [string]$token.ExpiresAt } switch ($installation.target_type) { 'User' { - $contextParams['TargetName'] = [string]$installation.account.login + $contextParams['TargetName'] = $installation.account.login } 'Organization' { - $contextParams['TargetName'] = [string]$installation.account.login + $contextParams['TargetName'] = $installation.account.login } 'Enterprise' { - $contextParams['TargetName'] = [string]$installation.account.slug + $contextParams['TargetName'] = $installation.account.slug } } Write-Verbose 'Logging in using a managed installation access token...' diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 01e0217c..cb420df5 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -52,7 +52,7 @@ function Set-GitHubContext { # Run functions to get info on the temporary context. try { Write-Verbose "Getting info on the context [$($contextObj['AuthType'])]." - switch -Regex ($contextObj['AuthType']) { + switch -Regex ($($contextObj['AuthType'])) { 'PAT|UAT|IAT' { $viewer = Get-GitHubViewer -Context $contextObj $viewer | Out-String -Stream | ForEach-Object { Write-Verbose $_ }