Using Tfs API, How to Find the Comments Which Were Made on a Code Review

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

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.

Get Code Review comments history from TFS 2018

This case provides a solution, you can check it:

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.

Code snippet is as below:

using Microsoft.TeamFoundation.Discussion.Client;
using System;
using System.Collections.Generic;

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

Uri uri = new Uri("http://tfs2018:8080/tfs/defaultcollection");
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);
Console.WriteLine(comment.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; }
}
}
}

Get TFS code review comments report given for files

Neither Work item query nor rest API is able to retrieve code review comments for now. However you can achieve your goal by using TFS API.

Specifically the comments are accessible via the DiscussionThread class. Just need to query discussions using IDiscussionManager.
Sample code as below:

 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; }
}

More details please refer this similar question: Using TFS API, how can I find the comments which were made on a Code Review?

Using TFS API, how can I find the status of code review like Looks Good, Needs work?

There are two types of work items comes to code reviews: Code Review Request
,Code Review Response.

When you ask for a review, TFS creates a Code Review Request, and then
behind the scenes creates one Code Review Response for every person
you’ve asked to review my work. So if you tag Alice, Bob, and Charlie
on my review, there are four work items created for me: one request
plus three responses.

The glue is something called a Related Link. The parent review
relates to the children responses and vice versa. This simplifies
things a bit because once you have the review, you can easily
determine the response work items based on their IDs.

The code you are trying to get the work item is Code Review Request. What you want to find is a filed called Closed Status in Code Review Response:

The status selected by the reviewer when closing the code review
request. The number is stored in the system and written to the data
warehouse as follows:

  • 0 – Not Reviewed
  • 1 - Looks Good
  • 2 - With Comments
  • 3- Needs Work
  • 4 - Declined
  • 5 - Removed

Reference name=Microsoft.VSTS.CodeReview.ClosedStatus

Source Link

You could use WIQL combine with TFS API to fetch what you need. Details steps please refer this tutorial: Getting Code Review Statistics Programmatically from TFS

More example of accessing code reviews with TFS API is shown here: Using TFS API, how can I find the comments which were made on a Code Review?

how to pull code review reports from TFSGIT

You can pull data from TFS with Rest API.

To get the PR code comments you can use the Pull Request Threads - List.

The request is:

https://{instance}/{collection}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/threads?api-version=4.1

In the JSON response, you will get the comments and the comment location in the code (line number):

"comments": [
{
"id": 1,
"parentCommentId": 0,
"author": {
"displayName": "Shayki Abramczyk",
"url": "https://spsprodweu3.vssps.visualstudio.com/Ac256a93d-7cea-4070-xxxxxxxxx/_apis/Identities/7a9a9b44-a2f1-6dfd-a7f6-xxxxxxxxxx",
"_links": {
"avatar": {
"href": "https://dev.azure.com/xxxxxx/_apis/GraphProfile/MemberAvatars/msa.N2E5YTlxxxxxxxxxxxxxxx"
}
},
"id": "7a9a9b44-a2f1-6dfd-a7f6-xxxxxxxxxx",
"uniqueName": "xxxxxxxx",
"imageUrl": "https://dev.azure.com/xxxxxxxx/_api/_common/identityImage?id=7a9a9b44-a2f1-6dfd-a7f6-xxxxxxxxxx",
"descriptor": "msa.N2E5YTliNDQtYTJmMS03ZGZkLWE3Zjxxxxxxxxxxx"
},
"content": "test comment",
"publishedDate": "2019-02-25T11:11:03.45Z",
"lastUpdatedDate": "2019-02-25T11:11:03.45Z",
"lastContentUpdatedDate": "2019-02-25T11:11:03.45Z",
"commentType": "text",
"usersLiked": [

]
}
],
"status": "active",
"threadContext": {
"filePath": "/SampleForVSTS/Program.cs",
"leftFileStart": {
"line": 14,
"offset": 1
},
"leftFileEnd": {
"line": 14,
"offset": 10
}
},

As you can see there is a comment test comment at line 14 in the file SampleForVSTS/Program.cs.

You can write simple code in any language to get the data with Rest API.

Team foundation server - retrieving review comments

Work item query is not able to retrieve code review comments, you can achieve your goal by using TFS API. This case provides a solution, you can check it:

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.

Code snippet is as below:

 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; }
}

How to retrieve all code reviews in My Work using Azure DevOps REST API

You could use Wiql - Query By Wiql API like below to get the code reviews:

    POST https://dev.azure.com/{organization}/{project}/_apis/wit/wiql?api-version=5.1

# get all code review that I request
{
"query": "SELECT [System.Id], [System.Links.LinkType], [System.Title], [System.State], [System.Reason], [System.AssignedTo] FROM WorkItemLinks WHERE (Source.[System.TeamProject] = @project and Source.[System.WorkItemType] in group 'Microsoft.CodeReviewRequestCategory' and Source.[System.AssignedTo] = @me and Source.[Microsoft.VSTS.Common.StateCode] <> '1') and ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') and (Target.[System.WorkItemType] in group 'Microsoft.CodeReviewResponseCategory') ORDER BY [System.CreatedDate] desc"
}

# get all code review that I you are a reviewer

{
"query": "SELECT [System.Id], [System.Links.LinkType], [System.Title], [System.State], [System.Reason], [System.AssignedTo] FROM WorkItemLinks WHERE ([Source].[System.TeamProject] = @project AND [Source].[System.WorkItemType] IN GROUP 'Microsoft.CodeReviewRequestCategory' AND [Source].[Microsoft.VSTS.Common.StateCode] <> 1) And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ([Target].[System.WorkItemType] IN GROUP 'Microsoft.CodeReviewResponseCategory' AND [Target].[System.AssignedTo] = @me) ORDER BY [System.CreatedDate] desc"
}

Find which WorkItems are Code Reviews using the TFS API

You could use the method below to get a workitem's type. But you need to provide the workitem id.

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

Uri collectionUri = (args.Length < 1) ?
new Uri("http://servername:8080/tfs/MyCollection") : new Uri(args[0]);

// Connect to the server and the store.
TfsTeamProjectCollection teamProjectCollection =
new TfsTeamProjectCollection(collectionUri);

WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
string queryString = "Select [State], [Title],[Description] From WorkItems Where [Work Item Type] = 'Code Review Request' or [Work Item Type] = 'Code Review Response'";

// Create and run the query.
Query query = new Query(workItemStore, queryString);
WorkItemCollection witCollection = query.RunQuery();

foreach (WorkItem workItem in witCollection)
{
workItem.Open();
Console.WriteLine(workItem.Fields["Title"].Value.ToString());

}


Related Topics



Leave a reply



Submit