Retrieving changesets form a work item programmatically

277 Views Asked by At

I need to try retrieve a list of changesets that are linked to certain work items and display info on them in the program. But I'm having a lot of trouble figuring it out.

I have been searching the internet extensively for 4 days and have come up empty handed. Is doing this even possible?

I have already tried using wiql http client but it seems to only be able to return work item information.

2

There are 2 best solutions below

1
On BEST ANSWER

Use Get Work Item with $expand=relations. In this case, you receive all relations including changesets. Example:

enter image description here

6
On

You can reference bellow sample to do that programmatically.

C# sample for your reference to retrieve a list of changesets that are linked to a specific work item.

using System;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri accountUri = new Uri("https://dev.azure.com/{organization}");                 
            String personalAccessToken = "PAT-Here";              
            int workItemId = 227;   // ID of a work item

            // Create a connection to the account
            VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));

            // Get an instance of the work item tracking client
            WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

            WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(workItemId, expand: WorkItemExpand.Relations).Result;

            Console.WriteLine(workitem.Id);

            Console.WriteLine("Relations with changesets associated:");
            foreach (var relation in workitem.Relations)
            {

                if (relation.Rel == "ArtifactLink")
                {
                    Console.WriteLine(relation.Url);
                }
            }
            Console.ReadLine();
        }
    }
}

enter image description here

PowerShell script calling the Get Work Item REST API to retrieve a list of changesets that are linked to a specific work item.

Param(
   [string]$orgurl = "https://dev.azure.com/{organization}", 
   [string]$project = "ProjectName",
   [string]$workitemid = "227",
   [string]$user = "",
   [string]$token = "PAT-Here"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

#Get workitem relateions
$baseUrl = "$orgurl/$project/_apis/wit/workitems/$($workitemid)?"+"$"+"expand=all"          
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

#Get changesets
$changesets = $response.relations | where {$_.rel -eq 'ArtifactLink' -and $_.attributes.name -eq "Fixed in Changeset"}

if($changesets){
    
    $changesets | select @{N="ChangesetID";E={$_.attributes.comment}}, @{N="CreatedDat";E={$_.attributes.resourceCreatedDate}}, url
}
else {
  Write-Host "No changeset associated to work item $workitemid ."
}

enter image description here