ps/Modules/Alkami.PowerShell.Choco/Private/Test-ChocoPackagesAvailablePrivate.ps1

81 lines
3.7 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Test-ChocoPackagesAvailablePrivate {
<#
.SYNOPSIS
Verifies that a list of packages and their versions are available in the Chocolatey package feed.
Returns a delimited list of packages that were not found. i.e. Alkami.Package1|1.0.0,Alkami.Package2|1.0.1
.PARAMETER Packages
Array of Package Objects to Test Availability based on configured feeds
.PARAMETER NugetCredential
Credential object for Nuget server
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[object[]]$Packages,
[Parameter(Mandatory = $true)]
[pscredential]$NugetCredential
)
$logLead = (Get-logLeadName)
Write-Host "$logLead : Verifying that all choco packages exist in the feed."
# Assign package sources to each package.
# HACK: Currently, _this_ function is ONLY called by it's non-private counterpart Test-ChocoPackagesAvailable
# which is ONLY called by TDC's Test-ChcooPackagesAvailable.ps1
# However, Set-ChocoPackageSourceFeeds is called by a LOT of other things, so it has
# a new Parameter "-MissingPackageLogLevel" that defaults to ERROR to maintain default behavior
# SRE-15611 needs this to not Write-Error for Missing Packages. That's the whole point of this
# call - to get a list of Missing Packages so that we can either AutoPromote them or
# use TeamCity's "buildProblem" outputs to fail while bubbling information up
Set-ChocoPackageSourceFeeds -Packages $packages -MissingPackageLogLevel "WARNING"
$missingPackages = @()
$success = $true
$packages | ForEach-Object {
$foundCurrentPackage = $false
$name = $_.Name
$version = $_.Version
if ($null -eq $_.Feed) {
#badnews
Write-Warning "$logLead : Package: $name $version has NULL FEED"
} else {
# This was throwing a NPE when the Package was not assigned a Feed from Set-ChocoPackageSourceFeeds
$feed = ($_.feed.Source).TrimEnd('/') # Just in case the source has a trailing '/'
try {
$authHeaders = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($NugetCredential.UserName):$(Get-PasswordFromCredential $NugetCredential)"))
}
$result = @(Invoke-WebRequest -Headers $authHeaders "$feed/Packages(Id='$name',Version='$version')" -ErrorAction SilentlyContinue)
if ($result.StatusCode -eq 200) {
Write-Host "$logLead : Package: $name $version found on Feed: $feed"
$foundCurrentPackage = $true
} else {
Write-Warning "$logLead : Package: $name $version not found on Feed: $feed"
}
} catch {
Write-Warning "$logLead : Package: $name $version not found on Feed: $feed"
}
}
if (-not $foundCurrentPackage) {
$feedString = if ($_.Feed) { $feed } else { "NULL FEED" }
Write-Warning "$logLead : Package: $name $version not found on Feed: $feedString"
$missingPackages += $_
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Justification = "Variable assignment is used further down in IF statement. False positive.")]
$success = $false
}
}
if ($success) {
Write-Host "$logLead : All Chocolatey packages are available in the feed."
} else {
Write-Host "$logLead : There are required Chocolatey package(s) that are unavailable in the Chocolatey feed!"
Write-Host $missingPackages
return (($missingPackages | ForEach-Object { "$($_.Name)|$($_.Version)" }) -join ",")
}
}