Post方法 - 更改嵌套值

时间:2018-05-30 16:08:08

标签: rest tfs

我正在尝试更改我的一个TFS 2017版本的值 - 特别是变量。根据我的理解,Patch根本不受支持。我可以使用Post方法成功排队构建,我也尝试使用相同的命令更改值。

当我运行Get方法时,我有:

out_put == "00000Hello"

我需要更改构建版本,其他所有内容都保持不变。邮差中的我的身体看起来像:

*A bunch of text*
"variables": {
    "system.debug": {
        "value": "false",
        "allowOverride": true
    },
     "BuildVersion": {
        "value": "ValueIWantToChange"
    }
},
*A bunch of text*

当我在Postman中运行时,我收到此错误:

  

"值不能为空。\ r \ n参数名称:definition.Repository"

有人能告诉我哪里出错或者是否可以使用其他方法?

2 个答案:

答案 0 :(得分:2)

似乎您想根据您的描述更新构建定义。

要使用REST API更新构建定义,您需要使用 PUT 方法,有关详细信息,请参阅Definitions - Update Definition

  1. 首先获取构建定义:

    GET http://server:8080/tfs/DefaultCollection/ScrumProject/_apis/build/definitions/6?api-version=3.2
    
  2. 将第一步中的所有json响应复制为请求正文, 然后更改您想要的特定变量的值 修改。

    PUT http://SERVER:8080/tfs/DefaultCollection/ScrumProject/_apis/build/definitions/6?api-version=3.2
    
    Content-Type: application/json
    
  3. 注意您需要在请求正文中提供最新版本

    <强>更新

    您也可以通过调用REST API来更新特定的变量值来使用PowerShell,只需尝试下面的示例:(下面示例中的变量名称为lctest,您只需将其替换为您的自己的变量名。

    Param(
       [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
       [string]$project = "ProjectName",
       [string]$definitionid = "6",
       [string]$user = "username",
       [string]$token = "password"
    )
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    #Get build definition
    $defurl = "$collectionurl/$project/_apis/build/definitions/$($definitionid)?api-version=3.2"            
    $definition = Invoke-RestMethod -Uri $defurl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    #Set new value for the specific variable
    $definition.variables.lctest.value = "1.0.0.4"
    
    $json = @($definition) | ConvertTo-Json -Depth 99
    
    #Update the definition
    $updatedef = Invoke-RestMethod  -Uri $defurl  -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    write-host $definition.variables.lctest.value
    

    enter image description here

答案 1 :(得分:0)

我不久前发现了自己的问题,却忘记了更新。我的最初任务是获取章鱼的API,所以这是长版本。如果您只对REST命令感兴趣,请参考代码的最后一部分。只是想将其余部分添加到其他上下文中。

#Create a folder
if(test-Path C:\Test){}
else{
new-item -path "C:\" -name "Test" -ItemType "directory"}

$encodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$TFSAPIKeyForAutomatedBuild"))
$GetURI = "$MyURI"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $encodedPAT")
[string]$Global:ChangeVersion = [version]$OctopusParameters["Octopus.Action[Deploy Package].Package.NuGetPackageVersion"]
write-host $ChangeVersion
$GetBuildresponse = Invoke-RestMethod -Method Get -header $headers -ContentType "application/json" -Uri $GetUri
write-host $GetBuildResponse
$y = convertTo-json $GetBuildresponse -depth 99 | Out-file -FilePath "C:\test\FromPostmanCopy.json"


$z = (get-content "C:\test\FromPostmanCopy.json") | select-string -pattern '(?<=value":  "2.)(.*)(?=")' | % { $_.Matches} | % { $_.value }
Write-Host $z
$Content = (Get-Content "C:\Test\FromPostmanCopy.json")
$content -replace "2.$z", $changeVersion | out-file "C:\Test\FromPostmanCopy.json"
$Content = (Get-Content "C:\Test\FromPostmanCopy.json")
$Buildresponse = Invoke-RestMethod -URI $GetURI -Method Put -header $headers -Body $content -ContentType application/json
相关问题