tag rails, ruby, campfire, subversion, svn

Campfire SVN and email notification

Here's a quick way to add Subversion notification for Campfire using Tinder.

Create svn-campfire.rb with the correct username and password:

#!/usr/local/bin/ruby
require 'rubygems'
require 'tinder'

svnlook = "/usr/local/bin/svnlook"


campfire = Tinder::Campfire.new 'campfiresubdomain'
campfire.login 'user@example.com', 'password'
room = campfire.find_room_by_name('room name')
room.join

if ARGV.size > 1
  revision = ARGV[1]
  path = ARGV[0]
  # we're using this for multiple svn repos so parse the project name from the path
  project = ARGV[0].gsub("/home/user/svn/", '')
  
  author = `#{svnlook} author -r #{revision} #{path}`
  paths  = `#{svnlook} changed -r #{revision} #{path}`   
  log    = `#{svnlook} log -r #{revision} #{path}`
  message = [log,paths].join("\n").strip
  url = "Changeset \##{revision} by #{author} (http://trac.domain.com/trac/#{project}/changeset/#{revision})"

  room.speak(url)
  room.paste(message)
else
  room.speak(ARGV[0])
end

room.leave

Add this to your SVN post-commit hook:

/usr/local/bin/ruby /path/to/svn-campfire.rb "$1" "$2"

Here's a quick way to send emails to campfire. Create an email address to use for sending messages to campfire and then create mailer-campfire.rb with your domain, usernames and passwords:

#!/usr/local/bin/ruby
require 'rubygems'
require 'action_mailer'
require 'tinder'
require 'net/pop'

# setup an email address to use for campfire
Net::POP3.delete_all("domain.com", nil, "user+domain.com", "password") do |m|
  campfire = Tinder::Campfire.new 'campfiresubdomain'
  campfire.login 'user@example.com', 'password'
  room = campfire.find_room_by_name('room name')
  room.join

  begin
    message = TMail::Mail.parse(m.pop)
    
    subject = message.subject if message.subject
    sender = message.from.first if message.from
    body = message.body if message.body
    
    room.speak("I've received an email from #{sender} with the subject '#{subject}'")
    room.paste(body)                
  rescue Exception => e
  end        
    
  room.leave
end

Then create a cron job to fire this off every minute:

* * * * * /usr/local/bin/ruby /home/path/to/mailer-campfire.rb

We also use it to remind us of daily standups:

#!/usr/local/bin/ruby
require 'rubygems'
require 'tinder'

campfire = Tinder::Campfire.new 'campfiresubdomain'
campfire.login 'user@domain.com', 'password'
room = campfire.find_room_by_name('room name')
room.join
room.paste("Time for daily standup.\nWhat did you do yesterday? What are you doing today? Any roadblocks?")    
room.leave

Our cron job fires it off every weekday at 2pm.:

0 14 * * 1-5 /usr/local/bin/ruby /home/path/to/status-campfire.rb

Next step, create a Jabber gateway for Campfire using Tinder.