TFS Server-side check-in policy for git repositories

TFSlovesGitIn many companies it is common practice that source code has to pass some tests right before it will be checked in. This is done by TFS check-in policies. And if you want these check-in policies enforced for everyone you have to create a server plugin. With Team Foundation Version Control you can write an event handler for check-ins. But how can you do that with TFS and Git repositories? Normally you would write a server-side hook, but this won’t work with Team Foundation Server. The solution is as simple as for TF Version Control. You can also write a plugin that receives a server side event and in this post I will show you how.

Problem:

You need to check if your Git Commit includes a meaningful commit message.

Answer:

Create a Team Foundation Server event handler that checks your TfsGitCommit for the commit message. All you have to do is to follow these steps:

1. Create a new class library project

PushNotification_1

2. Add these references to this library

PushNotification_2

You can find all assemblies in the Team Foundation Server folder. Ensure that they all have the same version (in my case version 12.0.0.0).

3. Create a class that implements the ISubscriber interface

PushNotification_3

Caution: Be sure that the class name for your event handler is unique.

4. Implement the ProcessEvent method to catch the git push event

For catching the push event you will have to subscribe to a notification called PushNotification. The PushNotification class is part of the Microsoft.TeamFoundation.Git.Server assembly.

PushNotification_4

If you want to check for the commit message you need to do following steps:

1 – get the repository service out of the TeamFoundationRequestContext

2 – load the repository by Id

3 – get all commits in the IncludedCommits property of the PushNotification

4 – Look up the TfsGitCommit and get the comment

Caution: The ProcessEvent method would likely called twice, once for the decision and if the process is permitted to complete once for the notification.

5. Check the comment for a meaningful message

When you get the commit message you can check if the message string contains an Id of a TFS Work Item or a number of an item of any other external tracking system. When the result is not as expected you can prevent the push to the repository by setting the EventNotificationStatus to ActionDenied.

There a three different values for the EventNotificationStatus

  • ActionDenied
  • ActionPermitted
  • ActionApproved

6. Drop the assembly in the plugins directory on your server

In a final step you have to drop the assembly in the plugins directory on your server C:\Program Files\Microsoft Team Foundation Server 12.0\Application Tier\Web Services\bin\Plugins. Then recycle your app pool and the plugin will be activated.

Caution: Be sure that you don’t drop the assembly twice e.g. with different names

 

Thanks to Kevin Resnick and Matthew Mitrik from Microsoft for supporting me in this issue.

Usually I write all my postings for the German-speaking community. But since there is no information out there I wanted to share this information with everyone. Have fun.

 

[1] MSDN Documentation: http://msdn.microsoft.com/en-us/vstudio/microsoft.teamfoundation.git.server.pushnotification(v=vs.95).aspx

[2] TFS Handling with Subscribers: http://nakedalm.com/team-foundation-server-2010-event-handling-with-subscribers/

 

2 Gedanken zu “TFS Server-side check-in policy for git repositories

  1. Great article. ..And thanks for writing it in English. I got this link from Matthew Mitrik and I’m very glad that I did. It will certainly be useful. I’ve been hunting quite a bit for this info!

    Thanks!

    • You’re welcome! I know what you mean I had the same problem.

Die Kommentarfunktion ist geschlossen.