Skip to content

Commit 2bc08e7

Browse files
MariusStorhaugMarius Storhaug
andauthored
🩹 [Patch]: Strong type GitHubContext properties (#204)
## Description - Changed the types of - `ApiBaseUri` from `string` to `uri` - `TokenExpirationDate` from `string` to `datetime` - Added `Assert-GitHubContext` call to validate the authentication type is `App`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --------- Co-authored-by: Marius Storhaug <Marius.Storhaug@dnb.no>
1 parent bdafc5f commit 2bc08e7

File tree

9 files changed

+29
-13
lines changed

9 files changed

+29
-13
lines changed

src/classes/public/Context/GitHubContext.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class GitHubContext : Context {
1+
class GitHubContext : Context {
22
# The GitHub Context Name.
33
# HostName/Username or HostName/AppSlug
44
# github.com/Octocat
@@ -18,7 +18,7 @@
1818

1919
# The API base URI.
2020
# https://api.github.com
21-
[string] $ApiBaseUri
21+
[uri] $ApiBaseUri
2222

2323
# The GitHub API version.
2424
# 2022-11-28

src/functions/public/API/Invoke-GitHubAPI.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
$headers | Remove-HashtableEntry -NullOrEmptyValues
132132

133133
if (-not $URI) {
134-
$URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '')
134+
$URI = ("$ApiBaseUri" -replace '/$'), ("$ApiEndpoint" -replace '^/') -join '/'
135135
}
136136

137137
$APICall = @{

src/functions/public/Apps/GitHub Apps/Get-GitHubApp.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
)
4141

4242
$Context = Resolve-GitHubContext -Context $Context
43+
Assert-GitHubContext -Context $Context -AuthType App
4344

4445
switch ($PSCmdlet.ParameterSetName) {
4546
'BySlug' {

src/functions/public/Apps/GitHub Apps/Get-GitHubAppInstallation.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727

2828
$Context = Resolve-GitHubContext -Context $Context
29+
Assert-GitHubContext -Context $Context -AuthType App
2930

3031
$inputObject = @{
3132
Context = $Context

src/functions/public/Apps/GitHub Apps/New-GitHubAppInstallationAccessToken.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
)
6767

6868
$Context = Resolve-GitHubContext -Context $Context
69+
Assert-GitHubContext -Context $Context -AuthType App
6970

7071
$inputObject = @{
7172
Context = $Context

src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookConfiguration.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828

2929
$Context = Resolve-GitHubContext -Context $Context
30+
Assert-GitHubContext -Context $Context -AuthType App
3031

3132
$inputObject = @{
3233
Context = $Context

src/functions/public/Apps/Webhooks/Get-GitHubAppWebhookDelivery.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727

2828
$Context = Resolve-GitHubContext -Context $Context
29+
Assert-GitHubContext -Context $Context -AuthType App
2930

3031
$inputObject = @{
3132
Context = $Context

src/functions/public/Auth/Assert-GitHubContext.ps1

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,27 @@
1818
Mandatory,
1919
ValueFromPipeline
2020
)]
21-
[GitHubContext] $Context,
21+
[object] $Context,
2222

2323
# The required authtypes for the command.
2424
[Parameter(Mandatory)]
2525
[string[]] $AuthType
2626
)
2727

28-
$command = (Get-PSCallStack)[1].Command
28+
begin {
29+
$commandName = $MyInvocation.MyCommand.Name
30+
Write-Verbose "[$commandName] - Start"
31+
}
32+
33+
process {
34+
$command = (Get-PSCallStack)[1].Command
35+
36+
if ($Context.AuthType -notin $AuthType) {
37+
throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]."
38+
}
39+
}
2940

30-
if ($Context.AuthType -notin $AuthType) {
31-
throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]."
41+
end {
42+
Write-Verbose "[$commandName] - End"
3243
}
3344
}

src/functions/public/Auth/Connect-GitHubApp.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Connect-GitHubApp {
1+
function Connect-GitHubApp {
22
<#
33
.SYNOPSIS
44
Connects to GitHub as a installation using a GitHub App.
@@ -104,7 +104,7 @@
104104
AuthType = [string]'IAT'
105105
TokenType = [string]'ghs'
106106
DisplayName = [string]$Context.DisplayName
107-
ApiBaseUri = [string]$Context.ApiBaseUri
107+
ApiBaseUri = [uri]$Context.ApiBaseUri
108108
ApiVersion = [string]$Context.ApiVersion
109109
HostName = [string]$Context.HostName
110110
ClientID = [string]$Context.ClientID
@@ -113,18 +113,18 @@
113113
Events = [string[]]$installation.events
114114
TargetType = [string]$installation.target_type
115115
Token = [securestring]$token.Token
116-
TokenExpirationDate = [string]$token.ExpiresAt
116+
TokenExpirationDate = [datetime]$token.ExpiresAt
117117
}
118118

119119
switch ($installation.target_type) {
120120
'User' {
121-
$contextParams['TargetName'] = $installation.account.login
121+
$contextParams['TargetName'] = [string]$installation.account.login
122122
}
123123
'Organization' {
124-
$contextParams['TargetName'] = $installation.account.login
124+
$contextParams['TargetName'] = [string]$installation.account.login
125125
}
126126
'Enterprise' {
127-
$contextParams['TargetName'] = $installation.account.slug
127+
$contextParams['TargetName'] = [string]$installation.account.slug
128128
}
129129
}
130130
Write-Verbose 'Logging in using a managed installation access token...'

0 commit comments

Comments
 (0)