ps/Modules/Cole.PowerShell.Developer/Public/Write-TeamCityBuildProblem.ps1

53 lines
2.0 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Write-TeamCityBuildProblem {
<#
.SYNOPSIS
Writes a build problem. This will halt the build by the end of the build-step. Some build-steps or dependencies may continue depending on their configuration.
.PARAMETER Description
The message to use in the UI to display that an error occurred
.PARAMETER Identity
The identity for the error message.
This must be no longer than 60 characters.
This must be a valid Java ID.
This command will trim to 60 characters with a warning if the length is too long.
This may cause you issues if you do not take care to maintain this length.
This will replace spaces and periods in the identity with underscores so it is a proper java identifier, and strip other non-alphanumeric characters.
.EXAMPLE
Write-TeamCityBuildProblem -Description "This will halt [the build]" -Identity "hahano"
#>
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $true)]
[string]$Description,
[Parameter(Mandatory = $true)]
[string]$Identity
)
$logLead = Get-LogLeadName
# Strip bad things
# Keep all alphanumerics => A-Za-z0-9 are range operators
# Underscores are valid Java ID characters
# Keep spaces and periods to convert them to underscores
$Identity = $Identity -replace '[^a-zA-Z0-9 _.]',''
$Identity = $Identity -replace '[^a-zA-Z0-9]','_'
# Trim the length
if ($Identity.Length -gt 60) {
Write-Warning "$logLead : Input string (once sanitized) is still longer than 60 characters length [$($Identity.Length)] value [$Identity], trimming to 60"
$Identity = $Identity.Substring(0,60)
}
$sanitizedDescription = ConvertTo-SafeTeamCityMessage -InputText $Description
if (Test-IsTeamCityProcess) {
# No need to sanitize the identity, we just stripped it to underscores and alphanumerics
Write-Host "##teamcity[buildProblem description='$sanitizedDescription' identity='$Identity']"
} else {
Write-Error "$logLead : $Identity : $sanitizedDescription"
}
}