Adolfo Perez
Adolfo Perez

Reputation: 2874

TFS Git Commit web page API call to get related WorkItems

I am trying to get a Git Commit related Work Items programmaticaly through the TFS REST API. I haven't found examples online but the tfs web portal seems to get this information by using a 'associatedWorkItems' api call...

When I navigate to my TFS portal for a given Git Commit:

http://{instance}/{collection}/{project}/_git/{repository}/commit/{commitId}

If I inspect the network traffic I see a POST call to:

http://{instance}/{collection}/{projectId}/_api/_versioncontrol/associatedWorkItems?__v=5

Which sends these Body Form Parameters in the request:

This call returns a list of associated Work Items inside a __wrappedArray collection.

Is there a way to call this from the TFS REST API? If so, where can I get the versions and RequestVerificationToken parameter values?

This is related to my previous question: Get latest Associated Work Items for Git Commit using TFS REST API

Update: I tried @PatrickLu-MSFT recommendation to pass 'IncludeWorkItems' parameter to GetcommitsAsync but still returned WorkItems as null:

GitQueryCommitsCriteria crit = new GitQueryCommitsCriteria();
crit.IncludeWorkItems = true;
crit.Ids = new List<string>(){"27a36707252ac9db742cc259a1018b9750d8e9a2"};

var commitTest = gitClient.GetCommitsAsync(repo.Id, crit).Result.FirstOrDefault();
//commitTest.WorkItems == null

Upvotes: 3

Views: 974

Answers (2)

Eugene Podskal
Eugene Podskal

Reputation: 10401

At least in TFS 2018.2 you can actually do it with GetCommits, though you should set commitIds through the ItemVersion/FromCommitId(GitQueryCommitsCriteria.Ids won't work):

var wiIds = (await git
    .GetCommitsAsync(
        project,
        repository,
        new GitQueryCommitsCriteria
        {
            // Ids query disregards IncludeWorkItems parameter.
            // ItemVersion query seems to disregard the Top parameter and returns 100 commits instead at least in TFS 2018.2.
            // So we use From+To query.
            FromCommitId = commitId,
            ToCommitId = commitId,
            IncludeWorkItems = true,
            Top = 1
        }))
    .Single()
    .WorkItems()
    .Select(wiRef => 
        Int32.Parse(wiRef.Id))

Upvotes: 2

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51153

Unfortunately, as said before, there is no such kind of Rest API ability to directly show the work items which associated with a GIT commit for now.

Here is also a related uservoice for your reference:

Rest API - Ability to show work items associated with a GIT commit

https://visualstudio.uservoice.com/forums/330519-visual-studio-team-services/suggestions/5951867-rest-api-ability-to-show-work-items-associated-w

The API you see from network traffic used, maybe a internal way. You could also judge it from the API version, _v=5 5 is definitely not a released Rest API version for now.

As a workaround, you could try to use .Net Client to do the similar task.

Specifying GitQueryCommitsCriteria.IncludeWorkItems = true in GitHttpClientBase.GetCommitsAsync returns associated work items in GitCommitRef.WorkItems. Note: both IncludeWorkItems and WorkItems have [EditorBrowsable(EditorBrowsableState.Never)] which hids it from editor suggestions.

Upvotes: 3

Related Questions