Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions src/classes/public/Config/GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
# The default OAuth app client ID.
[string] $OAuthAppClientID

# The default value for the GitHub API version to use.
[string] $ApiVersion

# The default value for the HTTP protocol version.
[string] $HttpVersion

# The default value for the 'per_page' API parameter used in 'Get' functions that support paging.
[int] $PerPage

# Simple parameterless constructor
GitHubConfig() {}

Expand Down
3 changes: 3 additions & 0 deletions src/classes/public/Context/GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class GitHubContext {
# The default value for the Repo parameter.
[string] $Repo

# The default value for the HTTP protocol version.
[string] $HttpVersion

# The default value for the 'per_page' API parameter used in 'Get' functions that support paging.
[int] $PerPage

Expand Down
17 changes: 11 additions & 6 deletions src/functions/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

# Specifies the HTTP version used for the request.
[Parameter()]
[version] $HttpVersion = '2.0',
[string] $HttpVersion,

# The file path to be used for the API request. This is used for uploading files.
[Parameter()]
Expand Down Expand Up @@ -97,20 +97,25 @@
$Token = $Context.Token
Write-Debug "Token : [$Token]"

if ([string]::IsNullOrEmpty($TokenType)) {
$TokenType = $Context.TokenType
if ([string]::IsNullOrEmpty($HttpVersion)) {
$HttpVersion = $Context.HttpVersion
}
Write-Debug "TokenType : [$($Context.TokenType)]"
Write-Debug "HttpVersion: [$HttpVersion]"

if ([string]::IsNullOrEmpty($ApiBaseUri)) {
$ApiBaseUri = $Context.ApiBaseUri
}
Write-Debug "ApiBaseUri: [$($Context.ApiBaseUri)]"
Write-Debug "ApiBaseUri: [$ApiBaseUri]"

if ([string]::IsNullOrEmpty($ApiVersion)) {
$ApiVersion = $Context.ApiVersion
}
Write-Debug "ApiVersion: [$($Context.ApiVersion)]"
Write-Debug "ApiVersion: [$ApiVersion]"

if ([string]::IsNullOrEmpty($TokenType)) {
$TokenType = $Context.TokenType
}
Write-Debug "TokenType : [$TokenType]"

switch ($TokenType) {
'ghu' {
Expand Down
24 changes: 12 additions & 12 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@
[Alias('Repository')]
[string] $Repo,

# API version used for API requests.
[Parameter()]
[string] $ApiVersion = '2022-11-28',

# The host to connect to. Can use $env:GITHUB_SERVER_URL to set the host, as the protocol is removed automatically.
# Example: github.com, github.enterprise.com, msx.ghe.com
[Parameter()]
Expand Down Expand Up @@ -166,6 +162,9 @@
if (-not $HostName) {
$HostName = $script:GitHub.Config.HostName
}
$httpVersion = $script:GitHub.Config.HttpVersion
$perPage = $script:GitHub.Config.PerPage
$ApiVersion = $script:GitHub.Config.ApiVersion
$HostName = $HostName -replace '^https?://'
$ApiBaseUri = "https://api.$HostName"
$authType = $PSCmdlet.ParameterSetName
Expand All @@ -184,14 +183,15 @@
}

$context = @{
ApiBaseUri = [string]$ApiBaseUri
ApiVersion = [string]$ApiVersion
HostName = [string]$HostName
AuthType = [string]$authType
Enterprise = [string]$Enterprise
Owner = [string]$Owner
Repo = [string]$Repo
PerPage = 100
ApiBaseUri = [string]$ApiBaseUri
ApiVersion = [string]$ApiVersion
HostName = [string]$HostName
HttpVersion = [string]$httpVersion
PerPage = [int]$perPage
AuthType = [string]$authType
Enterprise = [string]$Enterprise
Owner = [string]$Owner
Repo = [string]$Repo
}

Write-Verbose ($context | Format-Table | Out-String)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
ApiBaseUri = [string]$Context.ApiBaseUri
ApiVersion = [string]$Context.ApiVersion
HostName = [string]$Context.HostName
PerPage = 100
HttpVersion = [string]$Context.HttpVersion
PerPage = [int]$Context.PerPage
ClientID = [string]$Context.ClientID
InstallationID = [string]$installation.id
Permissions = [pscustomobject]$installation.permissions
Expand Down
3 changes: 3 additions & 0 deletions src/variables/private/GitHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
GitHubAppClientID = 'Iv1.f26b61bc99e69405'
OAuthAppClientID = '7204ae9b0580f2cb8288'
DefaultContext = ''
ApiVersion = '2022-11-28'
HttpVersion = '2.0'
PerPage = 100
}
Config = $null
Event = $null
Expand Down
22 changes: 18 additions & 4 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ param()
BeforeAll {
Get-SecretInfo | Remove-Secret
Get-SecretVault | Unregister-SecretVault
Import-Module -Name Context -Force -RequiredVersion 6.0.0
Get-Module -ListAvailable -Name Context | Sort-Object -Property Version | Select-Object -Last 1 | Import-Module -Force
}

Describe 'GitHub' {
Expand All @@ -23,9 +23,11 @@ Describe 'GitHub' {
$config | Should -Not -BeNullOrEmpty
}
It 'Get-GitHubConfig - Gets a configuration item by name' {
$config = Get-GitHubConfig -Name 'HostName'
Write-Verbose ($config | Format-Table | Out-String) -Verbose
$config | Should -Not -BeNullOrEmpty
$config = Get-GitHubConfig
$config.AccessTokenGracePeriodInHours | Should -Be 4
$config.HostName | Should -Be 'github.com'
$config.HttpVersion | Should -Be '2.0'
$config.PerPage | Should -Be 100
}
It 'Set-GitHubConfig - Sets a configuration item' {
Set-GitHubConfig -Name 'HostName' -Value 'msx.ghe.com'
Expand Down Expand Up @@ -66,6 +68,18 @@ Describe 'GitHub' {
Write-Verbose (Get-GitHubContext | Out-String) -Verbose
$context | Should -Not -BeNullOrEmpty
}
It 'Connect-GitHubAccount - Connects with default settings' {
$context = Get-GitHubContext
Write-Verbose ($context | Select-Object -Property * | Out-String) -Verbose
$context | Should -Not -BeNullOrEmpty
$context.ApiBaseUri | Should -Be 'https://api.github.com'
$context.ApiVersion | Should -Be '2022-11-28'
$context.AuthType | Should -Be 'IAT'
$context.HostName | Should -Be 'github.com'
$context.HttpVersion | Should -Be '2.0'
$context.TokenType | Should -Be 'ghs'
$context.Name | Should -Be 'github.com/github-actions/Organization/PSModule'
}
It 'Disconnect-GitHubAccount - Disconnects the context from the pipeline' {
$context = Get-GitHubContext
{ $context | Disconnect-GitHubAccount } | Should -Not -Throw
Expand Down