Skip to content

Commit

Permalink
Update Get-GitHubRepoTag.ps1
Browse files Browse the repository at this point in the history
Working version #544
  • Loading branch information
aaronparker committed Sep 25, 2024
1 parent e253988 commit 507d118
Showing 1 changed file with 12 additions and 107 deletions.
119 changes: 12 additions & 107 deletions Evergreen/Shared/Get-GitHubRepoTag.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
function Get-GitHubRepoTag {
<#
.SYNOPSIS
Calls the GitHub Tags API passed via $Uri, validates the response and returns a formatted object
Example: https://api.github.com/repos/PowerShell/PowerShell/releases/latest
TODO: support Basic or OAuth authentication to GitHub
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)]
Expand All @@ -15,28 +13,10 @@ function Get-GitHubRepoTag {
$true
}
else {
throw "'$_' must be in the format 'https://api.github.com/repos/user/repository/tags'. Replace 'user' with the user or organisation and 'repository' with the target repository name."
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}).*",

[Parameter(Mandatory = $false, Position = 2)]
[ValidateNotNullOrEmpty()]
[System.String] $VersionTag = "tag_name",

[Parameter(Mandatory = $false, Position = 3)]
[ValidateNotNullOrEmpty()]
[System.String] $Filter = "\.exe$|\.msi$|\.msp$|\.zip$",

[Parameter(Mandatory = $false, Position = 4)]
[System.Array] $VersionReplace,

[Parameter()]
[System.Management.Automation.SwitchParameter] $ReturnVersionOnly
[System.String] $Uri
)

begin {
Expand Down Expand Up @@ -96,8 +76,8 @@ function Get-GitHubRepoTag {
throw $_
}

if ($null -eq $script:resourceStrings.Properties.GitHub) {
Write-Warning -Message "$($MyInvocation.MyCommand): Unable to validate tag against GitHub releases property object because we can't find the module resource."
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
Expand All @@ -115,7 +95,7 @@ function Get-GitHubRepoTag {

# Throw an error for missing properties
if ($null -ne $missingProperties) {
Write-Verbose -Message "$($MyInvocation.MyCommand): Validated successfully."
Write-Verbose -Message "$($MyInvocation.MyCommand): Validated tag object successfully."
}
else {
Write-Verbose -Message "$($MyInvocation.MyCommand): Validation failed."
Expand All @@ -128,87 +108,12 @@ function Get-GitHubRepoTag {

# Build and array of the latest release and download URLs
Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($tags.count) tags."
Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($tags.assets.count) asset/s."

if ($PSBoundParameters.ContainsKey("ReturnVersionOnly")) {
if ($Uri -match "^*tags$") {
try {
# Uri matches tags fo the repo; find the latest tag
$Version = [RegEx]::Match($tags[0].name, $MatchVersion).Captures.Groups[1].Value
}
catch {
Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number as-is, returning: $($tags[0].$VersionTag)."
$Version = $tags[0].name
}
}
else {
try {
# Uri matches releases for the repo; return just the version string
$Version = [RegEx]::Match($tags[0].$VersionTag, $MatchVersion).Captures.Groups[1].Value
}
catch {
Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number as-is, returning: $($tags[0].$VersionTag)."
$Version = $tag.$VersionTag
}
}

if ($PSBoundParameters.ContainsKey("VersionReplace")) {
# Replace string in version
$Version = $Version -replace $res.Get.VersionReplace[0], $res.Get.VersionReplace[1]
}

# Build the output object
$PSObject = [PSCustomObject] @{
Version = $Version
}
Write-Output -InputObject $PSObject
}
else {
foreach ($tag in $tags) {
foreach ($asset in $tag.assets) {

# Filter downloads by matching the RegEx in the manifest. The the RegEx may perform includes and excludes
Write-Verbose -Message "$($MyInvocation.MyCommand): Match $Filter to $($asset.browser_download_url)."
if ($asset.browser_download_url -match $Filter) {
Write-Verbose -Message "$($MyInvocation.MyCommand): Building Windows release output object with: $($asset.browser_download_url)."

# Capture the version string from the specified release tag
try {
Write-Verbose -Message "$($MyInvocation.MyCommand): Matching version number against: $($tag.$VersionTag)."
$Version = [RegEx]::Match($tag.$VersionTag, $MatchVersion).Captures.Groups[1].Value
Write-Verbose -Message "$($MyInvocation.MyCommand): Found version number: $Version."
}
catch {
Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number, returning: $($tag.$VersionTag)."
$Version = $tag.$VersionTag
}

if ($PSBoundParameters.ContainsKey("VersionReplace")) {
# Replace string in version
Write-Verbose -Message "$($MyInvocation.MyCommand): Replace $($res.Get.VersionReplace[0])."
$Version = $Version -replace $res.Get.VersionReplace[0], $res.Get.VersionReplace[1]
}

# Build the output object
if ((Get-Platform -String $asset.browser_download_url) -eq "Windows") {
$PSObject = [PSCustomObject] @{
Version = $Version
Date = ConvertTo-DateTime -DateTime $tag.created_at -Pattern "MM/dd/yyyy HH:mm:ss"
Size = $asset.size
#Platform = Get-Platform -String $asset.browser_download_url
Architecture = Get-Architecture -String $(Split-Path -Path $asset.browser_download_url -Leaf)
InstallerType = Get-InstallerType -String $asset.browser_download_url
Type = [System.IO.Path]::GetExtension($asset.browser_download_url).Split(".")[-1]
URI = $asset.browser_download_url
}
Write-Output -InputObject $PSObject
}
}
else {
Write-Verbose -Message "$($MyInvocation.MyCommand): Skip: $($asset.browser_download_url)."
}
}
}
# Output the tags object
foreach ($tag in $tags) {
[PSCustomObject]@{
Tag = $tag.name
} | Write-Output
}
}
}
Expand Down

0 comments on commit 507d118

Please sign in to comment.