使用VsConnection WorkItemTrackingHttpClient补丁通过VSTS客户端API

时间:2018-01-09 19:36:27

标签: c# tfs azure-devops json-patch azure-devops-rest-api

我正在尝试以编程方式在两个工作项之间添加父子关系。我使用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到我的服务器并获得Wo​​rkItemTrackingHttpClient来执行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 });
}

1 个答案:

答案 0 :(得分:2)

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

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;