【问题标题】:Issue when updating build definition using the REST api of VSTS使用 VSTS 的 REST api 更新构建定义时的问题
【发布时间】:2018-10-07 06:26:52
【问题描述】:

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

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 放回时它失败了。

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

# 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,它可以工作...

【问题讨论】:

  • 您是否检查过$projectDefJson 中的JSON 输出是否有效? ConvertTo-Json 并不完美
  • @DanielMann 正在处理它,还有其他选择吗?
  • 当我输出结果被截断时,有什么办法解决这个问题?
  • 老实说,我看不出你所做的任何事情看起来非常不正确。不过,可能会感兴趣的一点:如果您允许构建定义访问 OAuth 令牌,则可以使用 $env:SYSTEM_ACCESSTOKEN 访问令牌——无需提供单独的 PAT。
  • Set-Content -- 将其转储到文件中。然后,您可以在任意数量的在线 JSON 验证器中验证 JSON 是否有效。

标签: azure-devops azure-pipelines azure-devops-rest-api


【解决方案1】:

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

([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

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2016-11-17
    • 1970-01-01
    • 2018-02-12
    • 2017-08-03
    • 2023-04-04
    • 1970-01-01
    • 2017-08-26
    • 2018-05-24
    相关资源
    最近更新 更多