【问题标题】:Correct json to add a parent link to a TFS work item更正 json 以将父链接添加到 TFS 工作项
【发布时间】:2017-09-17 05:46:19
【问题描述】:

我正在尝试使用 powershell 和 json 将父链接添加到 TFS 工作项。我们有一个内部 TFS 服务器(即,不是团队服务)。

我得到了我的查询的答案,所以我与 TFS 的连接正常,但是当我尝试更新时,我收到以下错误:

"You must pass a valid patch document in the body of the request."

我是一个 json 菜鸟,我从这个 MSDN page 得到了我的 json 骨架

这是我的 json:

[
  {
    "op": "add",
    "path": "/relations/-",
    "value":
    {
        "rel": "System.LinkTypes.Hierarchy-Reverse",
        "url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
        "attributes":
        {
            { "isLocked": false }
        }
    }
}
]

根据我发现的其他一些 json 示例,我在几个地方用方括号进行了测试,但它们没有帮助,所以我从上面的 MSDN 页面返回到语法。

这是我正在使用的 powershell 脚本。

param(
[System.String]$TaskId=288346
)

$username = "myUserInfo"
$password = "myPassword"

$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)       

$taskItemURL ="https://tfs.myCompanynet.org/tfs/DefaultCollection/_apis/wit/workitems/$TaskId"
$taskItemRequest = $taskItemUrl+'?$expand=relations' 
$taskItemJson = Invoke-RestMethod -uri "$taskItemRequest" -Method get -
Credential $credential 

if($taskItemJson.relations)
{
    write-host "relation exists: " $taskItemJson.relations[0].url
}
else
{
   write-host "relation does not exist. Creating it."
   $jsonTemplate = Get-Content E:\scripts\JsonTemplate.txt # | ConvertTo-Json

   $result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -UseDefaultCredentials -ContentType application/json-patch+json -body $jsonTemplate
}

如您所见,我已将 convertTo-json 注释掉,因为我收到此错误: ConvertTo-Json :转换后的 JSON 字符串格式错误。 我不确定我是否收到该错误,因为它已经是 json。

我还测试了跳过 get-content 并使用 -inFile 参数,但结果与上述相同。

$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -Credential $credential -ContentType application/json-patch+json -InFile E:\scripts\JsonTemplate.txt

关于我的 json 有什么问题有什么想法吗?

谢谢!

【问题讨论】:

    标签: json powershell tfs-workitem


    【解决方案1】:

    啊!我是如此接近。我偶然猜测属性下的双花括号是错误的,即使the documentation 看起来应该是这样。当我删除它们时,它工作得很好。

    现在我的 json 看起来像这样:

    [
    {
    "op": "add",
    "path": "/relations/-",
    "value":
      {
        "rel": "System.LinkTypes.Hierarchy-Reverse",
        "url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
        "attributes":
        {
             "isLocked": false 
        }
      }
    }
    ]
    

    【讨论】:

    • 上面的链接断开-您不必更新父值吗?目前正在为此苦苦挣扎
    • 抱歉没有早点回答。叹息,很难跟踪 MS 的文档移动。我不认为我改变了父母。你得到这个工作了吗?
    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2013-03-17
    • 2012-01-29
    • 2012-07-07
    • 2017-12-07
    • 2019-05-24
    相关资源
    最近更新 更多