简体   繁体   English

如何以编程方式链接 Azure DevOps 中的分支和工作项

[英]How to link a branch and work item in Azure DevOps programmatically

I'm trying to find a way to add a branch as a link in Azure DevOps through an application I'm building ( Basically just this, but within a C# console app ).我正在尝试找到一种方法,通过我正在构建的应用程序在 Azure DevOps 中添加一个分支作为链接(基本上就是这个,但在 C# 控制台应用程序中)。

I'm becoming familiar with the VisualStudio Services and TeamFoundation .NET libraries and have tried, as an example, to grab a work item with one of these links already created via the DevOps UI and port it to over to another work item like so:我开始熟悉 VisualStudio 服务和 TeamFoundation .NET 库,并尝试使用已通过 DevOps UI 创建的这些链接之一获取工作项,并将其移植到另一个工作项,例如:

var workItemWithBranchLink = await _WorkItemTrackingHttpClient.GetWorkItemAsync(3985, expand: WorkItemExpand.Relations);
var workItemWithoutBranchLink = await _WorkItemTrackingHttpClient.GetWorkItemAsync(3988, expand: WorkItemExpand.Relations);
var document = new JsonPatchDocument();
document.Add(new JsonPatchOperation()
{
    Operation = Operation.Add,
    Path = "/relations",
    Value = workItemWithBranchLink.Relations 
});
await _WorkItemTrackingHttpClient.UpdateWorkItemAsync(document, (int)workItemWithoutBranchLink.Id);

However, this throws an exception但是,这会引发异常

Microsoft.VisualStudio.Services.WebApi.Patch.PatchOperationFailedException: 'Work item patch does not support patching the top level property at path /relations. Microsoft.VisualStudio.Services.WebApi.Patch.PatchOperationFailedException: '工作项补丁不支持修补路径/关系中的顶级属性。

Since workItemWithoutBranchLink.Relations is null I'm not sure how else I could patch it.由于workItemWithoutBranchLink.Relations为空,我不知道我还能如何修补它。

Any ideas?有任何想法吗?

Try to update your path to "/relations/-" .尝试将您的路径更新为"/relations/-" I am not sure if the patch format in the .net library follows the Rest API but seems likely given the top level property error message.我不确定 .net 库中的补丁格式是否遵循Rest API,但鉴于顶级属性错误消息似乎很可能。 ie if you add a /- you are no longer at the top level.即,如果您添加/-您不再处于顶层。

Also seems to be the format used in this samples library .似乎也是这个示例库中使用的格式。

For git links the syntax is little different, here it's a working example (to link the master branch):对于 git links 语法有点不同,这里是一个工作示例(链接master分支):

VssConnection testConnection = new VssConnection(new Uri("azure-devops-uri"), new Microsoft.VisualStudio.Services.Common.VssCredentials());
var workItemClient = testConnection.GetClient<WorkItemTrackingHttpClient>();
var gitClient = testConnection.GetClient<GitHttpClient>();
string projectId = "cf456145-abgd-ffs23-be61-0fca39681234";
string repositoryId = "d6856145-abgd-42a3-be61-0fca3968c555";
var branchUri = string.Format
(
    "vstfs:///Git/Ref/{0}%2f{1}%2f{2}",
    projectId,
    repositoryId,
    "GBmaster"
);

var json = new JsonPatchDocument();
json.Add(
new JsonPatchOperation()
{
     Operation = Operation.Add,
     Path = "/relations/-",
     Value = new
     {
            rel = "ArtifactLink",
            url = branchUri,
            attributes = new
            {
                name = "Branch",
                comment = "Comment"
            }
     }
});

try
{
     int workItemToUpdate = 142144;
     var update = workItemClient.UpdateWorkItemAsync(json, workItemToUpdate).Result;
}
catch (Exception e)
{
     var error = e.Message;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式为本地 Azure DevOps Server 工作项注释中的 Active Directory 用户帐户添加 @提及(2021 年 1 月) - Programmatically add @mention for Active Directory User Account in on-prem Azure DevOps Server work item comment (Jan, 2021) 如何在 Azure DevOps 中以编程方式下载附件? - How to programmatically download attachments in Azure DevOps? Azure DevOps Rest API - 如何在工作项中获取指定用户的显示名称 - Azure DevOps Rest API - How to grab assigned user's display name in work item C# - 如何从 Azure DevOps 工作项列表反序列化 json? - C# - How do I deserialize json from Azure DevOps Work Item List? 无法访问Azure DevOps工作项的“分配给”字段 - Not able to access the Assigned To field of a Azure DevOps Work Item Azure DevOps 使用 API 获取工作项通知 - Azure DevOps Get work item notification using API 使用 Asp.net 更新单个工作项 Azure Devops - Updates a single work item Azure Devops using Asp.net 无法在 Azure Devops 中打开附加到工作项的 Excel 文件 - Unable to open the Excel file attached to work item in Azure Devops Azure DevOps API 创建工作项返回 404 错误 - Azure DevOps API creating work item returns 404 error 以编程方式创建 Azure DevOps 工件 - Creating Azure DevOps Artifact programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM