6

我正在尝试以编程方式在两个工作项之间添加父子关系。我正在使用 Microsoft Team Foundation 和 Visual Studio Services 库来导出和导入 TFS 2015 和 VSTS 积压对象。

https://docs.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries

https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples#migrating-work-items

我已经通过获取到我的服务器的 VssConnection 并获取 WorkItemTrackingHttpClient 来执行 Wiql 查询和创建工作项。我还有一个查询来识别目标工作项的父级。

我无法弄清楚的是如何添加子工作项与其父项之间的链接。我不知道添加父项的正确 JsonPatchDocument 项路径,或现有 WorkItem 上的正确属性或方法以使用父链接更新它。是否有人有关于使用这些库将父关系添加到工作项的文档链接或特定信息?

以下是上下文的一些代码摘录:

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// ...
var sourceConnection = new VssConnection(new Uri(_sourceTsUrl), new VssClientCredentials());
var targetConnection = new VssConnection(new Uri(_targetTsUrl), new VssClientCredentials());
var sourceClient = sourceConnection.GetClient<WorkItemTrackingHttpClient>();
var targetClient = targetConnection.GetClient<WorkItemTrackingHttpClient>();
// ...
var queryResults = sourceClient.QueryByWiqlAsync(query).Result;
var ids = queryResults.WorkItems.Select(x => x.Id).ToList();
var items = sourceClient.GetWorkItemsAsync(ids);
foreach (var item in items.Result)
{
    // ...
    var patchItem = new JsonPatchDocument();
    foreach (var fieldName in item.Fields.Keys)
    { patchItem.Add(new JsonPatchOperation() { Path = $"/fields/{fieldName}", Value = item.Fields[fieldName], Operation = Operation.Add }); }
    // TODO - add patch field(?) for parent relationship
    var parentResults = sourceClient.QueryByWiqlAsync(parentQuery).Result;
    // ...
    var task = targetClient.CreateWorkItemAsync(patchItem, targetProject, itemType, validateOnly, bypassRules, suppressNotifications);
    var newItem = task.Result;
    // TODO - alternatively, add parent via the returned newly generated WorkItem
}

附录:我尝试添加以下代码,但更改不会提交到远程对象,它只存在于本地内存中,我找不到推送更改/更新的方法。

if (!string.IsNullOrWhiteSpace(mappedParentUrl))
{
    if (newItem.Relations == null)
    { newItem.Relations = new List<WorkItemRelation>(); }
    newItem.Relations.Add(new WorkItemRelation() { Rel = "Parent", Title = mappedParentTitle, Url = mappedParentUrl });
}
4

1 回答 1

11

请参阅此代码以创建带有父链接的任务工作项(更新它以满足您的要求):

var url = new Uri("https://XXX.visualstudio.com"); 
            var connection = new VssConnection(url, new VssClientCredentials());
            var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();
            string projectName = "[project name]";
            int parentWITId = 771;//parent work item id
            var patchDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();
            patchDocument.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation() {
                Operation=Operation.Add,
                Path= "/fields/System.Title",
                Value="parentandchildWIT"
            });
            patchDocument.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/-",
                Value = new
                {
                    rel = "System.LinkTypes.Hierarchy-Reverse",
                    url = connection.Uri.AbsoluteUri+ projectName+ "/_apis/wit/workItems/"+parentWITId,
                    attributes = new
                    {
                        comment = "link parent WIT"
                    }
                }
            });
            var createResult = workitemClient.CreateWorkItemAsync(patchDocument, projectName, "task").Result;
于 2018-01-10T06:19:16.460 回答