0 Comments

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.

0 Comments

The other night while watching the Visual Studio 2013 Launch a question came up asking if the team had added support yet for automatically linking work items to the change set created  when checking in as it currently is in Git. Currently this functionality doesn't exist for TFVC Check ins, at the time I didn't think anything of it but tonight for some reason I thought "hey that would be something simply to create and should hopefully make lives easier for developers". So I opened up Visual Studio and at the same time started a new project on GitHub for TFS Server Plugins. The code required for the plugin was very basic, basically just checked the comment from the check in with a regular expression and then added the links between the change set and all the IDs found in the comments.

Example

If you have a comment that says "Changed all site references from Team Foundation Service to Visual Studio Online for item #365", this will when checked in go and try find a work item with the id 365 and if found it will add a link between work item 365 and the check ins change set number.

Getting the code

The code can be found on GitHub at https://github.com/Gordon-Beeming/TFS.ServerPlugins. Just download and compile the TFS.AutoAttachWorkItemIDs project and then copy the TFS.AutoAttachWorkItemIDs.dll and TfsApi.dll into the Plugins folder on your TFS server which is C:\Program Files\Microsoft Team Foundation Server 12.0\Application Tier\Web Services\bin\Plugins for TFS 2013.

What version of TFS will this work with?

This plugin will work with most of the latest TFS version however you will need to retarget the references for versions before TFS 2013.

Downloading the binaries

You can download the compiled binaries targeting 2013 assemblies from http://sdrv.ms/1aKeTbk.

 

Happy Deving Smile