Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 [Patch]: Bump Context to 5.0.3 #194

Merged
merged 7 commits into from
Dec 9, 2024
Merged
14 changes: 14 additions & 0 deletions src/classes/public/Config/GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@

# Simple parameterless constructor
GitHubConfig() {}

# Creates a context object from a hashtable of key-vaule pairs.
GitHubConfig([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}

# Creates a context object from a PSCustomObject.
GitHubConfig([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
37 changes: 27 additions & 10 deletions src/functions/private/Config/Initialize-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
function Initialize-GitHubConfig {
<#
.SYNOPSIS
Initialize the GitHub module configuration.

.DESCRIPTION
Initialize the GitHub module configuration.

.EXAMPLE
Initialize-GitHubConfig

Initializes the GitHub module configuration.

.EXAMPLE
Initialize-GitHubConfig -Force

Forces the initialization of the GitHub module configuration.
#>
[OutputType([void])]
[CmdletBinding()]
param ()
param (
# Force the initialization of the GitHub config.
[switch] $Force
)

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
}

process {
try {
$context = [GitHubConfig](Get-Context -ID $script:GitHub.Config.ID)
if (-not $context) {
$context = Set-Context -ID $script:GitHub.Config.ID -Context $script:GitHub.Config -PassThru
Write-Verbose "GitHubConfig initialized: [$($script:GitHub.Initialized)]"
Write-Verbose "Force: [$Force]"
if (-not $script:GitHub.Initialized -or $Force) {
try {
$context = [GitHubConfig](Get-Context -ID $script:GitHub.Config.ID)
if (-not $context -or $Force) {
Write-Verbose "Loading GitHubConfig from defaults"
$context = Set-Context -ID $script:GitHub.DefaultConfig.ID -Context $script:GitHub.DefaultConfig -PassThru
}
$script:GitHub.Config = [GitHubConfig]$context
$script:GitHub.Initialized = $true
} catch {
Write-Error $_
throw 'Failed to initialize GitHub config'
}
$script:GitHub.Config = [GitHubConfig]$context
$script:GitHub.Initialized = $true
} catch {
Write-Error $_
throw 'Failed to initialize GitHub config'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/functions/public/Auth/Context/Get-GitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

function Get-GitHubContext {
<#
Expand Down Expand Up @@ -39,6 +39,7 @@ function Get-GitHubContext {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$null = Get-GitHubConfig
}

process {
Expand Down
21 changes: 14 additions & 7 deletions src/functions/public/Auth/Context/Remove-GitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

filter Remove-GitHubContext {
<#
Expand Down Expand Up @@ -29,14 +29,21 @@ filter Remove-GitHubContext {
[string] $Context
)

$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$null = Get-GitHubConfig
}

$ID = "$($script:GitHub.Config.ID)/$Context"
process {
$ID = "$($script:GitHub.Config.ID)/$Context"

if ($PSCmdlet.ShouldProcess('Remove-Secret', $context.Name)) {
Remove-Context -ID $ID
if ($PSCmdlet.ShouldProcess('Remove-Secret', $context.Name)) {
Remove-Context -ID $ID
}
}

Write-Verbose "[$commandName] - End"
end {
Write-Verbose "[$commandName] - End"
}
}
3 changes: 2 additions & 1 deletion src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

function Set-GitHubContext {
<#
Expand Down Expand Up @@ -41,6 +41,7 @@ function Set-GitHubContext {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
$null = Get-GitHubConfig
}

process {
Expand Down
12 changes: 2 additions & 10 deletions src/functions/public/Config/Get-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

function Get-GitHubConfig {
<#
Expand All @@ -24,15 +24,7 @@ function Get-GitHubConfig {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
try {
if (-not $script:GitHub.Initialized) {
Initialize-GitHubConfig
Write-Debug "Connected to context [$($script:GitHub.Config.ID)]"
}
} catch {
Write-Error $_
throw 'Failed to initialize secret vault'
}
Initialize-GitHubConfig
}

process {
Expand Down
12 changes: 2 additions & 10 deletions src/functions/public/Config/Remove-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

function Remove-GitHubConfig {
<#
Expand All @@ -23,15 +23,7 @@ function Remove-GitHubConfig {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
try {
if (-not $script:GitHub.Initialized) {
Initialize-GitHubConfig
Write-Debug "Connected to context [$($script:GitHub.Config.ID)]"
}
} catch {
Write-Error $_
throw 'Failed to initialize secret vault'
}
Initialize-GitHubConfig
}

process {
Expand Down
37 changes: 37 additions & 0 deletions src/functions/public/Config/Reset-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Reset-GitHubConfig {
<#
.SYNOPSIS
Re-initializes the GitHub module configuration.

.DESCRIPTION
Re-initializes the GitHub module configuration.

.EXAMPLE
Reset-GitHubConfig

Re-initializes the GitHub module configuration.
#>
[OutputType([void])]
[CmdletBinding(SupportsShouldProcess)]
param ()

begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Debug "[$commandName] - Start"
}

process {
try {
if ($PSCmdlet.ShouldProcess('GitHubConfig', 'Reset')) {
Initialize-GitHubConfig -Force
}
} catch {
Write-Error $_
throw 'Failed to reset GitHub module configuration.'
}
}

end {
Write-Debug "[$commandName] - End"
}
}
12 changes: 2 additions & 10 deletions src/functions/public/Config/Set-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.1' }
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '5.0.3' }

function Set-GitHubConfig {
<#
Expand Down Expand Up @@ -27,15 +27,7 @@ function Set-GitHubConfig {
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
try {
if (-not $script:GitHub.Initialized) {
Initialize-GitHubConfig
Write-Debug "Connected to context [$($script:GitHub.Config.ID)]"
}
} catch {
Write-Error $_
throw 'Failed to initialize secret vault'
}
Initialize-GitHubConfig
}

process {
Expand Down
2 changes: 0 additions & 2 deletions src/loader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Write-Verbose 'Initializing GitHub PowerShell module...'
Write-Verbose "Path: $scriptFilePath"

Initialize-GitHubConfig

switch ($script:GitHub.EnvironmentType) {
'GHA' {
Write-Verbose 'Detected running on a GitHub Actions runner, preparing environment...'
Expand Down
3 changes: 2 additions & 1 deletion src/variables/private/Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
} else {
'Local'
}
Config = [GitHubConfig]@{
DefaultConfig = [GitHubConfig]@{
ID = 'PSModule.GitHub'
HostName = $env:GITHUB_SERVER_URL ?? 'github.com'
AccessTokenGracePeriodInHours = 4
GitHubAppClientID = 'Iv1.f26b61bc99e69405'
OAuthAppClientID = '7204ae9b0580f2cb8288'
DefaultContext = ''
}
Config = [GitHubConfig]::new()
}
1 change: 1 addition & 0 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ param()

BeforeAll {
Get-SecretInfo | Remove-Secret
Get-SecretVault | Unregister-SecretVault
}

Describe 'GitHub' {
Expand Down
Loading