Using TFS API, how can I find the comments which were made on a Code Review?
Asked Answered
C

2

18

I'm trying to figure out a way to find details about a Code Review Request / Response item in TFS2012.

I can query for all Code Review Request/Response items in the following way:

const string TfsUri = "http://mytfsserver:8080/tfs/Default ProjectCollection";

var tfs = new TfsTeamProjectCollection(new Uri(TfsUri));
var store = tfs.GetService<WorkItemStore>();

var versionStore = tfs.GetService<Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer>();

var queryText = "SELECT [System.Id], 
                 FROM WorkItems 
                 WHERE [System.WorkItemType] = 'Code Review Request' 
                 or [System.WorkItemType] = 'Code Review Response'";
var query = new Query(store, queryText);

var result = query.RunQuery().OfType<WorkItem>();

This gives me a list of WorkItem types. When I loop over the result.FirstOrDefault().Fields property, it does give me some usefull information about the ShelveSet which is related to the Code Review, the "Associated Context". Using this information, I can query for the ShelveSet:

var versionStore = tfs.GetService<VersionControlServer>();
var shelveset = versionStore.QueryShelvesets("someCodeReviewId_xxxx","someUserName");

this gives me a ShelveSet item, but that's where I get stuck.

I've looked into the Microsoft.TeamFoundation.CodeReview namespace provided by both Microsoft.TeamFoundation.CodeReview.Components and Microsoft.TeamFoundation.CodeReview.Controls libraries, but this doesn't help me further either.

My question is: How can I find the actual comments made on a ShelveSet during a Code Review (both General comments and File comments) through the TFS API?

Comyns answered 17/4, 2013 at 14:50 Comment(0)
T
13

I don't have code examples, but according to this discussion, you should be able to get to code review comments with functionality in the Microsoft.TeamFoundation.Discussion.Client namespace.

Specifically the comments are accessible via the DiscussionThread class. And you should be able to query discussions using IDiscussionManager.

Titration answered 8/5, 2013 at 7:32 Comment(0)
T
17

We have a new requirement to pull code review comments from TFS and here is a short example of what we implemented. The workItemId has to be queried through another method. You can even look it up in Visual Studio or through a TFS query in the UI. This is a small subset of what is available and what we are using. I found this link to be helpful after digging through MSDN.

 public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
 {
        List<CodeReviewComment> comments = new List<CodeReviewComment>();

        Uri uri = new Uri(URL_TO_TFS_COLLECTION);
        TeamFoundationDiscussionService service = new TeamFoundationDiscussionService();
        service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(uri));
        IDiscussionManager discussionManager = service.CreateDiscussionManager();

        IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null);
        var output = discussionManager.EndQueryByCodeReviewRequest(result);

        foreach (DiscussionThread thread in output)
        {
            if (thread.RootComment != null)
            {
                CodeReviewComment comment = new CodeReviewComment();
                comment.Author = thread.RootComment.Author.DisplayName;
                comment.Comment = thread.RootComment.Content;
                comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString();
                comment.ItemName = thread.ItemPath;
                comments.Add(comment);
            }
        }

        return comments;
    }

    static void CallCompletedCallback(IAsyncResult result)
    {
        // Handle error conditions here
    }

    public class CodeReviewComment
    {
        public string Author { get; set; }
        public string Comment { get; set; }
        public string PublishDate { get; set; }
        public string ItemName { get; set; }
    }
Tulipwood answered 29/10, 2013 at 19:19 Comment(2)
I am running this on thousands of code review request. Any way to speed this up? This step is taking time: var output = discussionManager.EndQueryByCodeReviewRequest(result);Vat
This step has always run pretty fast for us since it uses a single Work Item Id (workItemId) as an input parameter. We set up a web app that lists all of the changesets for the given criteria and highlights the ones with code reviews. Then we click on each one that we want to drill down on which leads to the step in this example.Tulipwood
T
13

I don't have code examples, but according to this discussion, you should be able to get to code review comments with functionality in the Microsoft.TeamFoundation.Discussion.Client namespace.

Specifically the comments are accessible via the DiscussionThread class. And you should be able to query discussions using IDiscussionManager.

Titration answered 8/5, 2013 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.