AlexanderM
AlexanderM

Reputation: 1683

Finding changesets associated with the work item or having specific comment TFS Api

I'm trying to find all changesets associated with the work item using Microsoft.TeamFoundation.WorkItemTracking.Client. Using query I was able to get the information about the work items in question, however I cannot find any changeset information on the object I'm getting back. In addition to that there are some changesets that are not linked to specific work item but easy identifiable by the comment. Is there a quick way to find these using tfs api?

Edit: this is not a duplicate of How to get work items associated with a changeset id using tfs api? b/c in that question person has a changeset and would like to find associated work items. In my case I have a work items and I would like to find all changesets associated with the specific work items. In addition to that I need to find all changesets that have specific string in the comment.

Upvotes: 2

Views: 4944

Answers (3)

Enis
Enis

Reputation: 186

You can also use RestAPI (as stated in the first answer)

https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#with-links-and-attachments

You need to filter out "relations" array with rel == "ArtifactLink"

Upvotes: 0

AlexanderM
AlexanderM

Reputation: 1683

After more googling on the subject and exploring tfs API here is what I ended up with:

If all you changesets are linked to the work items (not really my case but this is what I originally was asking about):

// based on https://etoptanci.wordpress.com/2011/05/04/seeing-all-code-changes-for-a-work-item/
private static void GetChangesForWorkItem()
{
    var configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(@"http://myserver:8080/tfs"));
    var tpcService = configurationServer.GetService<ITeamProjectCollectionService>();
    var collectionNodes = configurationServer.CatalogNode.QueryChildren(
           new[] { CatalogResourceTypes.ProjectCollection },
           false, CatalogQueryOptions.None);

    var collectionNode = collectionNodes.First(x => x.Resource.DisplayName == "<collection name>");

    // Use the InstanceId property to get the team project collection
    Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
    TfsTeamProjectCollection collection = configurationServer.GetTeamProjectCollection(collectionId);
    var vcs = collection.GetService<VersionControlServer>();
    var store = new WorkItemStore(collection);
    var workItems = new List<WorkItem>()
    {
        store.GetWorkItem(1123),
        store.GetWorkItem(1145),
    };

    var associatedChangesets = new List<Changeset>();

    foreach (var workItem in workItems)
    {
        foreach (var link in workItem.Links) 
        {
            if((link==null) || !(link is ExternalLink))
            continue;

            string externalLink = ((ExternalLink)link).LinkedArtifactUri;
            var artifact =LinkingUtilities.DecodeUri(externalLink);

            if (artifact.ArtifactType == "Changeset")
                associatedChangesets.Add(vcs.ArtifactProvider.GetChangeset(new Uri(externalLink)));
        }
    }

    Console.WriteLine(associatedChangesets.Select(x=>x.ChangesetId).OrderBy(x => x));
}

If you need to get by comment as well then you gate all changesets for the daterange and then filter out by Changeset.Comment which is a string.

Upvotes: 5

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31003

Check the REST API:

GET https://{instance}/defaultcollection/_apis/tfvc/changesets/{id}?api-version={version}[&includedetails={boolean}&includeworkitems={boolean}&includesourcerenames={boolean}&maxchangecount={int}&maxcommentlength={int}]

Upvotes: 1

Related Questions