0

I'm using TFS 2017 update 1 on premises. I'm using #ID in log comments of commits in order to associate workitem ID (of User Story, Task etc.) with GIT commits of source code. It properly works (I can see links to commit from workitem interface) but I'd like to use TFS SDK API with tfs aggregator in order to better manage GIT commits (e.g. dashboards using custom fields) . How can access git commits from Microsoft.TeamFoundation.WorkItemTracking.Client ?

Francesco Sclano
  • 145
  • 1
  • 12

1 Answers1

0

The git commits are linked to workitems with "ExternalLink" type. So you can get the links of the work item to query that information.

    WorkItemStore wis = ttpc.GetService<WorkItemStore>();
    int workitemid = 1;
    WorkItem wi = wis.GetWorkItem(workitemid);
    foreach (Link w in wi.Links)
    {
        if (w.GetType().ToString() == "Microsoft.TeamFoundation.WorkItemTracking.Client.ExternalLink")
        {
            ExternalLink el = (ExternalLink)w;
            Console.WriteLine(el.LinkedArtifactUri);

        }

    }

The LinkedArtifactUri will include the ID of the git commit. And then you can get the commit information via:

            GitHttpClient ght = ttpc.GetClient<GitHttpClient>();
            GitCommit gc = ght.GetCommitAsync("CommitID", "RepoID",99).Result;
            Console.WriteLine(gc.Committer.Date);
            Console.WriteLine(gc.Committer.Name);
            foreach (GitChange gch in gc.Changes)
            {
                Console.WriteLine(gch.Item.Path);
            }
            Console.ReadLine();
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • Is there a way to programmatically obtain the author, date, file name etc. of the git commit? – Francesco Sclano Aug 18 '18 at 08:10
  • @user3676571 Yes, as soon as you get the commit id and repo id, you can use GetCommitAsync method in Microsoft.TeamFoundation.SourceControl.WebApi to get the commit information. See my update answer for details. – Eddie Chen - MSFT Aug 20 '18 at 01:43
  • From workitem's history I have following value as external link vstfs:///Git/Commit/d0331cb9-1de2-462d-b712-ca84c797a71b%2fae6aee68-64ec-4c89-b3a2-36fcf26d41eb%2f219ea05aeb2cd116e1174f7e0c55cba06d750789. From web interface of same workitem when I click on commit link I obtain the opening of a new page of browser in this corresponding link: http://tfs_server-collection_url/d0331cb9-1de2-462d-b712-ca84c797a71b/_git/ae6aee68-64ec-4c89-b3a2-36fcf26d41eb/commit/219ea05aeb2cd116e1174f7e0c55cba06d750789 How can I programmatically obtain the second link from the first link? – Francesco Sclano Sep 14 '18 at 13:05