Trying to get who wrote the code by looking at the file name and its line number in the project using TFS

I am trying to write a small application that will use the TFS API.

I will have a method, and this method takes three parameters, such as project name, file name and line number. He then gives me the name of the person who wrote this piece of code.

public string GetProgrammer(string projectname, string file, int linenumber) { //implementation return programmerName; } 

I did a search for the TFS API, but I could not find the exact information to solve this problem. Does the TFS API provide this information?

Thanks in advance,

+4
source share
1 answer

Great question. However, there is no direct method that you could use for this. Instead, you will need to do the following:

 public string GetProgrammer(string projectname, string file, int linenumber) { // 1. Connect To TFS get the project name that you have passed var tfs = TfsTeamProjectCollectionFactory .GetTeamProjectCollection(new Uri("TfsUrl")); var vsStore = tfs.GetService<VersionControlServer>(); var myProject = vsStore.TryGetTeamProject(projectName); // 2. Use the versionControlServer Service and get a history // ie all changesets that fall under the parent 'filename' // with recursion.none var histories = service.GetBranchHistory( new ItemSpec[] { new ItemSpec (filePath, RecursionType.None) }, VersionSpec.Latest); // 3. Loop through each changeset and build your code block adding // the name of the user who owns the changeset in a List. foreach (BranchHistoryTreeItem history in histories[0]) { var change = service.GetChangeset( history.Relative.BranchToItem.ChangesetId, true, true); if (change.WorkItems.ToList().Count == 0) { // Create your file compiling the changes from each check-in // and lets say store in stream } } // 4. Now pass the line number, in what ever code block it falls // you should get the details of the user, changeset and other details // to return. // Query the stream build in the last step for the line number return programmerName; } 

Some unresolved issues - Several times the same line, or rather the same block of code, is modified by several users when developing a file. How do you plan to handle this?

Look at these blog posts that can help you get started connecting to TFS, using versionControlServer to retrieve changesets for a file and create loops using changesets. http://geekswithblogs.net/TarunArora/category/12804.aspx

NTN Greetings, Tarun

+6
source

All Articles