Skip to content

Commit 8333d92

Browse files
author
EwanNoble
committed
Add fix for special characters when converting to JSON in old powershell
1 parent eea60fe commit 8333d92

File tree

9 files changed

+71
-38
lines changed

9 files changed

+71
-38
lines changed

tasks/EnvironmentConfiguration/task/Invoke-Task.ps1

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ try {
66
Import-Module -Name $PSScriptRoot/InitializationHelpers.psm1 -Force
77
Initialize-TaskDependencies
88

9+
$AzAccountsModule = @(Get-Module Az.Accounts -ListAvailable)[0]
10+
$AzureRmProfileModule = @(Get-Module AzureRm.Profile -ListAvailable)[0]
11+
if ($AzAccountsModule) {
12+
Enable-AzureRmAlias -Scope Process
13+
$Global:IsAz = $true
14+
Write-Host "Using Az Module"
15+
}
16+
elseif ($AzureRmProfileModule) {
17+
$Global:IsAzureRm = $true
18+
Write-Host "Using AzureRm Module"
19+
}
20+
else {
21+
throw "No Azure powershell module found"
22+
}
23+
924
if ($ENV:TF_BUILD) {
1025

1126
# --- Inputs
@@ -25,20 +40,14 @@ try {
2540
# --- Init
2641
$Endpoint = Get-VstsEndpoint -Name $ServiceEndpointName -Require
2742

28-
$AzAccountsModule = @(Get-Module Az.Accounts -ListAvailable)[0]
29-
$AzureRmProfileModule = @(Get-Module AzureRm.Profile -ListAvailable)[0]
30-
31-
if ($AzAccountsModule) {
43+
if ($Global:IsAz) {
3244
Initialize-AzModule -Endpoint $Endpoint
33-
Enable-AzureRmAlias -Scope Process
34-
$Global:IsAz = $true
3545
}
36-
elseif ($AzureRmProfileModule) {
46+
elseif ($Global:IsAzureRm) {
3747
Initialize-AzureRMModule -Endpoint $Endpoint
38-
$Global:IsAzureRm = $true
3948
}
4049
else {
41-
throw "No Azure powershell module found"
50+
throw "Couldn't find Global Azure module setting $($MyInvocation.ScriptLineNumber) $($MyInvocation.ScriptName)"
4251
}
4352
}
4453

tasks/EnvironmentConfiguration/task/ps_modules/Handlers/private/configuration/Build-ConfigurationEntity.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function Build-ConfigurationEntity {
2121
$Schema = Expand-Schema -PropertyObject $SchemaObject.Properties
2222
$Configuration = ($Schema | ConvertTo-Json -Depth 10 -Compress)
2323

24+
if ($PSVersionTable.PSVersion.Major -lt 6) {
25+
$Configuration = [Regex]::Replace($Configuration,
26+
"\\u(?<Value>[a-zA-Z0-9]{4})", {
27+
param($m) ([char]([int]::Parse($m.Groups['Value'].Value,
28+
[System.Globalization.NumberStyles]::HexNumber))).ToString() } )
29+
}
30+
2431
Write-Output $Configuration
2532
}
2633
catch {

tasks/EnvironmentConfiguration/task/ps_modules/Handlers/private/storage/Get-TableEntity.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ function Get-TableEntity {
1919
}
2020
elseif ($Global:IsAzureRm) {
2121
$TableOperation = [Microsoft.WindowsAzure.Storage.Table.TableOperation]::Retrieve($PartitionKey, $RowKey)
22-
22+
}
23+
else {
24+
throw "Couldn't find Global Azure module setting $($MyInvocation.ScriptLineNumber) $($MyInvocation.ScriptName)"
2325
}
2426
$Entity = $StorageTable.CloudTable.Execute($TableOperation, $null, $null)
2527

tasks/EnvironmentConfiguration/task/ps_modules/Handlers/private/storage/New-TableEntity.ps1

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ function New-TableEntity {
1414

1515
if ($Global:IsAz) {
1616
$Entity = [Microsoft.Azure.Cosmos.Table.DynamicTableEntity]::new($PartitionKey, $RowKey)
17-
}
18-
elseif ($Global:IsAzureRm) {
19-
$Entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $PartitionKey, $RowKey
20-
}
21-
22-
$Entity.Properties.Add("Data", $Configuration)
23-
if ($Global:IsAz) {
17+
$Entity.Properties.Add("Data", $Configuration)
2418
$null = $StorageTable.CloudTable.Execute([Microsoft.Azure.Cosmos.Table.TableOperation]::Insert($Entity))
2519
}
2620
elseif ($Global:IsAzureRm) {
21+
$Entity = [Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity]::new($PartitionKey, $RowKey)
22+
$Entity.Properties.Add("Data", $Configuration)
2723
$null = $StorageTable.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($Entity))
2824
}
29-
25+
else {
26+
throw "Couldn't find Global Azure module setting $($MyInvocation.ScriptLineNumber) $($MyInvocation.ScriptName)"
27+
}
3028
}
3129
catch {
3230
Write-Error -Message "$_" -ErrorAction Stop

tasks/EnvironmentConfiguration/task/ps_modules/Handlers/private/storage/Set-TableEntity.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function Set-TableEntity {
1616
elseif ($Global:IsAzureRm) {
1717
$null = $StorageTable.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrReplace($Entity))
1818
}
19+
else {
20+
throw "Couldn't find Global Azure module setting $($MyInvocation.ScriptLineNumber) $($MyInvocation.ScriptName)"
21+
}
1922

2023
}
2124
catch {
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
$ENV:SYSTEM_CULTURE = "en_US"
2-
$VerbosePreference = "Continue"
3-
$Debugpreference = "Continue"
1+
$TestRunner = Start-Job -ScriptBlock {
2+
$ENV:SYSTEM_CULTURE = "en_US"
3+
$VerbosePreference = "Continue"
4+
$Debugpreference = "Continue"
45

5-
Import-Module -Name $PSScriptRoot/../task/ps_modules/VstsTaskSdk -Force
6-
Import-Module -Name $PSScriptRoot/modules/UnitTest.Helpers.psm1 -Force
7-
Import-Module -Name $PSScriptRoot/../task/InitializationHelpers.psm1 -Force
6+
Import-Module -Name $using:PSScriptRoot/../task/ps_modules/VstsTaskSdk -Force
7+
Import-Module -Name $using:PSScriptRoot/modules/UnitTest.Helpers.psm1 -Force
8+
Import-Module -Name $using:PSScriptRoot/../task/InitializationHelpers.psm1 -Force
89

9-
try {
10-
Set-MockEnvironment
10+
try {
11+
Set-MockEnvironment
1112

12-
$SourcePath = "$PSScriptRoot/resource"
13-
$TargetFilename = "*.schema.json"
14-
$TableName = "configuration"
15-
$StorageAccount = "helloitscraigstr"
16-
$EnvironmentName = "dev"
17-
. $PSScriptRoot/../task/Invoke-Task.ps1
13+
$SourcePath = "$using:PSScriptRoot/resource"
14+
$TargetFilename = "*.schema.json"
15+
$TableName = "configuration"
16+
$StorageAccount = "testconversion41"
17+
$EnvironmentName = "dev"
18+
. $using:PSScriptRoot/../task/Invoke-Task.ps1
1819

20+
}
21+
finally {
22+
Clear-MockEnvironment
23+
}
1924
}
20-
finally {
21-
Clear-MockEnvironment
22-
}
25+
# Wait for default input
26+
Wait-Job $TestRunner
27+
Receive-Job $TestRunner
28+
29+
Wait-Job $TestRunner
30+
Receive-Job $TestRunner

tasks/EnvironmentConfiguration/tests/modules/UnitTest.Helpers.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Set-MockEnvironment {
1818
$ENV:PaymentsObjectArray = @"
1919
[{"Enabled": true, "aString": "string"}]
2020
"@
21-
21+
$ENV:GoogleHeaderUrl = "'https://www.googletagmanager.com/gtm.js?id='wrappedinquotes'&gtm_auth=someauth&gtm_preview=env-7&gtm_cookies_win=x'"
2222
}
2323

2424
function Clear-MockEnvironment {
@@ -34,7 +34,8 @@ function Clear-MockEnvironment {
3434
"ENV:PaymentsEnabled",
3535
"ENV:PaymentsInt",
3636
"ENV:PaymentsNumber",
37-
"ENV:PaymentsArray"
37+
"ENV:PaymentsArray",
38+
"ENV:GoogleHeaderUrl"
3839
)
3940
}
4041

tasks/EnvironmentConfiguration/tests/resource/SFA.DAS.Test.Valid.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"Enabled": true,
1818
"aString": "string"
1919
}
20-
]
20+
],
21+
"GoogleHeaderUrl": "'https://www.googletagmanager.com/gtm.js?id='wrappedinquotes'&gtm_auth=someauth&gtm_preview=env-7&gtm_cookies_win=x'"
2122
}

tasks/EnvironmentConfiguration/tests/resource/SFA.DAS.Test.schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
},
7676
"minItems": 0,
7777
"environmentVariable": "PaymentsObjectArray"
78+
},
79+
"GoogleHeaderUrl": {
80+
"type": "string",
81+
"environmentVariable": "GoogleHeaderUrl"
7882
}
7983
},
8084
"additionalProperties": false,

0 commit comments

Comments
 (0)