【问题标题】:Programmatically access TFS annotations to determine owner以编程方式访问 TFS 注释以确定所有者
【发布时间】:2016-05-06 12:54:10
【问题描述】:

我在一个项目团队工作,我们的应用程序在 TFS 中。我试图确定每个团队成员负责多少行代码。在 TFS 中,我知道 Visual Studio 界面中的 Annotate 功能,它允许您查看谁最后修改了每一行代码,因此我知道 TFS 有这些信息。

我编写了一个小型控制台应用程序,它可以访问我的 TFS 项目及其所有文件,但我现在需要以编程方式访问注释,以便查看每行的所有者是谁。这是我现有的代码:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

public class Program
{
    static void Main(string[] args)
    {
        var credentials = new NetworkCredential(username, password, domain);
        var server = new TfsTeamProjectCollection(new Uri(serverUrl), credentials);
        var version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;
        var items = version.GetItems(projectPath, RecursionType.Full);
        var fileItems = items.Items.Where(x => x.ItemType == ItemType.File);            
        foreach (var fileItem in fileItems)
        {
            var serverItem = fileItem.ServerItem;
            //TODO: retrieve and parse annotations
        }
    }
}

一旦我拥有 TFS 项目,我似乎无法弄清楚如何检索注释。 This 链接解释了如何通过调用 TFPT 来实现,但在实现它 (tfpt annotate /noprompt <filename>) 之后,您只提供每行的最后一个变更集和代码,而不是所有者。

我还发现了一个 Microsoft.TeamFoundation.VersionControl.Server 命名空间,它有一个 Annotation 类。我在我的机器上安装了 TFS 以访问该 DLL,但它似乎对这个问题没有任何帮助。

如何以编程方式访问 TFS 注释以确定文件的一行代码的所有者?

【问题讨论】:

  • @CodeCaster:哈。正如您评论的那样,我刚刚更新了我的问题。答案并不是真正的答案......
  • 你能解释一下它没有帮助吗?如果这是真的,那么就没有内置的方法可以做到这一点,也没有任何工具(请注意,直接要求工具是题外话)。但是,您可以使用 TFS API 并编写一个小程序来执行此操作。
  • @CodeCaster:我想真正的问题是 TFS VersionControl 库似乎几乎完全缺乏文档。

标签: visual-studio tfs


【解决方案1】:

当 Item 的更改类型为 Branch 时,您可能需要查询分支。 举个简单的例子,有一个场景

$/Project
  /Main`
   /a.txt
  /Develop
   /a.txt (branched from main)

查询$/project/Develop/a.txt的历史时,也可以使用以下代码获取$/project/Main/a.txt的历史

void GetAllHistory(string serverItem)
    {                    
        var changesets=vcs.QueryHistory(serverItem,
            Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest,
            0,
             Microsoft.TeamFoundation.VersionControl.Client.RecursionType.None,
             null,
             new Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec(1),
              Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest,
              int.MaxValue,
              true,
              false);

        foreach (var obj in changesets)
        {
            Microsoft.TeamFoundation.VersionControl.Client.Changeset cs = obj as Microsoft.TeamFoundation.VersionControl.Client.Changeset;
            if (cs == null)
            {
                return;
            }
            foreach (var change in cs.Changes)
            {
                if (change.Item.ServerItem != serverItem)
                {
                    return;
                }

                Console.WriteLine(string.Format("ChangeSetID:{0}\tFile:{1}\tChangeType:{2}", cs.ChangesetId,change.Item.ServerItem, change.ChangeType));
                if ((change.ChangeType & Microsoft.TeamFoundation.VersionControl.Client.ChangeType.Branch) == Microsoft.TeamFoundation.VersionControl.Client.ChangeType.Branch)
                {
                    var items=vcs.GetBranchHistory(new Microsoft.TeamFoundation.VersionControl.Client.ItemSpec[]{new Microsoft.TeamFoundation.VersionControl.Client.ItemSpec(serverItem, Microsoft.TeamFoundation.VersionControl.Client.RecursionType.None)},
                        Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest);

                    GetAllHistory(items[0][0].Relative.BranchToItem.ServerItem);
                }
            }

        }
    }

【讨论】:

  • 在这段代码中我看不到哪里可以找到每一行代码的所有者。我实现了它,我能看到的唯一所有者是变更集的所有者 (obj.Owner)。
  • 我为您找到了一个有效的在线项目,它解释了如何通过 TFS API 获取注释。实际上它使用的是上面介绍的相同的东西 - 从 GetBranchHistory() 结果中获取 Item[0][0] 的递归数据。然而,实现更复杂。如果许可没问题,您可能希望将该 GitHub 模块包含到您的项目中。 github.com/SonarQubeCommunity/sonar-scm-tfvc
  • 感谢您将我链接到完整项目。它非常复杂/复杂,但我能够使用该 repo 中的一些类来注释每行文件!我接受你的回答,但是当我有时间时,我会发布我完成的代码以及我需要的所有内容。再次感谢!
  • 酷。很高兴听到问题得到解决。我还了解到注释比我想象的要多,这可能是 MS 不为其公开 API 的原因。
猜你喜欢
  • 2019-07-28
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 2013-02-11
相关资源
最近更新 更多