Skip to content

Commit 31e17d6

Browse files
🩹 [Patch]: Bump Context and Sodium to latest versions (#401)
## Description This pull request updates the GitHub module to improve context management by introducing a `ContextVault` parameter and upgrading module dependencies. The most significant changes include adding the `ContextVault` parameter to context-related functions, updating the required module versions, and modifying the default configuration structure. As a result of these changes, the `Anonymous` evaluation and default context resolution is changed. ### Context Management Updates * Added the `ContextVault` parameter to `Set-Context` and `Get-Context` calls across multiple functions, ensuring context data is stored and retrieved from the specified vault (`$script:GitHub.ContextVault`). * Introduced a new `ContextVault` property in the `$script:GitHub` object to centralize vault configuration. ### Dependency Updates * `Context` module from version `7.0.2` to `8.0.2` * `Sodium` module from version `2.1.2` to `2.2.0` ### Context resolution * Moved the `Anonymous` and default context resolution to the `Resolve-GitHubContext` function to streamline context retrieval. ## 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
1 parent 903cdd8 commit 31e17d6

File tree

178 files changed

+299
-308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+299
-308
lines changed

examples/Apps/Get-WebhookDelivery.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $deliveries | Where-Object { $_.Event -eq 'team' } | Format-Table -AutoSize
2626

2727
$Return.Response | Format-Table -AutoSize
2828

29-
Set-GitHubDefaultContext -Context 'msx.ghe.com/Marius-Storhaug'
29+
Switch-GitHubContext -Context 'msx.ghe.com/Marius-Storhaug'
3030

3131
1..10 | ForEach-Object {
3232
New-GitHubTeam -Organization 'my-org' -Name "Test$_"

examples/Connecting.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '7.0.2' }
2-
3-
###
1+
###
42
### CONNECTING
53
###
64

@@ -40,16 +38,15 @@ Get-GitHubContext -ListAvailable
4038
Get-GitHubContext -Context 'msx.ghe.com/MariusStorhaug'
4139

4240
# Take a name dynamically from Get-GitHubContext? Autocomplete the name
43-
Set-GitHubDefaultContext -Context 'msx.ghe.com/MariusStorhaug'
41+
Switch-GitHubContext -Context 'msx.ghe.com/MariusStorhaug'
4442

4543
# Set a specific context as the default context using pipeline
46-
'msx.ghe.com/MariusStorhaug' | Set-GitHubDefaultContext
44+
'msx.ghe.com/MariusStorhaug' | Switch-GitHubContext
4745

48-
Get-GitHubContext -Context 'github.com/MariusStorhaug' | Set-GitHubDefaultContext
46+
Get-GitHubContext -Context 'github.com/MariusStorhaug' | Switch-GitHubContext
4947

5048
# Abstraction layers on GitHubContexts
5149
Get-GitHubContext -Context 'msx.ghe.com/MariusStorhaug'
52-
Get-Context -ID 'PSModule.GitHub/msx.ghe.com/MariusStorhaug'
5350

5451
###
5552
### DISCONNECTING

src/classes/public/Config/GitHubConfig.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[string] $ID
44

55
# The access token grace period in hours.
6-
[int] $AccessTokenGracePeriodInHours
6+
[System.Nullable[int]] $AccessTokenGracePeriodInHours
77

88
# The default context.
99
[string] $DefaultContext
@@ -27,16 +27,16 @@
2727
[string] $HttpVersion
2828

2929
# The default value for the 'per_page' API parameter used in 'GET' functions that support paging.
30-
[int] $PerPage
30+
[System.Nullable[int]] $PerPage
3131

3232
# The default value for retry count.
33-
[int] $RetryCount
33+
[System.Nullable[int]] $RetryCount
3434

3535
# The default value for retry interval in seconds.
36-
[int] $RetryInterval
36+
[System.Nullable[int]] $RetryInterval
3737

3838
# The tolerance time in seconds for JWT token validation.
39-
[int] $JwtTimeTolerance
39+
[System.Nullable[int]] $JwtTimeTolerance
4040

4141
# Simple parameterless constructor
4242
GitHubConfig() {}

src/functions/private/Apps/GitHub Apps/Get-GitHubAppInstallableOrganization.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
begin {
3535
$stackPath = Get-PSCallStackPath
3636
Write-Debug "[$stackPath] - Start"
37-
$Context = Resolve-GitHubContext -Context $Context
3837
Assert-GitHubContext -Context $Context -AuthType IAT, UAT
3938
# enterprise_organization_installations=read
4039
}

src/functions/private/Auth/Context/Assert-GitHubContext.ps1

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,32 @@
3535

3636
process {
3737
$command = (Get-PSCallStack)[1].Command
38+
Write-Verbose "Context: $Context"
39+
Write-Verbose "AuthType: $AuthType"
3840

39-
if ($Context) {
40-
if ($Context.AuthType -in $AuthType) {
41-
return
42-
}
43-
$PSCmdlet.ThrowTerminatingError(
44-
[System.Management.Automation.ErrorRecord]::new(
45-
[System.Exception]::new("The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]."),
46-
"InvalidContextAuthType",
47-
[System.Management.Automation.ErrorCategory]::InvalidArgument,
48-
$Context
49-
)
50-
)
51-
} else {
41+
if (-not $Context) {
5242
if ('Anonymous' -in $AuthType) {
5343
return
5444
}
5545
$PSCmdlet.ThrowTerminatingError(
5646
[System.Management.Automation.ErrorRecord]::new(
5747
[System.Exception]::new("Please provide a valid context or log in using 'Connect-GitHub'."),
58-
"InvalidContext",
48+
'InvalidContext',
5949
[System.Management.Automation.ErrorCategory]::InvalidArgument,
6050
$Context
6151
)
6252
)
6353
}
64-
54+
if ($Context -eq 'Anonymous' -and $AuthType -contains 'Anonymous') { return }
55+
if ($Context.AuthType -in $AuthType) { return }
56+
$PSCmdlet.ThrowTerminatingError(
57+
[System.Management.Automation.ErrorRecord]::new(
58+
[System.Exception]::new("The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$command]."),
59+
'InvalidContextAuthType',
60+
[System.Management.Automation.ErrorCategory]::InvalidArgument,
61+
$Context
62+
)
63+
)
6564
# TODO: Implement permission check
6665
# if ($Context.AuthType -in 'IAT' -and $Context.Permission -notin $Permission) {
6766
# throw "The context '$($Context.Name)' does not match the required Permission [$Permission] for [$command]."

src/functions/private/Auth/Context/Remove-GitHubContext.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '7.0.2' }
2-
3-
filter Remove-GitHubContext {
1+
filter Remove-GitHubContext {
42
<#
53
.SYNOPSIS
64
Removes a context from the context vault.
@@ -36,14 +34,13 @@ filter Remove-GitHubContext {
3634
}
3735

3836
process {
39-
$ID = "$($script:GitHub.Config.ID)/$Context"
40-
4137
if ($PSCmdlet.ShouldProcess($context.Name, 'Remove context')) {
42-
Remove-Context -ID $ID
38+
Remove-Context -ID $Context -Vault $script:GitHub.ContextVault
4339
}
4440
}
4541

4642
end {
4743
Write-Debug "[$stackPath] - End"
4844
}
4945
}
46+
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.0.2' }

src/functions/private/Auth/Context/Resolve-GitHubContext.ps1

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
# Can be either a string or a GitHubContext object.
2828
[Parameter(Mandatory, ValueFromPipeline)]
2929
[AllowNull()]
30-
[object] $Context
30+
[object] $Context,
31+
32+
# If specified, makes an anonymous request to the GitHub API without authentication.
33+
[Parameter()]
34+
[bool] $Anonymous
3135
)
3236

3337
begin {
@@ -37,10 +41,25 @@
3741
}
3842

3943
process {
44+
Write-Verbose "Context: [$Context]"
45+
Write-Verbose "Anonymous: [$Anonymous]"
46+
if ($Anonymous -or $Context -eq 'Anonymous') {
47+
Write-Verbose 'Returning Anonymous context.'
48+
return [GitHubContext]@{
49+
Name = 'Anonymous'
50+
AuthType = 'Anonymous'
51+
}
52+
}
53+
4054
if ($Context -is [string]) {
4155
$contextName = $Context
42-
Write-Debug "Getting context: [$contextName]"
43-
$Context = Get-GitHubContext -Context $contextName
56+
Write-Verbose "Getting context: [$contextName]"
57+
return Get-GitHubContext -Context $contextName
58+
}
59+
60+
if ($null -eq $Context) {
61+
Write-Verbose 'Context is null, returning default context.'
62+
return Get-GitHubContext
4463
}
4564

4665
# TODO: Implement App installation context resolution

src/functions/private/Auth/Context/Set-GitHubContext.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '7.0.2' }
2-
3-
function Set-GitHubContext {
1+
function Set-GitHubContext {
42
<#
53
.SYNOPSIS
64
Sets the GitHub context and stores it in the context vault.
@@ -143,8 +141,8 @@ function Set-GitHubContext {
143141
$contextObj | Out-String -Stream | ForEach-Object { Write-Debug $_ }
144142
Write-Debug '----------------------------------------------------'
145143
if ($PSCmdlet.ShouldProcess('Context', 'Set')) {
146-
Write-Debug "Saving context: [$($script:GitHub.Config.ID)/$($contextObj['Name'])]"
147-
Set-Context -ID "$($script:GitHub.Config.ID)/$($contextObj['Name'])" -Context $contextObj
144+
Write-Debug "Saving context: [$($contextObj['Name'])]"
145+
Set-Context -ID $($contextObj['Name']) -Context $contextObj -Vault $script:GitHub.ContextVault
148146
if ($Default) {
149147
Switch-GitHubContext -Context $contextObj['Name']
150148
}
@@ -170,3 +168,4 @@ function Set-GitHubContext {
170168
Write-Debug "[$stackPath] - End"
171169
}
172170
}
171+
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.0.2' }

src/functions/private/Auth/DeviceFlow/Update-GitHubUserAccessToken.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
$Context.RefreshTokenExpirationDate = (Get-Date).AddSeconds($tokenResponse.refresh_token_expires_in)
8686

8787
if ($PSCmdlet.ShouldProcess('Access token', 'Update/refresh')) {
88-
Set-Context -Context $Context -ID $Context.ID
88+
Set-Context -Context $Context -Vault $script:GitHub.ContextVault
8989
}
9090

9191
if ($PassThru) {
@@ -98,4 +98,4 @@
9898
Write-Debug "[$stackPath] - End"
9999
}
100100
}
101-
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '7.0.2' }
101+
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.0.2' }

src/functions/private/Config/Initialize-GitHubConfig.ps1

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '7.0.2' }
2-
3-
function Initialize-GitHubConfig {
1+
function Initialize-GitHubConfig {
42
<#
53
.SYNOPSIS
64
Initialize the GitHub module configuration.
@@ -34,7 +32,7 @@ function Initialize-GitHubConfig {
3432
Write-Debug "Force: [$Force]"
3533
if ($Force) {
3634
Write-Debug 'Forcing initialization of GitHubConfig.'
37-
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $script:GitHub.DefaultConfig -PassThru
35+
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
3836
$script:GitHub.Config = [GitHubConfig]$context
3937
return
4038
}
@@ -46,11 +44,11 @@ function Initialize-GitHubConfig {
4644
}
4745

4846
Write-Debug 'Attempt to load the stored GitHubConfig from ContextVault'
49-
$context = Get-Context -ID $script:GitHub.DefaultConfig.ID
47+
$context = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault
5048
if ($context) {
5149
Write-Debug 'GitHubConfig loaded into memory.'
5250

53-
Write-Debug "Checking if new default properties are available in the stored context."
51+
Write-Debug 'Checking if new default properties are available in the stored context.'
5452
$needsUpdate = $false
5553
$defaultProperties = $script:GitHub.DefaultConfig.PSObject.Properties.Name
5654
foreach ($propName in $defaultProperties) {
@@ -62,18 +60,19 @@ function Initialize-GitHubConfig {
6260
}
6361
if ($needsUpdate) {
6462
Write-Debug 'Updating stored context with new default properties'
65-
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $context -PassThru
63+
$context = Set-Context -Context $context -Vault $script:GitHub.ContextVault -PassThru
6664
}
6765

6866
$script:GitHub.Config = [GitHubConfig]$context
6967
return
7068
}
7169
Write-Debug 'Initializing GitHubConfig from defaults'
72-
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $script:GitHub.DefaultConfig -PassThru
70+
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
7371
$script:GitHub.Config = [GitHubConfig]$context
7472
}
7573

7674
end {
7775
Write-Debug "[$stackPath] - End"
7876
}
7977
}
78+
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.0.2' }

0 commit comments

Comments
 (0)