Skip to content

Commit

Permalink
Merge pull request #746 from aaronparker/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
aaronparker authored Sep 26, 2024
2 parents 4b124db + 4aecebd commit b316e46
Show file tree
Hide file tree
Showing 24 changed files with 459 additions and 74 deletions.
27 changes: 10 additions & 17 deletions Evergreen/Apps/Get-AWSCLI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ function Get-AWSCLI {
$res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1])
)

# Get latest version and download latest release via GitHub API
$params = @{
Uri = $res.Get.Update.Uri
ContentType = $res.Get.Update.ContentType
ReturnObject = "Content"
}
# Get the latest version of AWS CLI via the latest tag on the repository
$Tags = Get-GitHubRepoTag -Uri $res.Get.Update.Uri

# Get only latest version tag from GitHub API
$Content = ((Invoke-EvergreenWebRequest @params | ConvertFrom-Json).name | ForEach-Object { New-Object -TypeName "System.Version" ($_) } | Sort-Object -Descending | Select-Object -First 1 | ForEach-Object {("{0}.{1}.{2}" -f $_.Major,$_.Minor,$_.Build)})
# Select the latest version
$Version = $Tags | Sort-Object -Property @{ Expression = { [System.Version]$_.Tag }; Descending = $true } | Select-Object -First 1

if ($null -ne $Content) {
$Content | ForEach-Object {
$PSObject = [PSCustomObject] @{
Version = $_
Type = "msi"
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $_
}
Write-Output -InputObject $PSObject
}
# Output the version and download URL
$PSObject = [PSCustomObject] @{
Version = $Version.Tag
Type = "msi"
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $Version.Tag
}
Write-Output -InputObject $PSObject
}
36 changes: 14 additions & 22 deletions Evergreen/Apps/Get-Npcap.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Get-Npcap {
function Get-Npcap {
<#
.SYNOPSIS
Returns the latest Npcap version number and download.
Expand All @@ -8,34 +8,26 @@ Function Get-Npcap {
E-mail: [email protected]
#>
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding(SupportsShouldProcess = $False)]
[CmdletBinding(SupportsShouldProcess = $false)]
param (
[Parameter(Mandatory = $False, Position = 0)]
[Parameter(Mandatory = $false, Position = 0)]
[ValidateNotNull()]
[System.Management.Automation.PSObject]
$res = (Get-FunctionResource -AppName ("$($MyInvocation.MyCommand)".Split("-"))[1])
)

# Get the latest version via the latest tag on the repository
$Tags = Get-GitHubRepoTag -Uri $res.Get.Update.Uri

# Get latest version and download latest release via GitHub API
$params = @{
Uri = $res.Get.Update.Uri
ContentType = $res.Get.Update.ContentType
ReturnObject = "Content"
}

# Get only latest version tag from GitHub API
$Content = ((Invoke-EvergreenWebRequest @params | ConvertFrom-Json).name -replace "v",""| ForEach-Object { New-Object -TypeName "System.Version" ($_) } | Sort-Object -Descending | Select-Object -First 1 | ForEach-Object {("{0}.{1}" -f $_.Major,$_.Minor)})
# Select the latest version
$Version = $Tags | Sort-Object -Property @{ Expression = { [System.Version]$_.Tag }; Descending = $true } | Select-Object -First 1

if ($null -ne $Content) {
$Content | ForEach-Object {
$PSObject = [PSCustomObject] @{
Version = $_
Type = "exe"
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $_
}
Write-Output -InputObject $PSObject
}
# Output the version and download URL
$PSObject = [PSCustomObject] @{
Version = $Version.Tag
Type = Get-FileType -File $res.Get.Download.Uri
URI = $res.Get.Download.Uri -replace $res.Get.Download.ReplaceText, $Version.Tag
}
}
Write-Output -InputObject $PSObject

}
7 changes: 7 additions & 0 deletions Evergreen/Evergreen.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
"upload_url",
"url",
"zipball_url"
],
"GitHubTags": [
"name",
"commit",
"zipball_url",
"tarball_url",
"node_id"
]
},
"Uri": {
Expand Down
12 changes: 6 additions & 6 deletions Evergreen/Evergreen.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ PowerShellVersion = '3.0'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Export-EvergreenApp', 'Export-EvergreenManifest',
'Find-EvergreenApp', 'Get-EvergreenApp', 'Get-EvergreenAppFromApi',
'Get-EvergreenAppFromLibrary', 'Get-EvergreenEndpointFromApi',
'Get-EvergreenLibrary', 'New-EvergreenLibrary', 'Save-EvergreenApp',
FunctionsToExport = @('Export-EvergreenApp', 'Export-EvergreenManifest',
'Find-EvergreenApp', 'Get-EvergreenApp', 'Get-EvergreenAppFromApi', 'ConvertTo-DotNetVersionClass',
'Get-EvergreenAppFromLibrary', 'Get-EvergreenEndpointFromApi',
'Get-EvergreenLibrary', 'New-EvergreenLibrary', 'Save-EvergreenApp',
'Start-EvergreenLibraryUpdate', 'Test-EvergreenApp')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand All @@ -82,8 +82,8 @@ CmdletsToExport = @()
# VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = 'sea', 'gea', 'fea', 'tea', 'iea', 'Invoke-EvergreenLibraryUpdate',
'Get-EvergreenLibraryApp', 'Invoke-EvergreenApp',
AliasesToExport = 'sea', 'gea', 'fea', 'tea', 'iea', 'Invoke-EvergreenLibraryUpdate',
'Get-EvergreenLibraryApp', 'Invoke-EvergreenApp',
'Get-EvergreenEndpoint'

# DSC resources to export from this module
Expand Down
3 changes: 2 additions & 1 deletion Evergreen/Manifests/Npcap.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"Get": {
"Update": {
"Uri": "https://api.github.com/repos/nmap/npcap/tags",
"ContentType": "application/json; charset=utf-8"
"ContentType": "application/json; charset=utf-8",
"MatchVersion": "(\\d+(\\.\\d+){1,4}).*"
},
"Download": {
"Uri": "https://npcap.com/dist/npcap-#version.exe",
Expand Down
16 changes: 16 additions & 0 deletions Evergreen/Private/Convert-Segment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Convert-Segment {
param (
[System.String] $Segment
)

if ($Segment -match '^\d+$') {
return [System.Int32]$Segment
}
else {
$Normalized = 0
foreach ($Char in $Segment.ToCharArray()) {
$Normalized = $Normalized * 100 + [System.Int32][System.Char]$Char
}
return $Normalized
}
}
44 changes: 44 additions & 0 deletions Evergreen/Public/ConvertTo-DotNetVersionClass.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function ConvertTo-DotNetVersionClass {
<#
.EXTERNALHELP Evergreen-help.xml
#>
param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = "A version string to convert to a standard .NET compliant version class.")]
[System.String] $Version
)

process {
# Split the version string into segments and initialise an array
$Segments = $Version -split '[.\-_+]'
$NormalizedSegments = @()

# Normalize each segment
foreach ($Segment in $Segments) {
$NormalizedSegments += @(Convert-Segment -Segment $Segment)
}

# If the number of segments is greater than 4, sum the last segments
if ($NormalizedSegments.Count -gt 4) {
$NormalizedSegments = $NormalizedSegments[0..2] + ($NormalizedSegments[3..($NormalizedSegments.Count - 1)] | Measure-Object -Sum).Sum
}

# If the number of segments is less than 4, pad with zeros
while ($NormalizedSegments.Count -lt 4) {
$NormalizedSegments += 0
}

# Return the version as a .NET Version class
try {
return [System.Version]($NormalizedSegments -join ".")
}
catch {
Write-Warning -Message "Failed to convert version string '$Version' to a .NET Version class."
return ($NormalizedSegments -join ".")
}
}
}
4 changes: 0 additions & 4 deletions Evergreen/Shared/Get-GitHubRepoRelease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ function Get-GitHubRepoRelease {
}
Write-Output -InputObject $PSObject
}
# Write-Verbose -Message "$($MyInvocation.MyCommand): Matching platform 'Windows' against: $($PSObject.Platform)."
# if ($PSObject.Platform -eq "Windows") {
# Write-Output -InputObject $PSObject
# }
}
else {
Write-Verbose -Message "$($MyInvocation.MyCommand): Skip: $($asset.browser_download_url)."
Expand Down
133 changes: 133 additions & 0 deletions Evergreen/Shared/Get-GitHubRepoTag.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
function Get-GitHubRepoTag {
<#
.SYNOPSIS
Calls the GitHub Tags API passed via $Uri and returns the tags for the repository
Example: https://api.github.com/repos/PowerShell/PowerShell/tags
#>
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding(SupportsShouldProcess = $false)]
param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript( {
if ($_ -match "^(https://api\.github\.com/repos/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/tags)") {
$true
}
else {
throw "'$_' must be in the format 'https://api.github.com/repos/user/tags'. Replace 'user' with the user or organisation and 'repository' with the target repository name."
}
})]
[System.String] $Uri,

[Parameter(Mandatory = $false, Position = 1)]
[ValidateNotNullOrEmpty()]
[System.String] $MatchVersion = "(\d+(\.\d+){1,4}).*"
)

begin {
$RateLimit = Get-GitHubRateLimit
}

process {
if ($RateLimit.remaining -eq 0) {
# We're rate limited, so output a special object
[PSCustomObject] @{
Version = "RateLimited"
URI = "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting"
} | Write-Output
}
else {
try {
# Retrieve the releases from the GitHub API
# Use TLS for connections
Write-Verbose -Message "$($MyInvocation.MyCommand): Set TLS to 1.2."
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072

# Invoke the GitHub releases REST API
# Note that the API performs rate limiting.
# https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#get-the-latest-release
$params = @{
ContentType = "application/vnd.github.v3+json"
ErrorAction = "Stop"
MaximumRedirection = 0
DisableKeepAlive = $true
UseBasicParsing = $true
UserAgent = "github-aaronparker-evergreen"
Uri = $Uri
}
if (Test-ProxyEnv) {
$params.Proxy = $script:EvergreenProxy
}
if (Test-ProxyEnv -Creds) {
$params.ProxyCredential = $script:EvergreenProxyCreds
}
# If GITHUB_TOKEN or GH_TOKEN exists, let's add that to the API request
if (Test-Path -Path "env:GITHUB_TOKEN") {
$params.Headers = @{ Authorization = "token $env:GITHUB_TOKEN" }
}
elseif (Test-Path -Path "env:GH_TOKEN") {
$params.Headers = @{ Authorization = "token $env:GH_TOKEN" }
}

# Output the parameters when using -Verbose
foreach ($tag in $params.GetEnumerator()) {
Write-Verbose -Message "$($MyInvocation.MyCommand): Invoke-WebRequest parameter: $($tag.name): $($tag.value)."
}

Write-Verbose -Message "$($MyInvocation.MyCommand): Get GitHub release from: $Uri."
$Tags = Invoke-RestMethod @params
}
catch {
throw $_
}

if ($null -eq $script:resourceStrings.Properties.GitHubTags) {
Write-Warning -Message "$($MyInvocation.MyCommand): Unable to validate tag against GitHub tags property object because we can't find the module resource."
}
else {
# Validate that $tags has the expected properties
Write-Verbose -Message "$($MyInvocation.MyCommand): Validating GitHub tag object."
foreach ($tag in $Tags) {

# Compare the GitHub release object with properties that we expect
$params = @{
ReferenceObject = $script:resourceStrings.Properties.GitHubTags
DifferenceObject = (Get-Member -InputObject $tag -MemberType NoteProperty)
PassThru = $true
ErrorAction = "Continue"
}
$missingProperties = Compare-Object @params

# Throw an error for missing properties
if ($null -ne $missingProperties) {
Write-Verbose -Message "$($MyInvocation.MyCommand): Validated tag object successfully."
}
else {
Write-Verbose -Message "$($MyInvocation.MyCommand): Validation failed."
$missingProperties | ForEach-Object {
throw [System.Management.Automation.ValidationMetadataException]::New("$($MyInvocation.MyCommand): Property: '$_' missing")
}
}
}
}

# Build and array of the latest release and download URLs
Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($tags.count) tags."
foreach ($Tag in $Tags) {

try {
# Uri matches tags for the repo; find the latest tag
$Version = [RegEx]::Match($Tag.name, $MatchVersion).Captures.Groups[1].Value
}
catch {
Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number, returning as-is: $($Tag.name)."
$Version = $Tag.name
}

# Output the tags object
[PSCustomObject]@{
Tag = $Version
} | Write-Output
}
}
}
}
Loading

0 comments on commit b316e46

Please sign in to comment.