Quantcast
Viewing all articles
Browse latest Browse all 12

Automatically announce Subversion commits on Yammer with svn2yammer

Image may be NSFW.
Clik here to view.

The staff at ProZ.com use the private social networking tool Yammer to post internal status updates. It’s an easy way to stay in the loop and know what everyone else in the company is up to.

I wrote a PHP script that posts to our Developers group on Yammer every time someone commits to our Subversion repository, to help keep on top of changes in the codebase.

The Yammer message includes the developer’s commit message and the list of changed files, with a link to view the changeset in WebSVN. If a developer wants to comment on the change, he can just reply to the Yammer message.

Here’s an example of a Subversion commit shown in Yammer:

Image may be NSFW.
Clik here to view.

Yammer has an API to support things like this, but it’s not easy to use. See The Many Ways in Which the Yammer API Sucks (pointbeing.net) for a lengthy discussion.

Fortunately, there’s an easier way. Yammer provides an email gateway for posting messages via email. The svn2yammer post-commit script takes advantage of this feature.

<?php
/**
 * Subversion post-commit script to announce commits in a Yammer group.
 *
 * Intended to be called by the Subversion post-commit hook, with the following arguments:
 *   svn2yammer.php {repository path} {revision}
 *
 * See https://www.yammer.com/company/email for more about the Yammer email gateway.
 *
 * @author    Jason Grimes <jg@proz.com>
 * @link      https://github.com/jasongrimes/svn2yammer
 * @copyright Copyright (c) 2011 ProZ.com
 */

/////////  Config  ///////////////

// The email address of the account sending the Yammer message. Must be a confirmed email at Yammer.
// Consider creating a separate Yammer account for SVN, ex. svn@example.com.
$from_email = 'svn@example.com';

// The email address of the group to which the Yammer message should be sent.
$to_email = 'developers+example.com@yammer.com';

// The base URL of your WebSVN server, for clicking through to review commmits.
// The revision and repository parameters are appended automatically.
$websvn_baseurl = 'http://dev.example.com/svn';

// The full path to the svnlook command, installed with Subversion.
$svnlook = '/usr/bin/svnlook';

//////// End config /////////////

// Parse command line args.
$repository = isset($argv[1]) ? $argv[1] : '';
$revision = isset($argv[2]) ? $argv[2] : '';

// Look up info about this commit.
$author = exec($svnlook . ' author ' . $repository);

$output = array();
exec($svnlook . ' changed ' . $repository, $output);
$changed = implode("\n", $output);

$output = array();
exec($svnlook . ' log ' . $repository, $output);
$log = implode("\n", $output);

// Make the Yammer message.
$msg = $author . ' committed ' . $websvn_baseurl . '/revision.php?repname=' . basename($repository) . '&rev=' . $revision;
$msg .= "\n\n";
$msg .= $log . "\n\n" . $changed;

// Send the message.
mail($to_email, "", $msg, 'From: ' . $from_email);

To set it up yourself, follow these steps.

Requirements

  • Yammer and Subversion (of course).
  • PHP on the Subversion server.
  • Ability to create email aliases at your domain (optional).

Step 1: Create an email alias and Yammer account for Subversion (optional). Set up an email alias like svn@example.com. You’ll need to be able to receive email sent to this address. Add it as a Yammer account, and confirm the email address in Yammer. Alternately, Subversion could post to Yammer from your own email address.

Step 2: Turn off confirmation of posts via email. By default, posts via email need to be confirmed. You’ll want to turn this off for the Subversion account. Send a test message to Yammer from the account that will post Subversion commits. Yammer will reply with a “Confirm your post” message. At the bottom of this message, click “To change your notifications settings, go here.” Untick the “confirm” option.

Step 3: Install the commit script. Download svn2yammer from GitHub (or copy/paste it from above). Save it somewhere on your Subversion server. Edit the config options at the top of the script. See the Yammer email gateway instructions to figure out what the “to” address needs to be.

Step 4: Set up the Subversion post-commit hook. Edit the “hooks/post-commit” file in the repository directory on your Subversion server, and add the following (adjusting the paths as necessary):

REPOS="$1"
REV="$2"
/usr/bin/php /path/to/svn2yammer.php $REPOS $REV

That’s it. Now commit something and see if it works.


Viewing all articles
Browse latest Browse all 12

Trending Articles