Wednesday, 25 June 2008

TFS Automated Build - Mailing the developer

Not strictly identity related, but anything to make my life easier is all good:

A little while ago I asked a question on the TFS forum. It was a simple request to email the person who requested the build when the build was finished, including the status of the build (success/failure).
The answer I received was to implement a web service, with a known interface, and subscribe this service to receive events from the build process. The web service receives a chunk of xml, which I'd need to parse to retrieve the user name. In turn I'd need to look up the email of this user (somehow!). All a bit of pain, frankly. Suffice to say, we never seemed to have the time to do this.

Recently I have been investigating gated builds, and in particular an open source product called OpenGauntlet. I noticed that these guys are emailing developers on start and on finish of their gated builds - brilliant. A quick look through the source code showed me how:

The TFS API is rich in functionality, but the important interface in this case is
IGroupSecurityService. Here's how it can be used to retrieve a user's email address:

// First we need to get our TFS and then find it's build store
TeamFoundationServer server = new TeamFoundationServer( serverUrl, CredentialCache.DefaultCredentials );


// Now get the correct interface
IGroupSecurityService gss = ( IGroupSecurityService ) server.GetService( typeof( IGroupSecurityService ) );


// Retrieve the identity
Identity objIdentity = gss.ReadIdentity( SearchFactor.AccountName, userName, QueryMembership.None );


// Get the email address
string email = objIdentity.MailAddres;

The userName is retrieved like so:

BuildStore store = ( BuildStore ) server.GetService( typeof( BuildStore ) );
// Get the build from our uri
BuildData bd = store.GetBuildDetails( buildUri );

// user name
string userName = buildData.RequestedBy;

Knowing this, it is a simple matter of writing a task to email the developer, passing serverUri, buildUri etc as optional parameters.

Simple.