Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to Enforce Check-In Policies

, 10 Jan 2014
Rate this:
Please Sign up or sign in to vote.
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

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.

License

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

Share

About the Author

Gordon W Beeming
Software Developer Derivco
South Africa South Africa
Developer who loves pushing the limits and playing with amazing @Microsoft Tech, #TFS, ALM Rangers, MVP, FoRG.
 
http://31og.com
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Answergood article PinmemberMember 1052195013-Jan-14 13:45 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 10 Jan 2014
Article Copyright 2014 by Gordon W Beeming
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid