[Bug] Increment version number malformats the json files
As mentioned in #143, the increment version number action (re)formats the json files using the PowerShell ConvertTo-Json.
However, looking into the commit, some characters (e.g. &, ', <, >) are incorrectly converted.
We faced the same in our custom DevOps pipelines and added an extra step after manipulating our json file to reformat them again.
... | ConvertTo-Json -Depth 32 | Format-Json
# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
# https://stackoverflow.com/questions/33145377/how-to-change-tab-width-when-converting-to-json-in-powershell
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
# https://stackoverflow.com/questions/69345030/sign-is-converted-into-u0026-through-powershell
$json = $json -replace '\\u0026', '&' -replace '\\u0027', "'" -replace '\\u003c', '<' -replace '\\u003e', '>'
($json -Split "`n" | % {
if ($_ -match '[\}\]]\s*,?\s*$') {
# This line ends with ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent) + $($_.TrimStart() -replace '": (["{[])', '": $1' -replace ': ', ': ')
if ($_ -match '[\{\[]\s*$') {
# This line ends with [ or {, increment the indentation level
$indent++
}
$line
}) -Join "`n"
}
This makes the commits far more readable.
As a reference: Sample of a custom 'Create Release' step in our DevOps pipeline.
Are you using self-hosted runners? Do you have PowerShell 7 installed on the self-hosted runners? Then it should use PS7 to format the json file, which should solve this issue.
You probably have this text in your log:
WARNING: pwsh (PowerShell 7) not installed, json will be formatted by PowerShell 5.1