5

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).

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:

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.

Since workItemWithoutBranchLink.Relations is null I'm not sure how else I could patch it.

Any ideas?

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Nathan
  • 435
  • 4
  • 16

2 Answers2

4

For git links the syntax is little different, here it's a working example (to link the master branch):

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;
}
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 2
    Works perfect, thank you. You even saved me my next question of how to build the branch uri – Nathan Feb 06 '20 at 23:53
2

Try to update your path to "/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. i.e. if you add a /- you are no longer at the top level.

Also seems to be the format used in this samples library.

Eric Smith
  • 2,340
  • 12
  • 16