ps/Modules/Alkami.DevOps.TeamCity/Public/Get-GitCurrentRelease.ps1

80 lines
2.9 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
Function Get-GitCurrentRelease {
<#
.SYNOPSIS
Get the current latest release version of Git for Windows
.PARAMETER Uri
Uri of the API endpoint for getting the latest release info
.PARAMETER DownloadInstaller
Whether to download the 64bit windows installer
.PARAMETER OutFile
The file to save the downloaded file as. If a full path is not provided, it will be saved in the current working directory.
.LINK
https://jdhitsolutions.com/blog/powershell/5633/get-git-with-powershell/
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory = $false, ParameterSetName = "Info")]
[Parameter(Mandatory = $false, ParameterSetName = "Download")]
[ValidateNotNullorEmpty()]
[string]$Uri = "https://api.github.com/repos/git-for-windows/git/releases/latest",
[Parameter(Mandatory = $false, ParameterSetName = "Download")]
[switch]$DownloadInstaller,
[Parameter(Mandatory = $true, ParameterSetName = "Download")]
[ValidateNotNullOrEmpty()]
[string]$OutFile
)
$logLead = Get-LogLeadName
Write-Host "$loglead : Getting current release information from $Uri"
try {
$gitForWindows = Invoke-RestMethod -Uri $Uri -Method Get
} catch {
$caughtEx = $_
Write-Warning "$logLead : Unable to get version data"
Write-Warning "$logLead : $($caughtEx.Exception.Message)"
}
if ($gitForWindows.tag_name) {
$gitForWindows64Bit = $gitForWindows.Assets.Where({ $_.Name -match "64-bit.exe" })
$downloadUrl64Bit = $gitForWindows64Bit.browser_download_url
[pscustomobject]$gitVersionData = @{
Name = $gitForWindows.name
Version = $gitForWindows.tag_name
Released = $($gitForWindows.published_at -as [datetime])
DownloadUrl64Bit = $downloadUrl64Bit
}
if ($PSCmdlet.ParameterSetName -eq "Download") {
$outfileDir = Split-Path -Path $OutFile -Parent
if ([string]::IsNullOrWhiteSpace($outfileDir)) {
Write-Warning "$logLead : NO PATH PROVIDED FOR DOWNLOAD!"
Write-Warning "$logLead : Downloading to Current Working Directory"
$currentWorkingDir = Get-Location
Write-Warning "$logLead : $currentWorkingDir"
}
try {
Write-Host "$logLead : Downloading git for windows`n`tfrom $downloadUrl64Bit`n`tto $Outfile"
Invoke-WebRequest -Uri $downloadUrl64Bit -Method GET -OutFile $OutFile
} catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
Write-Warning "$logLead : ERROR DOWNLOADING FILE!"
Write-Warning "$logLead : HTTP Status $StatusCode"
}
}
return $gitVersionData
} else {
Write-Host "$logLead : Not found"
return $null
}
}