testalino
testalino

Reputation: 5658

TFS API: Request Review on Changeset in C#

In Visual Studio I usually open the Changeset via Source Control Explorer, open the Changeset via Go to Changeset and then select Actions->Request Review in the Team Explorer window where the Changeset is shown.

In C# I have code that lets me query all my changesets:

VersionControlServer vcs = tpc.GetService<VersionControlServer>();
vcs.QueryHistory(...)

Now I have a List of Changeset instances. How can I implement the "Request Review" functionality?

I tried to create a Code Review Request like so:

Project teamProject = _workItemStore.Projects["XYZ"];
WorkItemType workItemType = teamProject.WorkItemTypes["Code Review Request"];
var request = new WorkItem(workItemType) { Title = "Testreview" };
request.Fields["Associated Context Type"].Value = "Changeset";
request.Fields["Associated Context"].Value = "5169";
request.Fields["Assigned To"].Value = "Joe Doe";
request.AreaPath = @"XYZ\Test";
request.IterationPath = @"XYZ\Test\1.5";
request.Save();

This creates a code review request very similar to the one in Visual Studio but Code Review can not be performed. What am I missing?

Upvotes: 1

Views: 979

Answers (1)

Rafael-WO
Rafael-WO

Reputation: 1630

From what I see you are using the package Microsoft.TeamFoundationServer.ExtendedClient. You are on the right track, but you need one more work item called Code Review Response. For the creation of these two work items refer to the blog post Tfs Extensibility - Automatically Create Code Reviews on Checkin. This post helped me a lot regarding the work item field values. Here is the essential code section from the post:

var type = project.WorkItemTypes["Code Review Response"];
var workItem = new WorkItem(type) { Title = checkinNotification.Comment };
workItem.Fields["System.AssignedTo"].Value = "Betty"; //todo pick someone better
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";

var result = workItem.Validate();
foreach (Field item in result)
{
    //insert some form of logging here
}
workItem.Save();

var responseId = workItem.Id;

type = project.WorkItemTypes["Code Review Request"];
workItem = new WorkItem(type) { Title = checkinNotification.Comment };
workItem.Fields["System.AssignedTo"].Value = checkinNotification.ChangesetOwner.DisplayName;
workItem.Fields["Microsoft.VSTS.CodeReview.ContextType"].Value = "Changeset";
workItem.Fields["Microsoft.VSTS.CodeReview.Context"].Value = checkinNotification.Changeset;
workItem.Fields["System.AreaPath"].Value = project.Name; //todo: may want a better location from source path
workItem.Fields["System.IterationPath"].Value = project.Name;
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";
WorkItemLinkTypeEnd linkTypeEnd = workitemStore.WorkItemLinkTypes.LinkTypeEnds["Child"];
workItem.Links.Add(new RelatedLink(linkTypeEnd, responseId));
workItem.Save();

The actual comments on the code review use the discussion service (see Creating code review request through API). Microsoft documentation on the disscusion service: Microsoft.TeamFoundation.Discussion.Client. In this namespace take a look at the class DiscussionThread

I hope this helps.

Upvotes: 2

Related Questions