Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2013 04:28
Show Gist options
  • Select an option

  • Save anonymous/5828803 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5828803 to your computer and use it in GitHub Desktop.
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Enlighten.Tfs.Subscribers.CodeReviewCreator
{
public class CodeReviewCreator : ISubscriber
{
public string Name
{
get { return "Automated Code Review Creation"; }
}
public SubscriberPriority Priority
{
get { return SubscriberPriority.Normal; }
}
public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out Microsoft.TeamFoundation.Common.ExceptionPropertyCollection properties)
{
statusCode = 0;
statusMessage = string.Empty;
properties = null;
try
{
if (notificationType == NotificationType.Notification && notificationEventArgs is Microsoft.TeamFoundation.VersionControl.Server.CheckinNotification)
{
var checkinNotification = notificationEventArgs as Microsoft.TeamFoundation.VersionControl.Server.CheckinNotification;
//5% chance to create code review - probably want something a bit more complex
if ((new Random()).NextDouble() <= 0.05)
{
TeamFoundationLocationService service = requestContext.GetService<TeamFoundationLocationService>();
Uri selfReferenceUri = service.GetSelfReferenceUri(requestContext, service.GetDefaultAccessMapping(requestContext));
TfsTeamProjectCollection tfsTeamProjectCollection = new TfsTeamProjectCollection(selfReferenceUri);
// Look up the user that we want to impersonate
IIdentityManagementService identityManagementService = tfsTeamProjectCollection.GetService<IIdentityManagementService>();
Microsoft.TeamFoundation.Framework.Client.TeamFoundationIdentity identity = identityManagementService.ReadIdentity(IdentitySearchFactor.AccountName, checkinNotification.ChangesetOwner.UniqueName, MembershipQuery.None, ReadIdentityOptions.None);
TfsTeamProjectCollection impersonatedCollection = new TfsTeamProjectCollection(tfsTeamProjectCollection.Uri, identity.Descriptor);
var workitemStore = impersonatedCollection.GetService<WorkItemStore>();
var project = workitemStore.Projects["Enlighten"]; //todo get project name from checkinNotification.GetSubmittedItems()
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 they're put into root of project, 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));
//todo copy related workitems from the checkin
result = workItem.Validate();
foreach (Field item in result)
{
//insert some form of logging here
}
workItem.Save();
}
}
}
catch (Exception e)
{
//insert some form of logging here
}
return EventNotificationStatus.ActionPermitted;
}
public Type[] SubscribedTypes()
{
return new Type[] { typeof(Microsoft.TeamFoundation.VersionControl.Server.CheckinNotification) };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment