Team Foundation Server: Subscribing to Policy Overriden Events on Checkin

I demonstrated over a year ago the code required to subscribe to the Policy Override event when developers checkin code, as you know you can apply Checkin policies to ensure developers do things like linking to Work Items, provide Checkin comments, etc. These can of course be "skipped" by Overriding the policy which is valid for scenarios like say 0200 when you need to get the code checked in for a change.

When I first demonstrated it, and plenty of people have demonstrated since, it involved in writing bits of code (not a lot but code nonetheless), things obviously improved as the product developed and here's the much cleaner way of achieving this.

You can simply use bissubscribe.exe to subcribe to events now instead of writing a custom application that used the various Team Foundation Web Services, this command subscribes to the Checkin event and filters on the PolicyOverrideComment - if it's been filled in (which is mandatory) the developer has "skipped" your Checkin Policy at which point you may wish to have words :)

bisSubscribe.exe /eventType CheckinEvent /userId VS2005\Administrator /address darren@domain.com /deliveryType EmailHtml /filter "PolicyOverrideComment <>''"

Note that CheckinEvent is case sensitive - watch out (it had me stumped for a while)

This will cause an email to be sent to darren@domain.com when a developer overrides the policy, you'll get an e-mail containing all of the changeset information including the TFS user in question.

You can obviously filter on anything you like but that will catch all PolicyOverrides, if you then want to have more control about the notification (perhaps IM, custom e-mail, etc. – then you’ll need to do it the Web Service Way:

 

bisSubscribe.exe /eventType CheckinEvent /userId VS2005\Administrator /address https://localhost:8080/CheckInNotificationWS/Service.asmx /deliveryType Soap /filter "PolicyOverrideComment <>''"

 

As you all should know I’m a big fan of strong-typing when it comes to XML so I use XSD/C on the checkinevent schema that you can find here:

C:\Program Files\Microsoft Visual Studio 2005 Team Foundation Server\TF Setup\CheckInEvent.xsd

Then declare any-old Web Service that has a method that looks like this (note the policy override code isn’t required as the bissubscribe tool is doing this for you)

[WebService(Namespace = "https://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service () {

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

    [SoapDocumentMethod(Action = "https://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/02/Notify",

                        RequestNamespace = "https://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/02")]

    [WebMethod]

    public void Notify(string eventXml)

    {

        XmlSerializer xs = new XmlSerializer(typeof(CheckinEvent));

        StringReader sr = new StringReader(eventXml);

        CheckinEvent CheckIn = xs.Deserialize(sr) as CheckinEvent;

        if (CheckIn != null)

        {

            // Were there any Policy Failures during this checkin?

            if (CheckIn.PolicyFailures.Length > 0)

            {

            }

        }

    }    

Enjoy!