Reputation: 609
I am trying to add a file "AssemblyInfo.cs" which contain a '' as part of the text.
When trying to use the RestAPI to do it i get Error 400
After Removing the Char RestApi pass
i am using the following code to convert the text to Json - Powershell based
function GetJSContentCS {
param (
$filepath
)
$read = (Get-Content -Encoding UTF8 -RAW -Path $filepath) -replace "(?s)`r`n\s*$"
$jscontent=''
foreach ($item in $read) {
$jsitem = $item+'\n'
$jsitem = $jsitem.replace('"','\"')
$jscontent += $jsitem
}
return $jscontent
}
GetJSContentCS -filepath 'C:\temp\AssemblyInfo (2).cs'
Any Advise Would be great
Upvotes: -1
Views: 200
Reputation: 30372
The problem is missing a \
character after c:
in the converted Json.
By running the PowerShell the converted Json is :
[assembly: XmlConfigurator(ConfigFile = @\"c:\appsLogging.xml\", Watch = true)]\n
However, the correct Json should like this:
[assembly: XmlConfigurator(ConfigFile = @\"c:\\appsLogging.xml\", Watch = true)]
Please try the following script to convert the text to Json:
function GetJSContentCS {
param (
$filepath
)
$read = (Get-Content -Encoding UTF8 -RAW -Path $filepath) -replace "(?s)`r`n\s*$"
cls
Write-Host $read
$jscontent=''
foreach ($item in $read) {
$jsitem = $item.replace('"','\"')
$jsitem = $jsitem.replace(':\',':\\')
$jscontent += $jsitem
}
return $jscontent
}
GetJSContentCS -filepath 'C:\temp\AssemblyInfo.cs'
UPDATE:
Sample PowerShell script for your reference to call the Pushes - Create REST API to add a file :
Param(
[string]$orgurl = “https://dev.azure.com/{organization}",
[string]$project = “ProjectName”,
[string]$repoid = “166d743e-623a-4bdd-923b-6af25ebfdf9a”,
[string]$oldObjectId = “cbfc5c415de65cf40bcc9509d8ee286e7c990a56”, #Previous commit ID
[string]$user = “”,
[string]$token = “PAT”
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get contents from the AssemblyInfo.cs file and convert to the correct Json.
function GetJSContentCS {
param (
$filepath
)
$read = (Get-Content -Encoding UTF8 -RAW -Path $filepath) -replace "(?s)`r`n\s*$"
cls
Write-Host $read
$jscontent=''
foreach ($item in $read) {
$jsitem = $item.replace('"','\"')
$jsitem = $jsitem.replace(':\',':\\')
$jscontent += $jsitem
}
return $jscontent
}
GetJSContentCS -filepath 'C:\temp\AssemblyInfo.cs'
#Create Json body
function CreateJsonBody
{
$value = @"
{
"refUpdates": [
{
"name": "refs/heads/master",
"oldObjectId": "$oldObjectId"
}
],
"commits": [
{
"comment": "Added a AssemblyInfo3.cs file.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/AssemblyInfo3.cs"
},
"newContent": {
"content": "$jscontent",
"contentType": "rawtext"
}
}
]
}
]
}
"@
return $value
}
$json = CreateJsonBody
#Add files - Push commint to repository
$Url = "$orgurl/$project/_apis/git/repositories/$repoid/pushes?api-version=7.1-preview.2"
$push = Invoke-RestMethod -Uri $Url -Method POST -ContentType "application/json" -Body $json -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$push
Upvotes: 1