Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式链接Azure DevOps中的分支和工作项_C#_Azure Devops_Azure Devops Rest Api - Fatal编程技术网

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

C# 如何以编程方式链接Azure DevOps中的分支和工作项,c#,azure-devops,azure-devops-rest-api,C#,Azure Devops,Azure Devops Rest Api,我正试图找到一种方法,通过我正在构建的应用程序()在Azure DevOps中添加一个分支作为链接 我越来越熟悉VisualStudio服务和TeamFoundation.NET库,并尝试(例如)使用已通过DevOps UI创建的其中一个链接获取一个工作项,然后将其移植到另一个工作项,如下所示: var workItemWithBranchLink = await _WorkItemTrackingHttpClient.GetWorkItemAsync(3985, expand: WorkIte

我正试图找到一种方法,通过我正在构建的应用程序()在Azure DevOps中添加一个分支作为链接

我越来越熟悉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: '工作项修补程序不支持在处修补顶级属性 路径/关系

由于没有BranchLink.Relations的
工作项为空,我不确定如何修补它


有什么想法吗?

试着更新你的路径到
“/relations/-”
。我不确定.net库中的修补程序格式是否遵循,但考虑到顶级属性错误消息,似乎很可能遵循。i、 e.如果您添加了
/-
,您将不再处于顶层


也似乎是本书中使用的格式

对于git链接,语法略有不同,这里是一个工作示例(链接
主分支):

VssConnection testConnection=新的VssConnection(新Uri(“azure devops Uri”)、新的Microsoft.VisualStudio.Services.Common.VssCredentials();
var workItemClient=testConnection.GetClient();
var gitClient=testConnection.GetClient();
字符串projectId=“cf456145-abgd-ffs23-be61-0fca39681234”;
字符串repositoryId=“d6856145-abgd-42a3-be61-0fca3968c555”;
var branchUri=string.Format
(
"vstfs:///Git/Ref/{0}%2f{1}%2f{2}“,
投射的,
repositoryId,
“GBmaster”
);
var json=new JsonPatchDocument();
json.Add(
新的JSONPATCH操作()
{
操作=操作。添加,
Path=“/relations/-”,
值=新
{
rel=“ArtifactLink”,
url=branchUri,
属性=新
{
name=“分行”,
comment=“comment”
}
}
});
尝试
{
int workItemToUpdate=142144;
var update=workItemClient.UpdateWorkItemAsync(json,workItemToUpdate).Result;
}
捕获(例外e)
{
var错误=e.消息;
}

非常好用,谢谢。你甚至帮我省下了下一个关于如何构建分支uriYup的问题,就是它。该示例库对于项目的其余部分也非常有用,谢谢。很高兴它有帮助!
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;
}