Geeks With Blogs

@jakobehn

News

Microsoft Community Contributor Award 2011


Jakob Ehn Visual Studio ALM MVP

** Source available at http://mergeworkitems.codeplex.com/ **


Half a year ago I wrote about about Merging Work Items with a custom check-in policy. The policy evaluated the pending changes and for all pending merges, it traversed the merge history to find the associated work items and let the user add them to the current changeset.

I promised to post the source to the check-in policy (and I’ve got a lot of requests for it), but I never did. This was primary for two reasons:

  1. The technical solution turned out to be a bit complicated. The problem was/is that it is not possible to modify the list of associated work items in the current pending changes using the API. This was a show stopper and the only way around it was to add another component that was executed on the server after the check-in that did the actual association. The information about the selected work items was temporarily stored in the comments of the changeset. This worked, but complicated the deployment.
  2. The feedback internally at Inmeta was that why should the developer be allowed to select which work items that should be associated? If a work item was associated with a changeset in a Main branch, it should always be associated with the merge changeset when merging to a Release branch. So the association should be done automatically.


For these reasons I changed the implementation and converted the check-in policy to a TFS server side event handler instead (for more info on work with these event handlers, check out my post about them here: Server Side Event Handlers in TFS 2010)

By having the association done server side, the process is very smooth for the developer. When a changeset that contains merges is checked in, the event handler evaluates all merges and associates the work items. If a work item has already been associated by the developer, it is of course not associated again. Since it is implemented with a server side event handler, it runs instantly without the user ever really noticing it.

Let’s look at how this works. Lets say that we have the following branch hierarchy:


image

Now, one of our finest developers have both fixed a bug and added a new user story in the Main branch. That was two changesets, each changeset was associated with a corresponding work item:


imageimage

Now, we want to push these changes to the 2.0 Release branch. So the developer performs a merge from Main to the 2.0 branch.

image

At this point, instead of manually adding the work items, the developer just checks in the changes. After this, lets take a look at the source history

image


Double-clicking on the latest changeset, we can see the following information on the work items tab:

image


The work items associated with the original changesets that was merged to the R2.0 branch, have been associated with the new changeset. Also, double-clicking on one of the work items, we can see that the server event handler has added a link to the changeset for this work item:

image



Note the following:

  • The changeset has been linked in the same way as when you associate a work item manually.
  • The change was done by the developer account (myself in this sample), and not the TFS service account. This is because the event handler uses the new TFS Impersonation API to impersonate the user who committed the check-in.
  • The history comment is a bit different than the usual (“Associated with changeset XXX”), just to highlight the reason for the change.
  • If the changeset would contain both merges and other types of pending changes, the merges would still be traversed, the other changes are just ignored.

 

Deployment
Deploying the server side event handler couldn’t be easier, just drop the Inmeta.TFS.MergeWorkItemsEventHandler.dll assembly into the plugin directory of the TFS AT server. This path is usually %PROGRAMFILES%\Microsoft Team Foundation Server 12.0\Application Tier\Web Services\bin\Plugins. Note: This will cause TFS to recycle to load the new assembly, so in production you might want to schedule this to minimize problems for your users.
See my post more details on this.

image



Implementation
I have uploaded the source code to CodePlex at: http://mergeworkitems.codeplex.com/ so you can check out the details there. One thing that is worth mentioning is that I had to resort to the TFS Client API to access and modify the associated work items. The TFS Server Side object model is not very documented, and according to Grant Holiday (in this post) the server object mode for work item tracking is not very useful. So instead of going down that road, I used the client object model to do the work Not as efficient, but it gets the job done.

Note that the event handler hooks into the Notification decision point, which is executed asynchronously after the check-in has been done, and therefor it doesn’t have a negative impact on the overall check-in process.

Hope that you find the event handler useful, contact me either through this blog or via the CodePlex site if you have any questions and/or feature requests.

Posted on Tuesday, May 17, 2011 8:43 PM TFS , Visual Studio 2010 , TFS API , Visual Studio 2013 | Back to top


Comments on this post: Automatically Merging Work Items in TFS 2013

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
You couldn't have posted this at a better time! Thanks!
Left by Thiago on May 17, 2011 9:01 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Ditto! Thanks so much!
Left by M Hamlin on May 18, 2011 4:16 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Your a lifesaver! :)
Left by René Hjorth on May 18, 2011 10:48 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Wow! This is great job! Thank you!
Left by Maxim on May 27, 2011 9:15 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Good Post. Can you locate me the sources to learn TFS programming? I found very less documentation on this.

And, I have as old ASP.NET Issue tracker application to manage all bugs. Now I would like to integrate my legacy application with TFS such a way that, TFS work items should be created automatically, whenever I create a bug in my ASP.NET issue tracker application. If status updated in TFS WI, the same should reflect in ASP.NET application.

Can you suggest me the high level design for this integration?
Left by Pradeep Y on Jun 23, 2011 12:05 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
You should do a blog series explaining getting merge info since all I could find on how to do this was some massive series on what was required for an older TFS version, and TFS2010 MergeSources was always empty. Someone else ran into this:

http://stackoverflow.com/questions/3772674/how-is-the-change-mergesources-field-populated-in-tfs

Here is a summary of the blog series on getting merge info:

http://blogs.msdn.com/b/buckh/archive/2006/02/21/bobmergeapi.aspx
Left by Jed on Jun 30, 2011 8:31 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
I am really looking forward to implementing this. The only question I have is currently we are still running TFS 2008, and although we have in plan to upgrade to TFS 2010 in the next few months, I want to implement this sooner.

Will this work with TFS 2008?
And if so, do I place the Inmeta.TFS.MergeWorkItemsEventHandler.dll in \Microsoft Visual Studio 2008 Team Foundation Server\Web Services\VersionControl\bin?

Thanks a lot in advance.
Left by Fanija on Sep 15, 2011 9:28 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@Fanija: Server event handlers are new in TFS 2010, so this will not work in TFS2008. You could however extract the source and place it in a standard web service event handler that you can create a subscription for. It will not be as immediate as this solution but it should work
Left by Jakob Ehn on Sep 15, 2011 9:48 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Hi,

I have a question. When I resolve a bug in a release branch and merge it into the main branch and later merge it again in the release branch is the work item always associated?

Regards,
Sven
Left by Sven on Oct 18, 2011 11:41 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Awesome post! This should definitely be included as part of the base functionality of TFS...
Left by Larry on Nov 09, 2011 2:35 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Thanks! Works perfect!
Left by nikita on Dec 24, 2011 11:44 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
http://geekswithblogs.net/jakob/archive/2011/05/17/automatically-merging-work-items-in-tfs-2010.aspx

This brings duplicate when you merge the main branch back into the development branch.

is there anyway we can eliminate it?

Thanks,
Mani
Left by Mani on Jan 31, 2012 4:10 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Appreciate your code. It is simply great!
I have a question though. I was debugging the source code of yours. I get the changeset number as -1 . Any idea about this?

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;

if (notificationType == NotificationType.DecisionPoint)
{
try
{
if (notificationEventArgs is CheckinNotification)
{
CheckinNotification notification = notificationEventArgs as CheckinNotification;

Changeset cs = requestContext.GetChangeset (notification.Changeset); // I get changeset number as -1 here
Left by Jaleel on Mar 27, 2012 10:31 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Question?
How is this reflected on Build reports?
I thought a Work Item will only reflect one "Fixed in" build. How then if one Work Item is associated with multiple changesets in different release branchs do you account for what build this was fixed in?
Left by John Ames on Apr 18, 2012 3:40 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@John: If you associate several work items with a changeset, they will all appear in the "Associated Work Items" section of the build summary, and the build will update all work items with the Fixed in build information.

And as you note, every new build will update the same work item with the new build number. If this is not what you want, you can take the route where you simple create a new work item for each branch.
Left by Jakob Ehn on Apr 18, 2012 3:49 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@Jaleel: This is because you are responding to a DecisionPoint event, which is _before_ the checkin is persisted. You should check for NotificationType.Notification instead, which will be executed after the checkin has been stored to the database
Left by Jakob Ehn on Apr 18, 2012 3:52 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Is this addressed in TFS 2012? Any Idea.?
Left by Pradeep Y on Jun 22, 2012 9:57 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Thank you for this contribution to the community. I have implemented the plugin in our Test TFS environment and validated that it will work for our branching/merging scenarios. This small plugin is extremely useful to the company i'm at! I will be sure to follow the development posted at http://tfsbuildextensions.codeplex.com/

Thanks again!
Left by Carl O on Jul 25, 2012 6:17 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@Carl: Thanks, glad that it helped!
Left by Jakob Ehn on Aug 20, 2012 12:25 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@Pradeep: No, thre is no change around this in TFS 2012. I just uploaded a TFS 2012 compatible version to CodePlex
Left by Jakob Ehn on Aug 20, 2012 12:26 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Hi,
This plugin looks like exactly what I need... :)

Is the version available on CodePlex from March this year compatible with both 2010 and 2012?

Cheers.
Left by Town on Dec 17, 2013 3:51 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
@Town: Serverside plugins are bound to the version of the TFS server. I upgraded the Main branch to TFS 2012 in march, but the old version for TFS 2010 is still available in a separate branch called TFS2010. You can either get that branch and build it, or you can download the original 1.0.0.0 version that supports TFS 2010.

Left by Jakob Ehn on Dec 17, 2013 3:56 PM

# Automatically Merging Work Items in TFS 2013 ... 13 anyone?
Requesting Gravatar...
This was the most important plugin in my TFS2010 installation - but now I have upgraded to TFS2013.

How to I a) use this plugin with 2013, b) use this plugin with 2013? :)

Many thanks Jakob - you're a hero!
Left by René Hjorth on Feb 25, 2014 11:21 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Hi Rene, and thanks!

I upgraded the project to TFS 2012 a year ago, but in order to use it with TFS 2013 you need to recompile it against the TFS 2013 object model. I'll see if I get the time to do that this weekend

/Jakob
Left by Jakob Ehn on Feb 28, 2014 5:27 PM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
Thanks in advance, I'll cross my fingers :)
Left by Rene' Hjorth on Mar 03, 2014 8:18 AM

# re: Automatically Merging Work Items in TFS 2010
Requesting Gravatar...
I keep crossing my fingers for a weekend to come, where customers, family or other interests doesn't comes in the way :)
Left by Rene' Hjorth on Mar 10, 2014 1:54 PM

# re: Automatically Merging Work Items in TFS 2013
Requesting Gravatar...
@Rene, it worked, I just uploaded a new release (1.1.0) that supports TFS 2013 :-)
Left by Jakob Ehn on Mar 12, 2014 8:56 AM

# re: Automatically Merging Work Items in TFS 2013
Requesting Gravatar...
Brilliant! Many thanks Jakob - it's really works!
Left by Rene' Hjorth on Mar 20, 2014 9:29 AM

# re: Automatically Merging Work Items in TFS 2013
Requesting Gravatar...
Hi Jackob,

First of all, congratulations for the well done job. It worked perfect in our TFS 2013.
But, in our environment, we have various Git repositories alongside TFVS repos, which of course the developers wanted to use your plugin with. Of course it didn't worked, and digging around in MSDN I've noticed that Git repositories has its own classes in TFS API.
For example, instead check-ins there is pushes, and instead changesets there is commits.
Do you already have plans to implement in your plugin the ability to work in Git repositories? Sorry for my question, that's because I'm an infrastructure specialist, and I'm a newbie in development...

Thanks for your attention, and again, great work!
Left by Vinícius Almeida on Jul 15, 2014 3:55 AM

# re: Automatically Merging Work Items in TFS 2013
Requesting Gravatar...
Works fine also on our TFS 2013.. Really a great feature !

Notice: the username associated with the workitems on the target branch is the tfs build service account when there is a gated build defined on that branch... The actual check-in is indeed executed from the build server.

Is there any trick to retrieve the name of the user who did the initial check-in ?
Left by vletroye on Sep 08, 2014 10:46 AM

# re: Automatically Merging Work Items in TFS 2013
Requesting Gravatar...
@vletroye: Good question, I'll look into that
Left by Jakob Ehn on Nov 18, 2014 10:55 AM

Your comment:
 (will show your gravatar)


Copyright © Jakob Ehn | Powered by: GeeksWithBlogs.net | Join free