Created
July 29, 2015 23:04
-
-
Save kria/a823bc71bba366382507 to your computer and use it in GitHub Desktop.
TFS subscriber that modifies a work item using impersonation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.TeamFoundation.Client; | |
using Microsoft.TeamFoundation.Common; | |
using Microsoft.TeamFoundation.Framework.Server; | |
using Microsoft.TeamFoundation.WorkItemTracking.Client; | |
using Microsoft.TeamFoundation.WorkItemTracking.Server; | |
using System; | |
using System.Linq; | |
namespace WorkItemModifier | |
{ | |
public class MySubscriber : ISubscriber | |
{ | |
public Type[] SubscribedTypes() | |
{ | |
return new Type[] { typeof(WorkItemChangedEvent) }; | |
} | |
public string Name | |
{ | |
get { return "WorkItemModifier"; } | |
} | |
public SubscriberPriority Priority | |
{ | |
get { return SubscriberPriority.Normal; } | |
} | |
public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType, | |
object notificationEventArgs, out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties) | |
{ | |
statusCode = 0; | |
statusMessage = string.Empty; | |
properties = null; | |
if (notificationType == NotificationType.Notification) | |
{ | |
var ev = notificationEventArgs as WorkItemChangedEvent; | |
TeamFoundationLocationService locationService = requestContext.GetService<TeamFoundationLocationService>(); | |
var tfsUri = locationService.GetSelfReferenceUri(requestContext, locationService.GetDefaultAccessMapping(requestContext)); | |
var changer = Microsoft.TeamFoundation.Framework.Client.IdentityHelper.CreateDescriptorFromSid(ev.ChangerSid); | |
var tfs = new TfsTeamProjectCollection(tfsUri, changer); | |
var store = tfs.GetService<WorkItemStore>(); | |
var workItemId = ev.CoreFields.IntegerFields.Single(f => f.ReferenceName == "System.Id").NewValue; | |
var workItem = store.GetWorkItem(workItemId); | |
var remaining = (double)workItem.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value; | |
if (remaining == 5) | |
{ | |
workItem.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value = 6; | |
} | |
workItem.Save(); // This will generate a new WorkItemChangedEvent, make sure to not create an infinite loop | |
} | |
return EventNotificationStatus.ActionPermitted; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment