How to Enforce Check-In Policies

So one of the benefits of using Server side Plugins is that you can't override them. The problem with this is that the users don't know that the policy is in place until they check-in their code. Wouldn't it be nice if me as a user were told this is what I need to do as in see the requirements from check-in policies but then to make the administrators or business rules that require the policies to also be happy.
📅 10 Jan 2014

So one of the benefits of using Server side Plugins is that you can't override them. The problem with this is that the users don't know that the policy is in place until they check-in their code. Wouldn't it be nice if me as a user were told this is what I need to do as in see the requirements from check-in policies but then to make the administrators or business rules that require the policies to also be happy.

One way that you could probably achieve this is to create a check-in policy and then create a server plugin with the same logic, this seems like double work and implementing this for each policy will become annoying. What about the policies that you get from Visual Studio Gallery like the one from Colin Dembovsky called Colin's ALM Checkin Policies for VS 2013, you'd have to request the source code and then each time there is an update you'd have to update your code.

A much easier way is to create one simple server plugin that will enforce that any check-in policies are met. To achieve this you can use the source code below as a server plugin

namespace TFS.EnforceCheckInPolicies
{
using System;
using System.Diagnostics;

using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.VersionControl.Server;

public class EnforceCheckInPoliciesSubscriber : ISubscriber
{
public string Name
{
get
{
return "TFS.EnforceCheckInPolicies";
}
}

public SubscriberPriority Priority
{
get
{
return SubscriberPriority.High;
}
}

public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties)
{
statusCode = 0;
properties = null;
statusMessage = string.Empty;

try
{
if (notificationType == NotificationType.DecisionPoint &&
notificationEventArgs is CheckinNotification)
{
var args = notificationEventArgs as
CheckinNotification;
if (args.PolicyOverrideInfo.PolicyFailures.Length > 0)
{
statusMessage = "Policy Overriding is not allowed.";
return EventNotificationStatus.ActionDenied;
}
}
return EventNotificationStatus.ActionPermitted;
}
catch (Exception ex)
{
// log the error and fail the check in
statusMessage = "Error in plugin '" + this.Name + "', error details: "
+ ex;
EventLog.WriteEntry("TFS Service", statusMessage,
EventLogEntryType.Error);
return EventNotificationStatus.ActionDenied;
}
}

public Type[] SubscribedTypes()
{
return new[] { typeof(CheckinNotification) };
}
}
}

This code sample at the moment will work against any Team Project in the instance you deploy it to. You can add code in if you need it to only work against certain collections or projects.

Basically what will happen now is you will get an error when not doing what is required by the check-in policy and then will have to override it

image

Except when you click check In you get another unexpected message

image

You'll never have to worry about policies being overridden again. Obviously this doesn't allow for the occasions where you would want to allow for overriding but you can build logic in that would then accept the override in certain situations.

You can get the code from GitHub  using the url https://github.com/Gordon-Beeming/TFS.ServerPlugins.