Skip to content

Commit d980561

Browse files
authored
Support for Unicode on release annotations
We have had two customers call in to open support cases because the sample script on this page was choking when customers supplied unicode characters as part of the release name or release properties json body payload. This update adds support for unicode characters to allow those annotations to get created without failures.
1 parent 8a84050 commit d980561

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

articles/azure-monitor/app/release-and-work-item-insights.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,35 @@ You can use the `CreateReleaseAnnotation` PowerShell script to create annotation
183183
[parameter(Mandatory = $true)][string]$releaseName,
184184
[parameter(Mandatory = $false)]$releaseProperties = @()
185185
)
186+
187+
# Function to ensure all Unicode characters in a JSON string are properly escaped
188+
function Convert-UnicodeToEscapeHex {
189+
param (
190+
[parameter(Mandatory = $true)][string]$JsonString
191+
)
192+
$JsonObject = ConvertFrom-Json -InputObject $JsonString
193+
foreach ($property in $JsonObject.PSObject.Properties) {
194+
$name = $property.Name
195+
$value = $property.Value
196+
if ($value -is [string]) {
197+
$value = [regex]::Unescape($value)
198+
$OutputString = ""
199+
foreach ($char in $value.ToCharArray()) {
200+
$dec = [int]$char
201+
if ($dec -gt 127) {
202+
$hex = [convert]::ToString($dec, 16)
203+
$hex = $hex.PadLeft(4, '0')
204+
$OutputString += "\u$hex"
205+
}
206+
else {
207+
$OutputString += $char
208+
}
209+
}
210+
$JsonObject.$name = $OutputString
211+
}
212+
}
213+
return ConvertTo-Json -InputObject $JsonObject -Compress
214+
}
186215
187216
$annotation = @{
188217
Id = [GUID]::NewGuid();
@@ -192,7 +221,11 @@ You can use the `CreateReleaseAnnotation` PowerShell script to create annotation
192221
Properties = ConvertTo-Json $releaseProperties -Compress
193222
}
194223
195-
$body = (ConvertTo-Json $annotation -Compress) -replace '(\\+)"', '$1$1"' -replace "`"", "`"`""
224+
$annotation = ConvertTo-Json $annotation -Compress
225+
$annotation = Convert-UnicodeToEscapeHex -JsonString $annotation
226+
227+
$body = $annotation -replace '(\\+)"', '$1$1"' -replace "`"", "`"`""
228+
196229
az rest --method put --uri "$($aiResourceId)/Annotations?api-version=2015-05-01" --body "$($body) "
197230
198231
# Use the following command for Linux Azure DevOps Hosts or other PowerShell scenarios

0 commit comments

Comments
 (0)