Click here to Skip to main content
15,888,089 members
Articles / Programming Languages / C#

How to Enforce Check-In Policies

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Jan 2014CPOL2 min read 9.7K   3
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.

One way that you could probably achieve this is to create a check-in policy and then create a server plug in 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 plug in that will enforce that any check-in policies are met. To achieve this, you can use the source code below as a server plug in:

C#
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 plug in '" + 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.

This article was originally posted at http://31og.com/post/how-to-enforce-check-in-policies

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SSW
South Africa South Africa

Comments and Discussions

 
QuestionHow can I install the Enforce Check-In Policies in the server Pin
Member 138983004-Jul-18 3:33
Member 138983004-Jul-18 3:33 
QuestionInstall project in server Pin
Member 138983004-Jul-18 3:21
Member 138983004-Jul-18 3:21 
Answergood article Pin
nashoa13-Jan-14 13:45
nashoa13-Jan-14 13:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.