06 October 2006

TFS - Team Build - On failed build assign workitem to a specific user


I just came across a post on the MSDN TFS forums that was asking how you go about assigning the workitem created on a failed build to a specific user.  I have also been asked to do this in my organization. 


Team Build will create a new workitem in your Team Project when a build fails.  The "task" is assigned to the account that ran the build.  In my case, that account is a build service account.  One of my Dev Leads asked how we could modify the build process to make the assignment to him whenever one of his builds failed.  I didn't have time to look into it, so I pretty much blew him off.  I recently had some time to look back into this subject.


I figured that I'd probably have to override the CreateWorkItem target to add an additional "AssignedTo" parameter to the call, but after looking through the interface for that task (using the wonderful Reflector program by Lutz Roeder) I came to the realization that the CreateWorkItem task doesn't expose an AssignTo property.  After a little experimentation and research I came to realize that the TFSBuild.proj file has a property called WorkItemFieldValues which is a semi-colon delimited list of work item object property names and values of properties that are not exposed directly by the CreateWorkItem task.  So all you have to do is add an additional name-value pair called "Assigned To" to the property, like so:


   1:      <!--  WorkItemFieldValues
2: Add/edit key value pairs to set
3: values for fields in the work item
4: created
during the build process.
5: Please make sure the field names are
6: valid
for the work item type being
7: used.
8: -->
9: <WorkItemFieldValues>
Assigned To=St. Jean, Steven;
11: Symptom=build break;
12: Steps To Reproduce=Start the build
13: using Team Build
14: </WorkItemFieldValues>

One thing to notice, the "Assigned To" property has a space in the name, "AssignedTo" doesn't work.  Upon first glance, you would think that the Value for the Assigned To property would be the user's credentials in the for DOMAIN\Username but that isn't so.  If you do use the DOMAIN\Username for the Value, a workitem will not be created for the build failure and you will receive the following message in your build log:

MSBUILD : warning : TF42095: A work item could not be created for build failure. An invalid value was passed for field Assigned To.

To get the work item to be assigned correctly you have to use the person's name as it appears in other TFS workitems with the format LastName, FirstName.  So as you can see above, the failed build workitem will be assigned to me using the key-value pair of Assigned To=St. Jean, Steven;.

A complete list of the keys that you can use in this property can be found in the MSDN documentation on the CoreField enumeration.

I hope this saves someone else a bit of time and frustration.

- Steve


adamw said...

Is there any way to generate the workitem without an error in the build. I have failed unit tests within the build and because there is no errors the build fails however it doesn't create the assigned workitem.

Steven St Jean said...

The testing tasks have the ContinueOnError task set to True. This causes any failure in the test to be converted into Warnings which will not fail the build. The only way that I've seen to do what you ask is to modify the task to set ContinueOnError to False so that the failure will be raised. You may also have to add an On Error target to capture the error outside of the normal processing. I don't have the Microsoft targets available to look at right now, so I can't give a more explicit answer than that, sorry.

- Steve

Bruce said...


Thanks for posting this it pointed me in the right direction. I'm actually using the powertoy posted by Buck Hodges to run tests without a Metadata file (vsmdi file).

In my case, in order to assign the created work item to me, I had to put my name in as firstname lastname, not lastname, firstname as you have shown.

Additionally, my failed unit tests were not creating a work item. I took your advice and fiddled with the build file and overrode the standard targets. Rather than pollute your comments with waffle, I've put it on my software development blog

Post a Comment