Created
December 29, 2010 23:21
-
-
Save semanticart/759205 to your computer and use it in GitHub Desktop.
a ruby script for speaking the hudson status in campfire with win/fail images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a little messy but it works. | |
# ran as a post-build task: ruby /path/to/script/campfire.rb 2>&1 >/dev/null & | |
require 'rubygems' | |
require 'uri' | |
require 'net/http' | |
require 'tinder' | |
IMAGE_TRANSLATION = { 'OK' => 'win', 'FAILURE' => 'fail' } | |
class BuildInfo | |
attr_accessor :build, :most_recent_build, :status | |
TRANSLATION = { 'SUCCESS' => "OK" } | |
def initialize | |
@build = Dir.pwd.match(/jobs\/(.*?)\/workspace/)[1] | |
@most_recent_build = Dir.entries(Dir.pwd + "/../builds/").reject{|c| c.match(/[\.-]/)}.map(&:to_i).max | |
@status = determine_status | |
end | |
def failure_or_new_success? | |
@status != 'OK' || last_status != 'OK' | |
end | |
def determine_status | |
last_line = '' | |
sleep_count = 0 | |
while last_line !~ /^Finished:/ | |
sleep 1 | |
sleep_count += 1 | |
exit if sleep_count > 60 | |
last_line = File.readlines(Dir.pwd + "/../builds/#{@most_recent_build}/log").last.strip | |
end | |
the_status = last_line.split(' ').last | |
TRANSLATION[the_status] || the_status | |
end | |
def last_status_file | |
"last_status_#{@build}.txt" | |
end | |
def last_status | |
if File.exist?(last_status_file) | |
return File.read(last_status_file) | |
end | |
end | |
def write_status | |
File.open(last_status_file, 'w'){|f| f.print @status} | |
end | |
def log_url | |
"http://hudson.test.patientslikeme.com/job/#{@build}/#{@most_recent_build}/consoleText" | |
end | |
end | |
# grab the image once so everyone sees the same thing | |
def random_image(my_status) | |
kind = IMAGE_TRANSLATION[my_status] | |
if kind | |
Net::HTTP.get(URI.parse("http://buildmacros.semanticart.com/random/#{kind}.txt")) rescue nil | |
end | |
end | |
info = BuildInfo.new | |
campfire = Tinder::Campfire.new 'plm', :token => '_YOUR_TOKEN_HERE' | |
room = campfire.rooms.detect{|room| room.name == "YOUR CAMPFIRE ROOM"} | |
room.speak "#{info.build} #{info.status}: #{info.log_url}" | |
# only send a message if it is a broken build or if the build is newly fixed | |
# i.e. berate often but praise rarely :) | |
if info.failure_or_new_success? | |
my_random_image = random_image(info.status) | |
room.speak my_random_image if my_random_image | |
end | |
info.write_status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment