Campfire and Git pushes

Previously when using Campfire, we used a library called Marshmallow, that joins a room and says what commit happened in an SVN repository.  Since moving over to a Git repository, we needed a new bot that would come and tell us what was happening.

I turned to an unofficial Campfire API called Tinder, and wrote a little bit of code to join a single room that has guest access turned on, to say what was just committed to the repository.

The code joins a room, looks for commits in Git, and "speaks".  It runs post-update, so put this in the /git/repo.git/hooks/post-update file.  Don't forget to chmod 770 the post-update file so it can be executed.  This hook is basically what is run when you do a push to the repository.

#!/usr/bin/env ruby

require 'rubygems'
require 'tinder' #require the tinder gem

# room: the subdomain you use for subdomain.campfirenow.com
# path: path to your repository you want to track
# hash: the guest hash, should be about 5 characters long.  you need to turn on guest access in campfire manually first.
# msg: a nice commit message, check the manual for git-log for some flags
#          %an is the author name, %ar is time since the commit, %s is the message, %H is the commit hash
config = {
  :room => 'SUBDOMAIN',
  :path => '/git/repo/path/repo.git',
    :hash => 'GUEST_HASH',
  :msg  => 'Commit by %an (%ar): \"%s\"%nChangeset: http://trac.yoursite.com/changeset/%H'
}

# create new campfire instance with the room
campfire = Tinder::Campfire.new(config[:room])
# get the guest room
room = campfire.find_room_by_guest_hash(config[:hash], 'gitbot')

# get all the commits
commits = `cd #{config[:path]} && git log -#{ARGV.size} --pretty=format:"#{config[:msg]}"`

# say something for each commit
commits.each_line do |commit|
  room.speak "#{commit}"
end

I added some comments above explaining what was going on.  You might also be curious about the msg hash.  The changeset link is going to a modified version of Trac, which I've installed a Git Plugin, so Trac uses our Git repository instead of our SVN repository.  There's probably better tracking systems out there, but we're just reusing what we had before.

Posted