如何在TFS模板中将子项分配到产品待办事项项

时间:2018-10-15 09:40:34

标签: templates tfs

在某些产品待办事项项(pbi)中,我们具有相同的任务,并且每次我们必须创建pbi和任务以及相同的东西时,只有一点点差异。因此,我们想创建一个模板pbi和任务,我们做了,但是不能将任务分配给pbi。当我们检查字段时,没有“父ID”或类似的内容。

该怎么做?我也接受我们可以编写Powershell脚本。

版本为16.131.27701.1

1 个答案:

答案 0 :(得分:2)

我们不能将关系添加到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)}
}