Thursday, May 24, 2012

Getting Hubot to Chat Commits to Subversion

If you happen to be using Hubot and Subversion together, you might want Hubot to chat subversion commit messages. (Of course, you might also want to switch to git or mercurial, but that's another conversation).  I wanted to do that, so I explored the option briefly.

Listening with Hubot
Hubot listens for HTTP requests on a port (depending on your configuration). Making a new hubot script to respond to certain requests is easy. You might start with something like this:
module.exports = (robot) ->
  robot.router.post "/hubot/svn", (req, res) ->

    revision = req.body.revision
    username = req.body.username
    log = req.body.log

    robot.logger.info "Subversion revision #{revision} committed."

    room = <room id>
    message = "Subversion revision #{revision} committed."

    robot.messageRoom room, message

    res.writeHead 200, {'Content-Type': 'text/plain'}
    res.end "Thanks; subversion commit written to chat.\n"

Reacting to a Subversion Commit
Now that you've got a listener, you'll want to send an HTTP request when a commit has been made. You can read more about svn post-commit hooks, but basically you add a script under your repo path as hooks/post-commit (or you modify an existing one, if you already have some post-commit hooks).

You might add something like this:
curl <server>:<port>/hubot/svn --data "revision=$2"

The SVN post-commit hook only has access to the repo path and the revision number. That's a decent starting point, but you're likely going to want to add more information.

Getting More Information
Assuming you want more information, you can either get that information before making the HTTP request or after (assuming Hubot can get access to your subversion repository).

If you want to get the information before making the HTTP request, you might revise your post-commit hook to do something like this:
revision=$2
log=$(svnlook log /opt/svn/repo -r $revision)
author=$(svnlook author /opt/svn/repo -r $revision)
curl <server>:<port>/hubot/svn --data "revision=$revision" --data "log=$log" --data "username=$author"

You could put that in an external script if you prefer to keep your post-commit hook clean. You would need to modify your Hubot script as well:
module.exports = (robot) ->
  robot.router.post "/hubot/svn", (req, res) ->

    revision = req.body.revision
    username = req.body.username
    log = req.body.log

    robot.logger.info "Subversion revision #{revision} committed."

    room = <room id>
    message = "Subversion revision #{revision} committed by #{username}:\n#{log}"

    robot.messageRoom room, message

    res.writeHead 200, {'Content-Type': 'text/plain'}
    res.end "Thanks; subversion commit written to chat.\n"

Alternately, you could simply pass the revision number to Hubot and then have Hubot retrieve the author's username and log message.

Et Voila
Now you have Hubot relaying information about subversion commits to your chatroom. Nice. Next stop, your continuous integration server.

No comments:

Post a Comment