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

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

我正在尝试以编程方式在两个工作项之间添加父子关系。我正在使用 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#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 });
}

最佳答案

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

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;

关于c# - 使用 VsConnection WorkItemTrackingHttpClient 补丁通过 VSTS 客户端 API 添加父关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48175492/

相关文章:

azure - 从 Azure DevOps 发布管道获取文件

migration - 我可以批量更新 VSTS 构建管道定义吗?

azure-devops - 是否可以让 Azure Pipeline 在提交时修改 README.md?

c# - Monotouch 检查两个框架的碰撞

c# - 在 WinForms 输入焦点上自动弹出平板电脑触摸键盘

TFS 2010-用于转换为分支的命令行

SVN 与 Team Foundation Server

c# - 说服遗留应用程序 VB6 开发人员切换到 C#

c# - 无法使用 SSDT Visual Studio 2012 "Unable to determine the database platform type. The database may be unavailable.."比较数据库架构

rest - 使用 REST api 创建一个新的团队项目