Reputation: 13
I'm trying to add retention to build run through the API (https://learn.microsoft.com/en-us/rest/api/azure/devops/build/leases/add?view=azure-devops-rest-6.0) with PowerShell.
My code below shows 3 of my attempts to add retention to the build:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Token))) #Encrypt token
$Head = @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$uri = "https://dev.azure.com/$myorg/$myproject/_apis/build/retention/leases?api-version=6.0-preview.1"
$BodyJson = '{
"daysValid":"5",
"DefinitionID":"12706",
"ownerId":"dashboard",
"protectPipeline":"true",
"runId":"2971238"
}'
$BodyConvertTo = @{
daysValid = "5"
DefinitionID= "12706"
ownerId = "dashboard"
protectPipeline = "true"
runId = "2971238"
} | ConvertTo-Json
$BodyObj = @{
daysValid = "5"
DefinitionID= "12706"
ownerId = "dashboard"
protectPipeline = "true"
runId = "2971238"
}
#BodyJson
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyJson -ContentType "application/json" -verbose
#BodyConvertTo
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyConvertTo -ContentType "application/json" -verbose
#BodyObj
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyObj -ContentType "application/json" -verbose
Outcomes:
$BodyJson:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be
deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RequestContentException","errorCode":0,"eventId":3000}
$BodyConvertTo:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be
deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RequestContentException","errorCode":0,"eventId":3000}
$BodyObj:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 5b090138-3392-4d76-8ebb-99d9968959c9.","typeName":"Newtonsoft.Json.JsonReaderException,
Newtonsoft.Json","typeKey":"JsonReaderException","errorCode":0,"eventId":0}
So I wonder, it don't wants Json nor PSobject, what I should send to body to make it work?
Upvotes: 1
Views: 2911
Reputation: 25928
Using Shell, it can be done as shown below. Here we frame the JSON string using jq utility, we use a retention period of 365 days and protect the pipeline from accidental deletion
$ RETENTION_LEASE_JSON_STRING=$(jq -cn --arg days 365 \
--arg defId <build definition id> \
--arg owner <org owner id> \
--arg protect true \
--arg runIdVal <build id> '[{ daysValid: $days, definitionId: $defId, ownerId:
$owner, protectPipeline: $protect, runId: $runIdVal }]')
$ curl -u <username>:<password> -H "Content-Type: application/json" -d $RETENTION_LEASE_JSON_STRING https://****.azure.com/{org-name}/{project-name}/_apis/build/retention/leases?api-version=6.0-preview.1
Upvotes: 0
Reputation: 31043
Try the following code:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Token))) #Encrypt token
$Head = @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$uri = "https://dev.azure.com/$myorg/$myproject/_apis/build/retention/leases?api-version=6.0-preview.1"
$Body = @(
@{
"definitionId" = "129"
"runId" = "2758"
"ownerId" = "User:32861cf9-xxxx-xxxx-xxxx-6f90c41bf1a6"
"daysValid" = "365000"
"protectPipeline" = "false"
}
)
$BodyJson = ConvertTo-Json $Body
#BodyJson
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyJson -ContentType "application/json" -verbose
Upvotes: 2