0
votes

I am using VSTS with git.

I have test cases with many bug work items being linked to them.

I want to specifically retrieve all bug work items linked to a particular test case using .NET Client Library or VSTS REST API. The version of the REST API can be 4.0 or later.

I could not find info pertaining to retrieving bug work items linked to a particular test case, though there is info related to retrieving all bug work items.

Here is the code I tried :

static List GetLinkedWorkItems() { int[] workitemIds = new int[] { 12697 };

        //VssConnection connection = Context.Connection;
        VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());
        WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

        List<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workitems = workItemTrackingClient.GetWorkItemsAsync(workitemIds, expand: WorkItemExpand.Links | WorkItemExpand.Relations).Result;

        foreach (var workitem in workitems)
        {
            Console.WriteLine("Work item {0}", workitem.Id);

            if (workitem.Links != null)
            {
                foreach (var link in workitem.Links.Links)
                {
                    Console.WriteLine("  {0} {1}", link.Key, link.Value);
                }
            }
        }


        return workitems;
    }

Note that there's no connectivity issues to VSTS. Also, I tried with query based approach as given below, but no use :

        VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());

        //create http client and query for resutls
        WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

        Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Test Case'" };
        WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;

        //Display reults in console
        var l = queryResults.WorkItemRelations;
        var t = queryResults.WorkItems.Skip(0).Take(100);
        if (queryResults == null || queryResults.WorkItems.Count() == 0)
        {
            Console.WriteLine("Query did not find any results");
        }
        else
        {
            foreach (var item in queryResults.WorkItems)
            {
                Console.WriteLine(item.Id);
                Console.WriteLine(item.Url);
            }
        }
1

1 Answers

0
votes

So what you need to do is updating the test case by removing it’s relations for bug work items. And you can achieve it by the update work item REST API. Details as below:

1. Get the relation work items for the test case

Use the Get work item REST API:

GET https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?$expand=all&api-version=4.1

2. Get the count of the related bug work items

Get the count of related work items from the response of step1. If the linked WIT is bug, then the count of relations is the count for linked bugs. Assume there are three bugs only link with the test case.

3. Update test case by removing the related bugs

The path for remove the related work items as /relations/index, and the index is the based on the count if the linked work items which start with 0. So if there are only three bugs linked to the test case, you can remove the paths with /relations/0, /relations/1 and /relations/2.

The Update work item REST API as below:

PATCH https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?api-version=4.1

application/json-patch+json:

[

  {
    "op": "remove",
    "path": "/relations/0"
  },
  {
    "op": "remove",
    "path": "/relations/1"
  }
  {
    "op": "remove",
    "path": "/relations/2"
  }

]