From b452f313ff724e1569ff7d0f927c873dae9fae9c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 01:00:21 +0100 Subject: [PATCH 01/22] 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 000000000..7e4955b1b --- /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 000000000..43f563f73 --- /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 000000000..c41e8b007 --- /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 000000000..1d449c55b --- /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/22] 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 43f563f73..b1363790d 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 7e4955b1b..000000000 --- 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 000000000..5f282702b --- /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 1d449c55b..a5f3f7e1d 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/22] 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 000000000..d9c41ed45 --- /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/22] 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 e41175727..2cb777a89 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/22] 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 e53b67ae0..76776d862 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/22] 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 64b8dbdf0..67e7913fa 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/22] 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 d9c41ed45..72efed459 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 76776d862..fcd6ea24a 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/22] 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 345aaf867..0f98d5e98 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 000000000..456fe841a --- /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/22] 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 2cb777a89..b1b4d9aae 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/22] 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 b1b4d9aae..301ed183e 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 000000000..accc7a605 --- /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 000000000..ff7fec82a --- /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 67e7913fa..8e917b454 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 b2ad5b3ae..4ee7ed70d 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 498ab089b..72a7097b9 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 cb420df53..b79710637 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/22] 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 3ec9dcff6..9270d6d97 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 ef2285379..7817b6fad 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 b79710637..6525c8a43 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/22] 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 7817b6fad..f570db117 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/22] 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 9055ec5ee..ff81067b8 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 79f051134..b452593a2 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 d4d3667e3..c802e9e91 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 8e917b454..53f2ae8da 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 4ee7ed70d..36537fa25 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/22] 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 301ed183e..736d8fdb9 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 0f98d5e98..345aaf867 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 456fe841a..000000000 --- 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 72efed459..000000000 --- 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 accc7a605..000000000 --- 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 ff7fec82a..000000000 --- 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 9270d6d97..3ec9dcff6 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 fcd6ea24a..e53b67ae0 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 72a7097b9..0fa7235fb 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/22] 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 f570db117..ef2285379 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 6525c8a43..922df15d2 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/22] 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 ff81067b8..bbcd89b33 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 b452593a2..fb8c1aefd 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 c802e9e91..34bf8658d 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 53f2ae8da..163b65fdd 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 36537fa25..e5a256094 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/22] 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 922df15d2..01e0217cd 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 1f314638e215ad2466d252c6c034dac23fd32a78 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 13 Dec 2024 22:44:22 +0100 Subject: [PATCH 18/22] Refactor context type annotations to use 'object' for improved flexibility --- src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 | 2 +- src/functions/public/Auth/Assert-GitHubContext.ps1 | 2 +- src/functions/public/Auth/Context/Get-GitHubContext.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 index 1302d8068..8c3a8963d 100644 --- a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 @@ -20,7 +20,7 @@ This will return the GitHubContext object. #> - [OutputType([GitHubContext])] + [OutputType([object])] [CmdletBinding()] param( # The context to resolve into an object. Used to get the details for the API call. diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index e53b67ae0..a230cc049 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -18,7 +18,7 @@ Mandatory, ValueFromPipeline )] - [GitHubContext] $Context, + [object] $Context, # The required authtypes for the command. [Parameter(Mandatory)] diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index ef2285379..1ce2ec842 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -17,7 +17,7 @@ function Get-GitHubContext { 'PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Encapsulated in a function. Never leaves as a plain text.' )] - [OutputType([GitHubContext])] + [OutputType([object])] [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] param( # The name of the context. From e780683ea6f0853e19b1f82b353b1adf9dc522d2 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 14 Dec 2024 00:57:58 +0100 Subject: [PATCH 19/22] Update output type annotations to use 'GitHubContext' for improved type safety --- src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 | 2 +- src/functions/public/Auth/Assert-GitHubContext.ps1 | 2 +- src/functions/public/Auth/Context/Get-GitHubContext.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 index 8c3a8963d..1302d8068 100644 --- a/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 +++ b/src/functions/private/Auth/Context/Resolve-GitHubContext.ps1 @@ -20,7 +20,7 @@ This will return the GitHubContext object. #> - [OutputType([object])] + [OutputType([GitHubContext])] [CmdletBinding()] param( # The context to resolve into an object. Used to get the details for the API call. diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index a230cc049..e53b67ae0 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -18,7 +18,7 @@ Mandatory, ValueFromPipeline )] - [object] $Context, + [GitHubContext] $Context, # The required authtypes for the command. [Parameter(Mandatory)] diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index 1ce2ec842..ef2285379 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -17,7 +17,7 @@ function Get-GitHubContext { 'PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Encapsulated in a function. Never leaves as a plain text.' )] - [OutputType([object])] + [OutputType([GitHubContext])] [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')] param( # The name of the context. From 7b4aec167590ba0331ddaa8ffdc170d8585e4cc4 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 14 Dec 2024 01:21:44 +0100 Subject: [PATCH 20/22] Fix regex switch statement to correctly evaluate authentication type in Set-GitHubContext function --- 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 01e0217cd..cb420df53 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 $_ } From 517330096014119fc0693620fe4744bf1ac04213 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 14 Dec 2024 01:39:27 +0100 Subject: [PATCH 21/22] Refactor Assert-GitHubContext to use 'object' for Context parameter and add verbose logging --- .../public/Auth/Assert-GitHubContext.ps1 | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Auth/Assert-GitHubContext.ps1 b/src/functions/public/Auth/Assert-GitHubContext.ps1 index e53b67ae0..ffa46dd35 100644 --- a/src/functions/public/Auth/Assert-GitHubContext.ps1 +++ b/src/functions/public/Auth/Assert-GitHubContext.ps1 @@ -18,16 +18,27 @@ Mandatory, ValueFromPipeline )] - [GitHubContext] $Context, + [object] $Context, # The required authtypes for the command. [Parameter(Mandatory)] [string[]] $AuthType ) - $command = (Get-PSCallStack)[1].Command + begin { + $commandName = $MyInvocation.MyCommand.Name + Write-Verbose "[$commandName] - Start" + } + + process { + $command = (Get-PSCallStack)[1].Command + + if ($Context.AuthType -notin $AuthType) { + throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]." + } + } - if ($Context.AuthType -notin $AuthType) { - throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]." + end { + Write-Verbose "[$commandName] - End" } } From ce2e8cb5992e53b325b1137fa42caa15ea6c6d6d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 14 Dec 2024 21:26:05 +0100 Subject: [PATCH 22/22] Fix URI --- src/functions/public/API/Invoke-GitHubAPI.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/API/Invoke-GitHubAPI.ps1 b/src/functions/public/API/Invoke-GitHubAPI.ps1 index 3ec9dcff6..462390d62 100644 --- a/src/functions/public/API/Invoke-GitHubAPI.ps1 +++ b/src/functions/public/API/Invoke-GitHubAPI.ps1 @@ -131,7 +131,7 @@ $headers | Remove-HashtableEntry -NullOrEmptyValues if (-not $URI) { - $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') + $URI = ("$ApiBaseUri" -replace '/$'), ("$ApiEndpoint" -replace '^/') -join '/' } $APICall = @{