我们在一些产品积压项目(pbi)中有相同的任务,每次我们都必须创建pbi和任务以及相同的东西,只有一点点差异。因此,我们想要创建一个模板 pbi 和任务,我们做到了,但无法将任务分配给 pbi。当我们检查字段时,没有“父 ID”或类似的内容。
如何做到这一点?我也可以接受我们可以编写一个 powershell 脚本。版本是版本16.131.27701.1
最佳答案
我们无法将关系添加到 TFS 工作项模板中,我们只能为工作项模板中的特定工作项类型设置可用字段的值。请参阅Use templates to add and update work items了解详情。
因此,我们无法将子项分配给 TFS 工作项模板中的 PBI。不过,我们可以通过 REST API ( Add a Link ) 分配子级。
例如:
PATCH http://server:8080/tfs/DefaultCollection/_apis/wit/workitems/111?api-version=4.0
Content-Type: application/json-patch+json
[
{
"op": "test",
"path": "/rev",
"value": 8
},
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "System.LinkTypes.Hierarchy-Forward",
"url": "http://server:8080/tfs/DefaultCollection/{ProjectName or ID}/_apis/wit/workItems/129",
"attributes": {
"comment": "Add child link to PBI"
}
}
}
]
您可以使用以下 PowerShell 脚本将多个子工作项分配给特定的父 PBI:
Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "0511ScrumTFVC",
[string]$PBI = "111",
[string]$user = "Domain\user",
[string]$token = "Password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$uri = "$baseurl/_apis/wit/workitems/$($PBI)?api-version=4.0"
#For non-continuous child work item IDs
#$childs = (130,134,136)
#For Continuous child work item IDs
$childs = (130..134) #This will link child work item 130,131,132,134 to PBI 111
foreach ($child in $childs)
{
function CreateJsonBody
{
$value = @"
[
{
"op": "test",
"path": "/rev",
"value": 8
},
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "System.LinkTypes.Hierarchy-Forward",
"url": "$baseurl/$projectName/_apis/wit/workItems/$child",
"attributes": {
"comment": "Add child work itme $child to PBI $PBI"
}
}
}
]
"@
return $value
}
$json = CreateJsonBody
$response = Invoke-RestMethod -Uri $uri -Method PATCH -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}
关于templates - 如何将子项分配给 TFS 模板中的产品待办事项列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52813841/