ps/Modules/Alkami.PowerShell.Choco/Public/Get-PackageVersionsFromAlkamiProget.ps1

226 lines
11 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-PackageVersionsFromAlkamiProget {
<#
.SYNOPSIS
This script searches for packages in the dev and qa ProGet feeds. If found, returns a JSON representation of package names and versions to the user.
.DESCRIPTION
This script searches for packages in the dev and qa ProGet feeds. Accepted input is the full path to a text file containing
one package name per line (PackageNamesFile), or a comma-delimited string of full or partial package names (PackageNames).
If the search for a given package name returns results, they are added to a hash table. After all input package names are
searched for, the hash table is converted to JSON and displayed to the user.
.PARAMETER DevUrl
An optional string that allows the user to specify a Dev proget URL to search. Defaults to Alkami's current dev feed.
.PARAMETER PackageNames
An optional string containing the names of the packages to search for. Comma-delimited for multiple packages. Partial names are
acceptable if exact matching isn't specified. Results will vary if only partial names are provided.
.PARAMETER PackageNamesFile
An optional flag expecting the full path to a file. Searches for all package names in the given file. Expected file contents is
one line per package name in the file.
.PARAMETER QaUrl
An optional string that allows the user to specify a QA proget URL to search. Defaults to Alkami's current QA feed.
.PARAMETER ExactMatch
An optional flag that searches for the exact name of the provided packages. If not passed, searches will return partial matches.
.PARAMETER SearchDevFeed
An optional flag that tells the script to search the dev feed for packages.
.PARAMETER SearchQaFeed
An optional flag that tells the script to search the qa feed for packages. If neither of the feed-specific parameters are
passed, defaults to dev.
.PARAMETER ShowPreVersions
An optional flag that tells the script to also search the dev feed for pre versions of the specified packages.
.EXAMPLE
Get-PackageVersionsFromAlkamiProget -PackageNames "Alkami.Apps.Benefits,Alkami.Apps.AugeoRewards,Alkami.MS.SSOProviders.AugeoRewards.Host" -ExactMatch -ShowPreVersions
Search just the dev feed for release and pre versions of packages by exact name
.EXAMPLE
Get-PackageVersionsFromAlkamiProget -PackageNames "overdraft,orbital,rules" -SearchDevFeed -SearchQaFeed -ShowPreVersions
Search the qa and dev feeds for release and pre versions of packages by partial names. NOTE: these results can vary. The chocolatey search does not just search on package name. Tags and other fields in ProGet can cause a package to be returned.
.EXAMPLE
Get-PackageVersionsFromAlkamiProget -PackageNamesFile "C:\Temp\testFile.txt"
Search the dev feed for release versions of packages in a given package names file. NOTE: The file format the script expects is a text file with one package name per line.
#>
Param(
[CmdletBinding()]
[Parameter(Mandatory = $false)]
[string]
$DevUrl = "https://packagerepo.orb.alkamitech.com/nuget/choco.dev",
[Parameter(Mandatory = $false)]
[string]
$PackageNames = "",
[Parameter(Mandatory = $false)]
[string]
$PackageNamesFile = "",
[Parameter(Mandatory = $false)]
[string]
$QaUrl = "https://packagerepo.orb.alkamitech.com/nuget/choco.qa",
[Parameter(Mandatory = $false)]
[Switch]
$ExactMatch,
[Parameter(Mandatory = $false)]
[Switch]
$SearchDevFeed,
[Parameter(Mandatory = $false)]
[Switch]
$SearchQaFeed,
[Parameter(Mandatory = $false)]
[Switch]
$ShowPreVersions
)
$logLead = (Get-LogLeadName)
$feedsArray = @()
$resultsHash = [ordered]@{}
# Parameter validation
if ($PSBoundParameters.ContainsKey('PackageNames')) {
if ([string]::IsNullOrEmpty($PackageNames)) {
Throw "PackageNames was provided but it is null or empty. Please specify at least one value to search for. The script expects a comma-delimited string of full or partial package names to search for."
}
if ($PSBoundParameters.ContainsKey('PackageNamesFile')) {
Throw "Both the PackageNames and PackageNamesFile parameters were passed. Please only specify one or the other."
}
Write-Host "`n$logLead : Package names supplied by user. Package names to search for are: $packageNames"
}
if (!$PSBoundParameters.ContainsKey('PackageNames')) {
if (!$PSBoundParameters.ContainsKey('PackageNamesFile')) {
Throw "Neither the PackageNames parameter nor the PackageNamesFile parameter were provided. Please use one or the other to specify packages to search for."
}
}
if ($PSBoundParameters.ContainsKey('PackageNamesFile')) {
if ([string]::IsNullOrEmpty($PackageNamesFile)) {
Throw "The PackageNamesFile parameter was provided but it is empty or null. Please specify the full filepath to a text file containing a list of packagenames, one package per line. Make sure to provide the full path to the file with file extension. i.e. 'C:\Temp\test.txt'"
}
if (Test-Path -Path $PackageNamesFile) {
Write-Host "`n$logLead : Package names file directory specified by the user. Searching for packages found in file: '$PackageNamesFile'"
} else {
Throw "Path: '$PackageNamesFile' does not exist. Make sure to provide the full path to the file with file extension. i.e. 'C:\Temp\test.txt'"
}
$fileContentsArray = Get-Content $PackageNamesFile
if (Test-IsCollectionNullOrEmpty $fileContentsArray.Length) {
Throw "A package names file was specified, but the file is empty. Please double-check the directory or file provided."
} else {
Write-Host "$logLead : Number of package names found in the file: $($fileContentsArray.Length)"
$packageNames = $fileContentsArray -join ","
}
}
if ($SearchDevFeed.IsPresent) {
Write-Host "$logLead : SearchDevFeed parameter was passed by the user. The dev feed will be searched."
$feedsArray += "dev"
}
if ($SearchQaFeed.IsPresent) {
Write-Host "$logLead : SearchQaFeed parameter was passed by the user. The qa feed will be searched."
$feedsArray += "qa"
}
if (Test-IsCollectionNullOrEmpty $feedsArray) {
Write-Host "$logLead : Neither SearchDevFeed nor SearchQaFeed were passed by the user. The dev feed will be searched by default."
$feedsArray += "dev"
}
if ($ShowPreVersions.IsPresent) {
Write-Host "$logLead : The ShowPreVersions parameter was passed. This script will also search for pre versions of the provided packages."
}
if ($ExactMatch.IsPresent) {
Write-Host "$logLead : ExactMatch parameter was passed. Searches will use exact name matching."
}
$packageNamesArray = $packageNames.Split(",");
$packageNamesArray = $packageNamesArray | Select-Object -Unique
foreach ($packageName in $packageNamesArray) {
$packageName = $packageName.Trim()
foreach ($feed in $feedsArray) {
if ($feed -eq "dev") {
Write-Host "`n$logLead : Searching feed: $feed with search filter: '$packageName'"
$temp = Get-ChocoState -s "$DevUrl" -packageName "$packageName" -exact:$ExactMatch
if ($NULL -ne $temp) {
foreach ($result in $temp) {
Write-Verbose "$logLead : Current result is: '$result'"
if ($resultsHash.Keys -contains "$($result.Name)") {
if ($resultsHash[$($result.Name)].Keys -contains "DevRelease") {
Write-Verbose "$logLead : Results already contain a DevRelease entry for package '$($result.Name)'. Skipping add."
} else {
$resultsHash[$result.Name] += @{"DevRelease" = $result.Version }
}
} else {
$resultsHash[$result.Name] += @{"DevRelease" = $result.Version }
}
}
}
if ($ShowPreVersions) {
Write-Host "`n$logLead : Searching feed: $feed for pre versions with search filter: '$packageName'"
$temp = Get-ChocoState -s "$DevUrl" -packageName "$packageName" -exact:$ExactMatch -pre
if ($NULL -ne $temp) {
foreach ($result in $temp) {
Write-Verbose "$logLead : Current result is: '$result'"
if ($result.Version -like "*pre*") {
if ($resultsHash.Keys -contains "$($result.Name)") {
if ($resultsHash[$($result.Name)].Keys -contains "DevPre") {
Write-Verbose "$logLead : Results already contain a DevPre entry for package '$($result.Name)'. Skipping add."
} else {
$resultsHash[$result.Name] += @{"DevPre" = $result.Version }
}
} else {
$resultsHash[$result.Name] += @{"DevPre" = $result.Version }
}
}
}
}
}
}
if ($feed -eq "qa") {
Write-Host "`n$logLead : Searching feed: $feed with search filter: '$packageName'"
$temp = Get-ChocoState -s "$QaUrl" -packageName "$packageName" -exact:$ExactMatch
if ($NULL -ne $temp) {
foreach ($result in $temp) {
Write-Verbose "$logLead : Current result is: '$result'"
if ($resultsHash.Keys -contains "$($result.Name)") {
if ($resultsHash[$($result.Name)].Keys -contains "QaRelease") {
Write-Verbose "$logLead : Results already contain a QaRelease entry for package '$($result.Name)'. Skipping add."
} else {
$resultsHash[$result.Name] += @{"QaRelease" = $result.Version }
}
} else {
$resultsHash[$result.Name] += @{"QaRelease" = $result.Version }
}
}
}
}
}
}
Write-Host ""
if ($resultsHash.Keys.Count -eq 0) {
Write-Warning "$logLead : No results were found given the following:`nDevUrl: '$DevUrl'`nQaUrl: '$QaUrl'`n`nPackages names searched for:`n$packageNamesArray"
}
$resultsJson = $resultsHash | ConvertTo-Json
return $resultsJson
}