如何在TFS2015中克隆事件时复制任务?

时间:2017-01-11 10:28:01

标签: tfs tfs2015 tfs-workitem

当我们在TFS中克隆任何事件(WorkItem)时,除了已经与原始事件关联的任务外,所有内容都会被复制。我们如何复制任务? 我错过了任何工作流程吗? 我是否需要为此添加任何扩展名/ API?

1 个答案:

答案 0 :(得分:0)

创建工作项的副本时,可以复制一些指向现有项的链接,例如排列到工作项的测试用例。但是,您无法创建子项的副本,例如任务。

例如:您创建了产品待办事项项目并将任务分配给产品待办事项项目。当您执行 创建工作项目副本 时,它不会复制'任务'并将它们链接到复制的工作项。

有一个相关的 uservoice ,您可以为此投票以获得更多关注:

  

让Workitem模板成为一等公民

     

https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/2073749-make-workitem-templates-first-class-citizens

现在您可以尝试使用excel和PS脚本来实现您想要的效果。更多详情请参阅此类似问题:How to copy a work item and its tasks

if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
     Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")

# Connect to TFS and get Work Item Store.
$tfsCollectionUrl = "https://tfs.CORP.com/tfs/group"
$tfs = Get-TfsServer -name $tfsCollectionUrl
$ws = $tfs.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")

$storyID = 15211    # ID of the story you want to copy.

$story = $ws.GetWorkItem($storyID)
Write-Host "Cloning User Story: " + $story.Title

#Clone User Story
$cloneStory = $story.Copy()
($story, $cloneStory )
$cloneStory.Title = "COPY : " + $story.Title

# cloneStory will not have links to all the tasks that were linked to the orginal story.
# cloneStory will have two links, one to the same "feature" that the orginal was linked to, and one to the story it was cloned from.
$cloneStory.Links

# cloneStory will have 0 for an ID, because it has not yet been saved.
$cloneStory.Id
#$cloneStory.Save()
# cloneStory will now have an ID.
$cloneStory.Id
$parentID = $cloneStory.Id  # Parent ID will be used to link new cloned tasks to this story.

$links = $story.Links

# Define a Link Type to be used in the loop.
$linkType = $ws.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]


foreach ( $link in $links )
{

    $itemID = $link.RelatedWorkItemId
    #$itemID

    $item = $ws.GetWorkItem($itemID)

    if ( ($item.Type).Name -eq "Task" )
    {
        $reportLine = "Cloning Task ID:{0} {1}" -f $itemId, $item.Title
        Write-Host $reportLine
        # Clone the Task
        # Create the Parent Link object
        # Add the Parent Link to Cloned Task
        # Save New Cloned Task

        $cloneTask = $item.Copy()
        $cloneTask.Title = "COPY : " + $item.Title
        $parentLink = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $parentID)
        $cloneTask.WorkItemLinks.Add($parentLink)
        $cloneTask.save()
        $cloneTask
    }
    else
    {
        $reportLine = "Skipping: {0} is not a Task, it is a {1}" -f $item.Title, ($item.Type).Name
        Write-Host $reportLine
    }
}