Tom Anderson
Tom Anderson

Reputation: 99

VSTS REST API - Updating a release

I'm trying to automated the tagging of VSTS releases, using the REST API documented here, using PowerShell, and it seems like it's impossible to add / edit Release tags using the API.

I'm able to GET a release OK using:

$release = Invoke-RestMethod -Method GET -Uri $releaseUri -Headers $headers

But when I try to update the release, I either get an error, or the tags remain unchanged. I'm trying to set the release to be retained, and to be tagged with a semantic version number, so first I tried, as the docs say, a PUT request:

$body = ConvertTo-Json @{ keepForever = "true"; tags = @($semanticVersion)}
Invoke-RestMethod -Method PUT -Uri $releaseUri -Headers $headers -ContentType "application/json" -Body $body

Which gives an error:

"VS402892: The ID of the release does not match the ID of the original release resource. Ensure that you are trying to update the correct resource."

If I try adding the ID to match, it says I need a name, if I add a name I get an error

"VS402886: Adding or deleting environments from the release is not allowed."

So it seems like it needs ALL of the release info to be provided in the body to work..? So I instead try to just add the info to the Release info I got from the GET request:

$release = Invoke-RestMethod -Method GET -Uri $releaseUri -Headers $headers
$release.tags += "$semanticVersion"
$release.keepForever = "True"
$body = ConvertTo-Json $release
Invoke-RestMethod -Method PUT -Uri $releaseUri -Headers $headers -ContentType "application/json" -Body $body

Which gives the error:

"VS402897: Artifact source with alias: 'build alias' has been modified. Field changed: 'DefinitionReference'"

I then try removing the Artifacts property from $body, and I get another error:

"VS402885: Adding or deleting artifacts from the release is not allowed."

I've just noticed that the DefinitionReference property doesn't have any info in it - could this be what's causing the error? And why would that not be getting returned when I GET get the release?

So it just seems like at this point a PUT request isn't going to do it, so I try using PATCH instead. This seems to work better - I don't get any errors with the following:

$body = ConvertTo-Json @{ keepForever = "true"; tags = @($semanticVersion)}
Invoke-RestMethod -Method PATCH -Uri $releaseUri -Headers $headers -ContentType "application/json" -Body $body

This correctly updates the 'keepForever' property, but the tags remain unchanged. I've also tried getting the full release info with GET again, and just adding the tags, but I still get the same thing where it updates the keepForever info, but not the tags.

I've also tried using POST, but I again get the same errors as with PUT:

"VS402897: Artifact source with alias: 'build alias' has been modified. Field changed: 'DefinitionReference'"* and *"VS402885: Adding or deleting artifacts from the release is not allowed."

Any ideas what I'm doing wrong here? It looks like it should work to me, and I'm able to do similar operations on builds in VSTS, and it updates some properties correctly - just not tags! I'm able to add the tags manually through my browser, just not through the API.

Upvotes: 3

Views: 1259

Answers (1)

jessehouwing
jessehouwing

Reputation: 114957

Looks like the Tags API for releases just issues a PATCH against the Tags property of the release. Captured from the UI:

Invoke-WebRequest 
-Uri "https://{account}.vsrm.visualstudio.com/{team-project}/_apis/Release/releases/{id}/tags/{tagname}" 
-Method "PATCH" 
-Headers 
@{
   "X-TFS-FedAuthRedirect"="Suppress"; 
   "Origin"="https://{account}.visualstudio.com"; 
   "X-VSS-ReauthenticationAction"="Suppress"; 
   "Accept-Encoding"="gzip, deflate, br"; 
   "Accept-Language"="nl,en-US;q=0.9,en;q=0.8,nl-NL;q=0.7"; 
   "Accept"="application/json;api-version=5.0-preview.1;excludeUrls=true"; 
} 
-ContentType "application/json"

Upvotes: 1

Related Questions