Programmatically get TFS blame (annotation) data

2019-04-28 11:40发布

问题:

I'm trying to implement a plugin for Team Foundation Server 2010 that will create reports about users in a team project. Conceptually, all I need in order to properly implement this plugin is access to the same data that you get when you use the "Annotate" feature in Visual Studio: I need to be able to tell who was the last person to author a given line of code.

I've scoured the Internet for documentation or code samples, but all that I can find are either suggestions such as using the TFS command-line tools or seemingly incomplete code samples.

I don't mind doing a lot of heavy lifting in the client code, but there doesn't seem to be an obvious way to get useful authorship data about the contents of the code in a Changeset, nor from the merge details return.

回答1:

Meanwhile I found a working solution that executes Team Foundation Power Tools process and parses its output:

private readonly Regex m_Regex = new Regex(@"^(?<changeset>\d+)(?<codeLine>.*)", RegexOptions.Compiled | RegexOptions.Multiline);

public List<Changeset> GetAnnotations(string filepath, string codeText)
    {
        var versionControlServer = CreateVersionControlServer();

        return m_Regex.Matches(ExecutePowerTools(filepath))
            .Cast<Match>()
            .Where(m => m.Groups["codeLine"].Value.Contains(codeText))
            .Select(v => versionControlServer.GetChangeset(int.Parse(v.Groups["changeset"].Value), false, false))
            .ToList();
    }

    private static VersionControlServer CreateVersionControlServer()
    {
        var projectCollection = new TfsTeamProjectCollection(new Uri(@"TFS URL"));
        var versionControlServer = projectCollection.GetService<VersionControlServer>();
        return versionControlServer;
    }

    private static string ExecutePowerTools(string filepath)
    {
        using (var process = Process.Start(TfptLocation, string.Format("annotate /noprompt {0}", filepath)))
        {
            process.WaitForExit();
            return process.StandardOutput.ReadToEnd();
        }
    }


回答2:

I had very similar requirement to get details of particular attribute in a file e.g. who added, when, related work items etc.; Following GitHub project is having implementation to get required details and required minimal changes to work-

SonarQube SCM TFVC plugin

It requires analysis to be executed from Windows machines with the Team Foundation Server Object Model installed (download for TFS 2013).