使用VSTS的REST API更新内部定义时出现的问题 [英] Issue when updating build definition using the REST api of VSTS

查看:55
本文介绍了使用VSTS的REST API更新内部定义时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新构建定义中的变量,但是当它尝试运行 Invoke-RestMethod 时,我收到以下异常:

I want to update a variable in my build definition but when it tries to run the Invoke-RestMethod I receive the following exception:

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, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a","typeKey":"RequestContentException","errorCode":0,"eventId":3000}
At D:\a\_temp\231f1be5-edc0-4bd9-a2e4-efd23a8308d1.ps1:42 char:1
+ Invoke-RestMethod -Method Put -Uri "$($projectDef.Url)&api-version=2. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

其他所有东西似乎都起作用,我得到了构建定义,可以更新该变量,但是当我尝试使用 Invoke-RestMethod 将JSON放回原位时,它将失败.

Everything else seems to work, I get the build definition, I can update the variable but when I try to PUT the json back using the Invoke-RestMethod it fails.

下面是在Powershell内联脚本中运行的已使用代码:

Below is the used code which runs in a Powershell inline script:

# This script is intended to be used for PowerShell script tasks in VSTS in "inline mode" 

$valueName = 'ProjectBuildNumber'
$token = 'MYTOKENCODE'

$uriRoot = $env:SYSTEM_TEAMFOUNDATIONSERVERURI
$ProjectName = $env:SYSTEM_TEAMPROJECT
$ProjectId = $env:SYSTEM_TEAMPROJECTID 
$uri = "$uriRoot$ProjectName/_apis/build/definitions?api-version=2.0"

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $token)))
$header = @{Authorization = ("Basic {0}" -f $base64AuthInfo)}

# Get the list of Build Definitions
$buildDefs = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers $header

# Find the build definition for this project
$buildDef = $buildDefs.value | Where-Object { $_.Project.id -eq $ProjectId }

if ($buildDef -eq $null)
{
    Write-Error "Unable to find a build definition for Project '$ProjectName'. Check the config values and try again." -ErrorAction Stop
}
# NOTE: ensure we call the v 2.0 api! (both get and put calls need the same api versions!)
# get its details
$projectDef = Invoke-RestMethod -Uri "$($buildDef.Url)?api-version=2.0" -Method Get -ContentType "application/json" -Headers $header

if ($projectDef.variables.$valueName -eq $null)
{
    Write-Error "Unable to find a variable called '$valueName' in Project $ProjectName. Please check the config and try again." -ErrorAction Stop
}
# get and increment the variable in $valueName
[int]$counter = [convert]::ToInt32($projectDef.variables.$valueName.Value, 10)
$updatedCounter = $counter + 1
Write-Host "Project Build Number for '$ProjectName' is $counter. Will be updating to $updatedCounter"

# Update the value and update VSTS
$projectDef.variables.$valueName.Value = $updatedCounter.ToString()
$projectDefJson = $projectDef | ConvertTo-Json -Depth 50 -Compress

# when we build the URL need to cater for if the Project Definition URL already has parameters or not.
$separator = "?"
if ($projectDef.Url -like '*?*')
{
    $separator = "&"
}
$putUrl = "$($projectDef.Url)$($separator)api-version=2.0"
Write-Host "Updating Project Build number with URL: $putUrl"
Invoke-RestMethod -Method Put -Uri $putUrl -Headers $header -ContentType "application/json" -Body $projectDefJson | Out-Null

更新

当我使用邮递员进行测试时,我先执行一次get操作,然后执行一次put操作,

When I use postman to test this, I first run a get and then a put, it works...

推荐答案

好的问题在于字符&替换为 \ u0026 .添加了以下内容来解决此问题:

Ok problem was with the character & which got replaced by \u0026. Added the following which solved this:

([System.Text.Encoding]::UTF8.GetBytes($projectDefJson))

所以最后一行变为:

Invoke-RestMethod -Method Put -Uri $putUrl -Headers $header -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($projectDefJson)) | Out-Null

这篇关于使用VSTS的REST API更新内部定义时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆