簡體   English   中英

如何以編程方式鏈接 Azure DevOps 中的分支和工作項

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

我正在嘗試找到一種方法,通過我正在構建的應用程序在 Azure DevOps 中添加一個分支作為鏈接(基本上就是這個,但在 C# 控制台應用程序中)。

我開始熟悉 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);

但是,這會引發異常

Microsoft.VisualStudio.Services.WebApi.Patch.PatchOperationFailedException: '工作項補丁不支持修補路徑/關系中的頂級屬性。

由於workItemWithoutBranchLink.Relations為空,我不知道我還能如何修補它。

有任何想法嗎?

嘗試將您的路徑更新為"/relations/-" 我不確定 .net 庫中的補丁格式是否遵循Rest API,但鑒於頂級屬性錯誤消息似乎很可能。 即,如果您添加/-您不再處於頂層。

似乎也是這個示例庫中使用的格式。

對於 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM