使用 VsConnection WorkItemTrackingHttpClient 补丁通过 VSTS 客户端 API 添加父关系

问题描述 投票:0回答:1

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

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

https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples#migration-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 });
}
c# tfs azure-devops json-patch azure-devops-rest-api
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;
© www.soinside.com 2019 - 2024. All rights reserved.