Skip to content

Added TLS 1.3 support and fixed strict-mode variable errors in tests #444

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions GitHubCore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}

Set-Variable -Scope Script -Option ReadOnly -Name ValidBodyContainingRequestMethods -Value ('Post', 'Patch', 'Put', 'Delete')
Set-Variable -Scope Script -Option ReadOnly -Name PreferredSecurityProtocolType -Value (Get-PreferredSecurityProtocolType)

function Invoke-GHRestMethod
{
Expand Down Expand Up @@ -275,6 +276,7 @@ function Invoke-GHRestMethod
}

$originalSecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol = $PreferredSecurityProtocolType

# When $Save is in use, we need to remember what file we're saving the result to.
$outFile = [String]::Empty
Expand Down Expand Up @@ -315,8 +317,6 @@ function Invoke-GHRestMethod
# Disable Progress Bar in function scope during Invoke-WebRequest
$ProgressPreference = 'SilentlyContinue'

[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12

$result = Invoke-WebRequest @params

if ($Method -eq 'Delete')
Expand Down
32 changes: 32 additions & 0 deletions Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,35 @@ function New-ErrorRecord

return $errorRecord
}

filter Get-PreferredSecurityProtocolType
{
<#
.SYNOPSIS
Decides the security protocol to use when making a REST API call

.DESCRIPTION
Decides to use TLS 1.3 if the user is running on PowerShell version 7.0 or higher, otherwise
uses TLS 1.2 if the user is running on Windows PowerShell.

.NOTES
See https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#minimum-version
TLS Best Practices https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls
#>
[CmdletBinding()]
[OutputType([Net.SecurityProtocolType])]
param()
$dotNet47 = 460798
if (($PSVersionTable.PSVersion -ge [version]'7.0.0') -or (
(Get-ItemPropertyValue -LiteralPath 'HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Release) -ge $dotNet47)
)
{
# .NET 4.7 (460798) is the first .NET version to respect the system default and supports TLS 1.3 out-of-box
return [Net.SecurityProtocolType]::SystemDefault
}
elseif (([Net.SecurityProtocolType] | Get-Member -MemberType Property -Static).Name -contains 'Tls13')
{
return [Net.SecurityProtocolType]::Tls13
}
else { return [Net.SecurityProtocolType]::Tls12 }
}
6 changes: 3 additions & 3 deletions Tests/GitHubBranches.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Describe 'GitHubBranches\Get-GitHubRepositoryBranchProtectionRule' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -723,7 +723,7 @@ Describe 'GitHubBranches\New-GitHubRepositoryBranchProtectionRule' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -785,7 +785,7 @@ Describe 'GitHubBranches\Remove-GitHubRepositoryBranchProtectionRule' {

}
AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/GitHubCodespaces.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Describe 'GitHubCodespaces\New-GitHubCodespace' {
}

AfterAll {
if ($codespace)
if (Get-Variable -Name codespace -ErrorAction SilentlyContinue)
{
Remove-GitHubCodespace -CodespaceName $codespace.name -Confirm:$false
}
Expand Down Expand Up @@ -249,7 +249,7 @@ Describe 'GitHubCodespaces\New-GitHubCodespace' {
}

AfterAll {
if ($codespace)
if (Get-Variable -Name codespace -ErrorAction SilentlyContinue)
{
Remove-GitHubCodespace -CodespaceName $codespace.name -Confirm:$false
}
Expand Down Expand Up @@ -289,7 +289,7 @@ Describe 'GitHubCodespaces\New-GitHubCodespace' {
}

AfterAll {
if ($codespace)
if (Get-Variable -Name codespace -ErrorAction SilentlyContinue)
{
Remove-GitHubCodespace -CodespaceName $codespace.name -Confirm:$false
}
Expand Down Expand Up @@ -329,7 +329,7 @@ Describe 'GitHubCodespaces\New-GitHubCodespace' {
}

AfterAll {
if ($codespace)
if (Get-Variable -Name codespace -ErrorAction SilentlyContinue)
{
Remove-GitHubCodespace -CodespaceName $codespace.name -Confirm:$false
}
Expand Down Expand Up @@ -357,7 +357,7 @@ Describe 'GitHubCodespaces\New-GitHubCodespace' {
}

AfterAll {
if ($codespace)
if (Get-Variable -Name codespace -ErrorAction SilentlyContinue)
{
Remove-GitHubCodespace -CodespaceName $codespace.name -Confirm:$false
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/GitHubDeployments.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,17 @@ Describe 'GitHubDeployments\Remove-GitHubDeploymentEnvironment' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
$repo | Remove-GitHubRepository -Confirm:$false
}

if ($reviewerTeam1)
if (Get-Variable -Name reviewerTeam1 -ErrorAction SilentlyContinue)
{
$reviewerTeam1 | Remove-GitHubTeam -Confirm:$false
}

if ($reviewerTeam2)
if (Get-Variable -Name reviewerTeam2 -ErrorAction SilentlyContinue)
{
$reviewerTeam2 | Remove-GitHubTeam -Confirm:$false
}
Expand Down
28 changes: 14 additions & 14 deletions Tests/GitHubRepositories.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -97,7 +97,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -162,7 +162,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -195,7 +195,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -227,7 +227,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -259,7 +259,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -291,7 +291,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -323,7 +323,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -355,7 +355,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -387,7 +387,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -419,7 +419,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -477,7 +477,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -515,7 +515,7 @@ Describe 'GitHubRepositories\New-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down Expand Up @@ -1310,7 +1310,7 @@ Describe 'GitHubRepositories\Set-GitHubRepository' {
}

AfterAll -ScriptBlock {
if ($repo)
if (Get-Variable -Name repo -ErrorAction SilentlyContinue)
{
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
Expand Down
5 changes: 4 additions & 1 deletion Tests/GitHubTeams.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,10 @@ Describe 'GitHubTeams\Get-GitHubTeamMember' {
}

AfterAll {
$team | Remove-GitHubTeam -Force
if (Get-Variable -Name team -ErrorAction SilentlyContinue)
{
$team | Remove-GitHubTeam -Force
}
}

Context 'Getting team members using TeamName' {
Expand Down