Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7b8a2cd
Added a function to manage API calls.
MariusStorhaug Sep 24, 2023
2ac2b75
Suggestions to refactor
MariusStorhaug Sep 24, 2023
c0c6377
Suggestions
MariusStorhaug Sep 24, 2023
bbf5547
Simplification
MariusStorhaug Sep 24, 2023
e43569f
Suggestions
MariusStorhaug Sep 24, 2023
0709f49
suggestions
MariusStorhaug Sep 24, 2023
618e27f
Added built in autocomplete for HTTP
MariusStorhaug Sep 24, 2023
235c759
test
MariusStorhaug Sep 24, 2023
97a1e8f
added docs
MariusStorhaug Sep 24, 2023
5d5dd23
Soon done :)
MariusStorhaug Sep 24, 2023
2cef822
pagination first attempt
MariusStorhaug Sep 24, 2023
e9ec318
pagination
MariusStorhaug Sep 24, 2023
b95ac43
Stream output to pipeline
MariusStorhaug Sep 24, 2023
eba9120
Updates to the loader
MariusStorhaug Sep 24, 2023
93276d6
Improve InitSecVault
MariusStorhaug Sep 24, 2023
29ff4ef
Make token values secure string
MariusStorhaug Sep 24, 2023
ea23883
fix Initialize-SecretVault
MariusStorhaug Sep 24, 2023
ec63890
fix pathing
MariusStorhaug Sep 24, 2023
8da9e31
fix pathing
MariusStorhaug Sep 24, 2023
9d87f2d
Default to SecretStore, as we only support one :)
MariusStorhaug Sep 24, 2023
06bad48
Add all the things
MariusStorhaug Sep 24, 2023
250a356
Fix data storage
MariusStorhaug Sep 24, 2023
7950cbc
Fix auth issues
MariusStorhaug Sep 24, 2023
dbe236d
All the fixes :)
MariusStorhaug Sep 24, 2023
dea9266
More fixes then...
MariusStorhaug Sep 24, 2023
e6d23ac
Fixes
MariusStorhaug Sep 24, 2023
bf302d8
fixes for -AsPlainText
MariusStorhaug Sep 24, 2023
c3e4feb
Fixes
MariusStorhaug Sep 24, 2023
ab98315
Add debug to the auth calls
MariusStorhaug Sep 24, 2023
56e9af4
Fix logon
MariusStorhaug Sep 24, 2023
692b79a
fix API calls and add ability to get all config
MariusStorhaug Sep 24, 2023
c57c651
Fix API stuff
MariusStorhaug Sep 24, 2023
3c06cca
API fixes
MariusStorhaug Sep 25, 2023
2fc8b2f
Troubleshooting the api function
MariusStorhaug Sep 25, 2023
96329b7
Fixes
MariusStorhaug Sep 25, 2023
520eac6
Remove POC files
MariusStorhaug Sep 25, 2023
3b6b081
Adding Meta component
MariusStorhaug Sep 25, 2023
4cad56e
Fix Meta
MariusStorhaug Sep 25, 2023
d4a22ef
Full test
MariusStorhaug Sep 25, 2023
6a1a4d7
Check the response headers
MariusStorhaug Sep 25, 2023
0088de4
Test API
MariusStorhaug Sep 25, 2023
2ef0590
Final testing stuff
MariusStorhaug Sep 25, 2023
68d3c78
What happends with the framework if i move it like this?
MariusStorhaug Sep 25, 2023
542b4be
Fix loader
MariusStorhaug Sep 25, 2023
1572548
Moved meta back in
MariusStorhaug Sep 25, 2023
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
18 changes: 8 additions & 10 deletions src/GitHub/GitHub.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
Write-Verbose "Initializing GitHub module..." -Verbose
$scriptFilePath = $MyInvocation.MyCommand.Path

Write-Verbose "[$scriptFilePath] - Initializing GitHub module..." -Verbose

$script:Config = $script:ConfigTemplate | ConvertTo-Json -Depth 100 | ConvertFrom-Json
Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type
Restore-GitHubConfig

if (-not [string]::IsNullOrEmpty($env:GH_TOKEN)) {
Write-Verbose 'Logging on using GH_TOKEN'
Connect-GitHubAccount -AccessToken $env:GH_TOKEN
}
if (-not [string]::IsNullOrEmpty($env:GITHUB_TOKEN)) {
Write-Verbose 'Logging on using GITHUB_TOKEN'
Connect-GitHubAccount -AccessToken $env:GITHUB_TOKEN
# Autologon if a token is present in environment variables
$envVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1
$envVarPresent = $envVar.count -gt 0
if ($envVarPresent) {
Connect-GitHubAccount
}
27 changes: 13 additions & 14 deletions src/GitHub/GitHub.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ Write-Verbose "[$scriptName] - Importing module"

#region - Importing data files
Write-Verbose "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Write-Verbose "[$scriptName] - [data] - [$dataFolder]"
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' | ForEach-Object {
$dataFolder = Join-Path $PSScriptRoot 'data'
Get-ChildItem -Path $dataFolder -Recurse -Force -Include '*.psd1' -ErrorAction SilentlyContinue | ForEach-Object {
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing data file"
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
}
Write-Verbose "[$scriptName] - [data] - Done"
#endregion - Importing datas
#endregion - Importing data files

#region - Importing script files
$folders = 'init', 'classes', 'private', 'public'
foreach ($folder in $folders) {
Write-Verbose "[$scriptName] - [$folder] - Processing folder"
$folderPath = Join-Path -Path $PSScriptRoot -ChildPath $folder
$folderPath = Join-Path $PSScriptRoot $folder
if (Test-Path -Path $folderPath) {
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse | Sort-Object -Property FullName
foreach ($file in $files) {
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Importing script file"
Import-Module $file -Verbose:$false
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Done"
}
Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse |
Sort-Object -Property FullName | ForEach-Object {
Write-Verbose "[$scriptName] - [$folder] - [$($_.Name)] - Importing script file"
Import-Module $_ -Verbose:$false
Write-Verbose "[$scriptName] - [$folder] - [$($_.Name)] - Done"
}
}
Write-Verbose "[$scriptName] - [$folder] - Done"
}
Expand All @@ -44,7 +43,7 @@ Write-Verbose "[$scriptName] - [Root] - Done"
#endregion - Importing root script files

#region Export module members
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object -Property Name -In $folders
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object Name -In $folders
$moduleFiles = $foldersToProcess | Get-ChildItem -Include '*.ps1' -Recurse -File -Force
$functions = $moduleFiles.BaseName
$param = @{
Expand All @@ -54,9 +53,9 @@ $param = @{
Alias = '*'
}

Write-Verbose 'Exporting module members'

Write-Verbose "[$scriptName] - Exporting module members"
Export-ModuleMember @param
Write-Verbose "[$scriptName] - Exporting module members - Done"
#endregion Export module members

Write-Verbose "[$scriptName] - Done"
6 changes: 2 additions & 4 deletions src/GitHub/data/SecretVault.psd1
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
@{
Name = 'GitHub' # $script:SecretVault.Name
Name = 'SecretStore' # $script:SecretVault.Name
Type = 'Microsoft.PowerShell.SecretStore' # $script:SecretVault.Type
Secret = @{
Name = 'Config' # $script:SecretVault.Secret.Name
}
Prefix = 'GHPS_' # $script:SecretVault.Prefix
}
17 changes: 2 additions & 15 deletions src/GitHub/en_US/about_Config.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ FUNCTIONS

- Get-GitHubConfig: Fetches the current module configuration.
- Reset-GitHubConfig: Resets all or specific sections to its default values.
- Restore-GitHubConfig: Restores the configuration from the secret vault.
- Save-GitHubConfig: Saves the current configuration to the secret vault.
- Set-GitHubConfig: Allows setting specific elements of the configuration.

CONFIGURATION
Expand Down Expand Up @@ -87,21 +85,10 @@ EXAMPLES

-------------------------- EXAMPLE 3 --------------------------

Restore-GitHubConfig
Reset-GitHubConfig -Scope Auth

This command restores the GitHub configuration from the secret vault.
This command resets the Auth related settings of the GitHub configuration to its default values.

-------------------------- EXAMPLE 4 --------------------------

Reset-GitHubConfig -Scope 'App.API'

This command resets the 'App.API' section of the GitHub configuration to its default values.

-------------------------- EXAMPLE 5 --------------------------

Save-GitHubConfig

This command saves the current GitHub configuration to the secret vault.

KEYWORDS

Expand Down
12 changes: 0 additions & 12 deletions src/GitHub/private/Auth/DeviceFlow/Check-GitHubAccessToken.ps1

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

# The refresh token to use for re-authentication.
[Parameter()]
[string] $RefreshToken
[securestring] $RefreshToken
)

do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
Mandatory,
ParameterSetName = 'RefreshToken'
)]
[string] $RefreshToken
[securestring] $RefreshToken
)

$body = @{
Expand All @@ -44,7 +44,7 @@

if ($PSBoundParameters.ContainsKey('RefreshToken')) {
$body += @{
'refresh_token' = $RefreshToken
'refresh_token' = (ConvertFrom-SecureString $RefreshToken -AsPlainText)
'grant_type' = 'refresh_token'
}
}
Expand All @@ -67,6 +67,8 @@
Write-Verbose ($RESTParams.GetEnumerator() | Out-String)

$tokenResponse = Invoke-RestMethod @RESTParams -Verbose:$false

Write-Verbose ($tokenResponse | ConvertTo-Json | Out-String)
return $tokenResponse
} catch {
Write-Error $_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@
throw $_
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Mandatory,
ParameterSetName = 'RefreshToken'
)]
[string] $RefreshToken,
[securestring] $RefreshToken,

# The interval to wait between polling for the token.
[Parameter()]
Expand Down
28 changes: 0 additions & 28 deletions src/GitHub/private/Config/Config.ps1

This file was deleted.

27 changes: 17 additions & 10 deletions src/GitHub/private/Config/Initialize-SecretVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ function Initialize-SecretVault {
Initializes a secret vault named 'SecretStore' using the 'Microsoft.PowerShell.SecretStore' module.

.NOTES
For more information aobut secret vaults, see https://learn.microsoft.com/en-us/powershell/utility-modules/secretmanagement/overview?view=ps-modules
For more information about secret vaults, see https://learn.microsoft.com/en-us/powershell/utility-modules/secretmanagement/overview?view=ps-modules
#>

[OutputType([void])]
[CmdletBinding()]
param (
# The name of the secret vault.
[Parameter()]
[string] $Name,
[string] $Name = 'SecretStore',

# The type of the secret vault.
[Parameter()]
[Alias('ModuleName')]
[string] $Type
[string] $Type = 'Microsoft.PowerShell.SecretStore'
)

$secretVault = Get-SecretVault | Where-Object { $_.ModuleName -eq $Type }
$secretVaultExists = $secretVault.count -ne 0
Write-Verbose "[$Name] - exists - [$secretVaultExists]"
if (-not $secretVaultExists) {
Write-Verbose "[$Name] - Registering"
$functionName = $MyInvocation.MyCommand.Name

$vault = Get-SecretVault | Where-Object { $_.ModuleName -eq $Type }
if (-not $vault) {
Write-Verbose "[$functionName] - [$Type] - Registering"

switch ($Type) {
'Microsoft.PowerShell.SecretStore' {
Expand All @@ -51,17 +52,23 @@ function Initialize-SecretVault {
Reset-SecretStore @vaultParameters
}
}
Write-Verbose "[$functionName] - [$Type] - Done"
} else {
Write-Verbose "[$functionName] - [$Type] - already registered"
}

$secretStore = Get-SecretVault | Where-Object { $_.Name -eq $Name }
$secretStoreExists = $secretStore.count -ne 0
if (-not $secretStoreExists) {
if (-not $secretStore) {
Write-Verbose "[$functionName] - [$Name] - Registering"
$secretVault = @{
Name = $Name
ModuleName = $Type
DefaultVault = $true
Description = 'SecretStore'
}
Register-SecretVault @secretVault
Write-Verbose "[$functionName] - [$Name] - Done"
} else {
Write-Verbose "[$functionName] - [$Name] - already registered"
}
}
62 changes: 62 additions & 0 deletions src/GitHub/private/Config/Reset-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function Reset-GitHubConfig {
<#
.SYNOPSIS
Reset the GitHub configuration.

.DESCRIPTION
Reset the GitHub configuration. Specific scopes can be reset by using the Scope parameter.

.EXAMPLE
Reset-GitHubConfig

Resets the entire GitHub configuration.

.EXAMPLE
Reset-GitHubConfig -Scope 'Auth'

Resets the Auth related variables of the GitHub configuration.
#>
[Alias('Reset-GHConfig')]
[OutputType([void])]
[CmdletBinding()]
param(
# Reset the GitHub configuration for a specific scope.
[Parameter()]
[ValidateSet('Auth', 'All')]
[string] $Scope = 'All'
)

Write-Verbose "Resetting GitHub configuration for scope '$Scope'..."
switch ($Scope) {
'Auth' {
$Settings = @{
AccessToken = [securestring]::new()
AccessTokenExpirationDate = [datetime]::MinValue
AccessTokenType = ''
AuthType = ''
DeviceFlowType = ''
RefreshToken = [securestring]::new()
RefreshTokenExpirationDate = [datetime]::MinValue
Scope = ''
}
}
'All' {
$Settings = @{
AccessToken = [securestring]::new()
AccessTokenExpirationDate = [datetime]::MinValue
AccessTokenType = ''
ApiBaseUri = 'https://api.github.com'
ApiVersion = '2022-11-28'
AuthType = ''
DeviceFlowType = ''
Owner = ''
RefreshToken = [securestring]::new()
RefreshTokenExpirationDate = [datetime]::MinValue
Repo = ''
Scope = ''
UserName = ''
}
}
}
Set-GitHubConfig @Settings
}
Loading