In 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.
You need to check if your Git Commit includes a meaningful commit message.
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
2. Add these references to this library
You can find all assemblies in the Team Foundation Server folder. Ensure that they all have the same version (in my case version 18.104.22.168).
3. Create a class that implements the ISubscriber interface
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.
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
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.
 MSDN Documentation: http://msdn.microsoft.com/en-us/vstudio/microsoft.teamfoundation.git.server.pushnotification(v=vs.95).aspx
 TFS Handling with Subscribers: http://nakedalm.com/team-foundation-server-2010-event-handling-with-subscribers/