Showing posts with label hubot. Show all posts
Showing posts with label hubot. Show all posts

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.

Friday, May 4, 2012

Hubot Project Automation

I'd been curious to try Hubot, a GitHub chatbot written in node.js that can interact with users in IRC, Campfire and other chat systems and do any number of tasks which are defined in CoffeeScript. I was interested in the project automation scripts that others had written and contributed to the hubot-scripts repository, such as the teamcity and jira integrations. I was also interested in discovering if having a hubot in our chatroom would encourage the adoption of chat within the project team.

Installation
It took a couple of hours to get set up and working with Campfire. There were a few gotchas and pieces of unclear documentation along the way, but soon enough I had my hubot installed and working, and I tried a few of the contributed scripts.  It didn't help that the server on which I wanted to run Hubot didn't have node.js installed, so some of that time was the node setup.

Hubot Scripts
It's easy to find yourself staring at the github folder of contributed scripts, wondering what they all do. I encourage you to go look at the script catalog instead, which is a little more amenable to skim-reading. A lot of them are simple humor things which weren't really my area of interest for hubot -- I was more interested in fuctional integrations.

The scripts that are there tend to be pretty limited; if you wanted to do anything sophisticated, you're probably going to have to do it yourself. I was hoping for a little more, to be honest.

That said, the scripts look fairly easy to write, and if you're already familiar with CoffeeScript or you're interested in picking it up, I don't think it's going to be a big barrier to entry.  It would be nice to have more sophisticated well-documented examples showing how to do a few things like respond to externally-triggered events.

In any case, much of the things I might want a hubot to do are going to be specific to the projects and environments of a particular project and company, and those things aren't going to be things that others will write.

Automating with Hubot vs. Other Tools
I haven't personally written any Hubot scripts, so I can only speculate at what Hubot would be good at and where other tools would be a better fit.

Looking at the technology and the kinds of integrations that have already been written, I feel like Hubot's basis in CoffeeScript and Node.js give it a lot of affinity with web technologies. That means that integrating with other systems using REST and JSON is pretty easy in a way that wouldn't be for shell scripts. If you're doing a lot of these kinds of integrations, you'd probably find that Hubot's a good fit. If what you really want to do is interact with the filesystem on a server, I suspect you'd find it easier to do that with shell scripts and build tools.

Encouraging Chat Usage
After trying out Hubot briefly, I'm going to have to say that I don't think it's sufficient to really encourage the use of chat. If you're already using a chat room, then being able to invoke those things from the chat room you're already in and in a way that's visible to the rest of the team is potentially worth the trouble.

If you're not in a chat room already, then everything you might have a hubot do is probably simpler to do with shell scripts, build tools (e.g. maven, ant, rake, make), and custom command-line programs (e.g. awesome-cli-ruby) than loading a chat room and using a chat robot.

Thoughts
Hubot is a neat bit of technology. If you're already using team chat, and particularly if you're distributed I'd definitely suggest checking it out. But if you're not using team chat, then I don't think that Hubot will be the driver that will make that happen.