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

🚀 [Feature]: Add classes and restructure context handling #191

Merged
merged 67 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
ef673e3
Test docs
MariusStorhaug Dec 7, 2024
9d2588f
I mean, what can go wrong?
MariusStorhaug Dec 7, 2024
a38198b
test
MariusStorhaug Dec 7, 2024
7fa93fb
Update the header with the using module Context to get the class impo…
MariusStorhaug Dec 7, 2024
ba0b5c8
Fix config
MariusStorhaug Dec 7, 2024
8aee6e1
Fix
MariusStorhaug Dec 7, 2024
6146aec
test
MariusStorhaug Dec 7, 2024
f9a12f9
test
MariusStorhaug Dec 7, 2024
0abe203
Fix
MariusStorhaug Dec 7, 2024
e613d31
Add properties to GitHubConfig class for configuration settings
MariusStorhaug Dec 7, 2024
5f18d92
test
MariusStorhaug Dec 7, 2024
d0af627
Add blank line for improved readability in loader.ps1
MariusStorhaug Dec 8, 2024
6b47071
Add module requirements to GitHub configuration functions and update …
MariusStorhaug Dec 8, 2024
232ace8
test
MariusStorhaug Dec 8, 2024
00f8d9a
Refactor Remove-GitHubConfig function to improve context handling and…
MariusStorhaug Dec 8, 2024
6b521df
Fix
MariusStorhaug Dec 8, 2024
d8f25f1
Add a name of the context
MariusStorhaug Dec 8, 2024
e7ce8f1
test
MariusStorhaug Dec 8, 2024
bd291d1
Refactor context retrieval to simplify syntax and improve readability
MariusStorhaug Dec 8, 2024
faf7171
Test what happens with Config during tests
MariusStorhaug Dec 8, 2024
e6ef2e1
Bump to 4.0.5
MariusStorhaug Dec 8, 2024
f59fa59
Fix loader
MariusStorhaug Dec 8, 2024
703bc8f
Moved GitHubconfig initializer to separate function
MariusStorhaug Dec 8, 2024
8dd4ef5
Refactor environment detection to use switch statement for GitHub Act…
MariusStorhaug Dec 8, 2024
ad98acb
Refactor GitHub configuration structure and update required module ve…
MariusStorhaug Dec 8, 2024
b3a9232
Refactor GitHub configuration references to use consistent naming and…
MariusStorhaug Dec 8, 2024
c7528f8
Fix
MariusStorhaug Dec 8, 2024
cd04eb8
Enhance argument completer to filter properties based on user input
MariusStorhaug Dec 8, 2024
21fb701
Refactor GitHub authentication scripts to use centralized configurati…
MariusStorhaug Dec 8, 2024
545509f
Replace Disconnect-GitHubAccount with Get-SecretInfo and Remove-Secre…
MariusStorhaug Dec 8, 2024
9ba3bdd
Fix
MariusStorhaug Dec 8, 2024
0de0eb3
Refactor GitHub context handling and improve verbose logging in authe…
MariusStorhaug Dec 8, 2024
85a4675
Improve verbose logging for GitHub authentication and context setup
MariusStorhaug Dec 8, 2024
28acae1
Enhance Get-FunctionParameter to support hashtable output and streaml…
MariusStorhaug Dec 8, 2024
580cbca
Fix Set-GitHubContext
MariusStorhaug Dec 8, 2024
bec1a72
Setup all classes and fix in set
MariusStorhaug Dec 8, 2024
6fa4037
MOved extension classes to a folder named the same as the parent class
MariusStorhaug Dec 8, 2024
0981ced
Update module requirement version to 5.0.1 in GitHub context and conf…
MariusStorhaug Dec 8, 2024
990ebbc
Refactor GitHub context initialization and retrieval for improved typ…
MariusStorhaug Dec 8, 2024
b8ffdbf
Refactor GitHub context handling to use hashtables for improved reada…
MariusStorhaug Dec 8, 2024
c4f6c45
FIx Set-GitHubContext
MariusStorhaug Dec 8, 2024
f4dd45e
Refactor Get-GitHubContext and Set-GitHubContext for improved paramet…
MariusStorhaug Dec 8, 2024
2b9353e
Refactor GitHub configuration and context functions for improved verb…
MariusStorhaug Dec 8, 2024
6f8224a
Add 'TypeNotFound' to excluded rules in PowerShell Script Analyzer co…
MariusStorhaug Dec 8, 2024
c33f075
Refactor Get-GitHubContext for improved structure and verbosity
MariusStorhaug Dec 8, 2024
ffbee28
Enhance verbosity in Set-GitHubContext by formatting context output f…
MariusStorhaug Dec 8, 2024
e7cef36
Add passwordless constructors
MariusStorhaug Dec 8, 2024
5574637
Reorganize severity settings and remove 'TypeNotFound' from excluded …
MariusStorhaug Dec 8, 2024
21ccde7
test
MariusStorhaug Dec 8, 2024
c458ae9
test
MariusStorhaug Dec 9, 2024
4e44195
Fix :)
MariusStorhaug Dec 9, 2024
689f9d9
Remove auto detection
MariusStorhaug Dec 9, 2024
ba4e784
Check commands in GitConfig
MariusStorhaug Dec 9, 2024
5eb2475
Refactor GitHub context handling and improve verbosity in authenticat…
MariusStorhaug Dec 9, 2024
f186b5e
Refactor context handling in Set-GitHubContext function for improved …
MariusStorhaug Dec 9, 2024
dd8bdb7
Add verbose logging for token details in GitHub context functions
MariusStorhaug Dec 9, 2024
e728b88
Disable PowerShell linter :(
MariusStorhaug Dec 9, 2024
e932613
test
MariusStorhaug Dec 9, 2024
98b6f04
Add verbose logging for context details in Get-GitHubContext function
MariusStorhaug Dec 9, 2024
90cfb54
Enhance verbose logging in Get-GitHubContext function to include all …
MariusStorhaug Dec 9, 2024
19f3eb7
Instead of casting, use construction function
MariusStorhaug Dec 9, 2024
81f1442
Fix
MariusStorhaug Dec 9, 2024
34b59ca
Fix nested app logon
MariusStorhaug Dec 9, 2024
ea767ad
Refactor GitHub context functions to remove sensitive token logging a…
MariusStorhaug Dec 9, 2024
3a93bbc
Test all contexts output
MariusStorhaug Dec 9, 2024
b3fdcb8
Fix context naming for IATs
MariusStorhaug Dec 9, 2024
b4c3621
Clean up variables and force garbage collection in GitHub authenticat…
MariusStorhaug Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/linters/.powershell-psscriptanalyzer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
@{
#CustomRulePath='path\to\CustomRuleModule.psm1'
#RecurseCustomRulePath='path\of\customrules'
#Severity = @(
# 'Error'
# 'Warning'
#)
# Severity = @(
# 'Error'
# 'Warning'
# )
#IncludeDefaultRules=${true}
ExcludeRules = @(
'PSMissingModuleManifestField'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/Linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
VALIDATE_MARKDOWN_PRETTIER: false
VALIDATE_YAML_PRETTIER: false
VALIDATE_GITLEAKS: false
VALIDATE_POWERSHELL: false # Added due to class usage in the project... TypeNotFound
19 changes: 19 additions & 0 deletions src/classes/public/Config/GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class GitHubConfig : Context {
# The access token grace period in hours.
[int] $AccessTokenGracePeriodInHours

# The default context.
[string] $DefaultContext

# The default GitHub App client ID.
[string] $GitHubAppClientID

# The default host name.
[string] $HostName

# The default OAuth app client ID.
[string] $OAuthAppClientID

# Simple parameterless constructor
GitHubConfig() {}
}
58 changes: 58 additions & 0 deletions src/classes/public/Context/GitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class GitHubContext : Context {
# The GitHub Context Name.
# HostName/Username or HostName/AppSlug
# github.com/Octocat
[string] $Name

# The context type
# User / App / Installation
[string] $Type

# The API hostname.
# github.com / msx.ghe.com / github.local
[string] $HostName

# The API base URI.
# https://api.github.com
[string] $ApiBaseUri

# The GitHub API version.
# 2022-11-28
[string] $ApiVersion

# The authentication type.
# UAT / PAT / App / IAT
[string] $AuthType

# User ID / App ID as GraphQL Node ID
[string] $NodeID

# The Database ID of the context.
[string] $DatabaseID

# The user name.
[string] $UserName

# The access token.
[securestring] $Token

# The token type.
# ghu / gho / ghp / github_pat / PEM / ghs /
[string] $TokenType

# The default value for the Enterprise parameter.
[string] $Enterprise

# The default value for the Owner parameter.
[string] $Owner

# The default value for the Repo parameter.
[string] $Repo

# Simple parameterless constructor
GitHubContext() {}

[string] ToString() {
return $this.Name
}
}
18 changes: 18 additions & 0 deletions src/classes/public/Context/GitHubContext/AppGitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AppGitHubContext : GitHubContext {
# Client ID for GitHub Apps
[string] $ClientID

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

# Creates a context object from a PSCustomObject.
AppGitHubContext([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class InstallationGitHubContext : GitHubContext {
# Client ID for GitHub Apps
[string] $ClientID

# The token expiration date.
# 2024-01-01-00:00:00
[datetime] $TokenExpirationDate

# The installation ID.
[int] $InstallationID

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

# Creates a context object from a PSCustomObject.
InstallationGitHubContext([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
38 changes: 38 additions & 0 deletions src/classes/public/Context/GitHubContext/UserGitHubContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class UserGitHubContext : GitHubContext {
# The authentication client ID.
# Client ID for UAT
[string] $AuthClientID

# The device flow type.
# GitHubApp / OAuthApp
[string] $DeviceFlowType

# The scope when authenticating with OAuth.
# 'gist read:org repo workflow'
[string] $Scope

# The token expiration date.
# 2024-01-01-00:00:00
[datetime] $TokenExpirationDate

# The refresh token.
[securestring] $RefreshToken

# The refresh token expiration date.
# 2024-01-01-00:00:00
[datetime] $RefreshTokenExpirationDate

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

# Creates a context object from a PSCustomObject.
UserGitHubContext([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
105 changes: 0 additions & 105 deletions src/classes/public/GitHubContext.ps1

This file was deleted.

31 changes: 17 additions & 14 deletions src/functions/private/Auth/Context/Resolve-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,35 @@
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
Write-Verbose 'Context:'
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$Context | Format-Table | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
}

process {
if ($Context -is [string]) {
$contextName = $Context
Write-Debug "Getting context: [$contextName]"
Write-Verbose "Getting context: [$contextName]"
$Context = Get-GitHubContext -Context $contextName
}

if (-not $Context) {
throw "Context [$contextName] not found. Please provide a valid context or log in using 'Connect-GitHub'."
}

switch ($Context.Type) {
'App' {
$availableContexts = Get-GitHubContext -ListAvailable |
Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID }
$params = Get-FunctionParameter -Scope 2
Write-Verbose 'Resolving parameters used in called function'
Write-Verbose ($params | Out-String)
if ($params.Keys -in 'Owner', 'Organization') {
$Context = $availableContexts | Where-Object { $_.Owner -eq $params.Owner }
}
}
}
# switch ($Context.Type) {
# 'App' {
# $availableContexts = Get-GitHubContext -ListAvailable |
# Where-Object { $_.Type -eq 'Installation' -and $_.ClientID -eq $Context.ClientID }
# $params = Get-FunctionParameter -Scope 2
# Write-Verbose 'Resolving parameters used in called function'
# Write-Verbose ($params | Out-String)
# if ($params.Keys -in 'Owner', 'Organization') {
# $Context = $availableContexts | Where-Object { $_.Owner -eq $params.Owner }
# }
# }
# }

Write-Verbose 'Resolved Context:'
$Context | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
}

end {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
)

$Context = Resolve-GitHubContext -Context $Context

$gitHubConfig = Get-GitHubConfig
$tokenExpirationDate = $Context.TokenExpirationDate
$currentDateTime = Get-Date
$remainingDuration = [datetime]$tokenExpirationDate - $currentDateTime

# If the remaining time is less that $script:Auth.AccessTokenGracePeriodInHours then the token should be refreshed
$remainingDuration.TotalHours -lt $gitHubConfig.AccessTokenGracePeriodInHours
$remainingDuration.TotalHours -lt $script:GitHub.Config.AccessTokenGracePeriodInHours
}
36 changes: 36 additions & 0 deletions src/functions/private/Config/Initialize-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function Initialize-GitHubConfig {
<#
.SYNOPSIS

.DESCRIPTION

.EXAMPLE
Initialize-GitHubConfig
#>
[OutputType([void])]
[CmdletBinding()]
param ()

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
}
$script:GitHub.Config = [GitHubConfig]$context
$script:GitHub.Initialized = $true
} catch {
Write-Error $_
throw 'Failed to initialize GitHub config'
}
}

end {
Write-Debug "[$commandName] - End"
}
}
Loading
Loading