Skip to content

Instantly share code, notes, and snippets.

@noahd1
Created October 4, 2010 16:56
Show Gist options
  • Save noahd1/610044 to your computer and use it in GitHub Desktop.
Save noahd1/610044 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'curb'
require 'xmlsimple'
# NB: currently throws errors to cron if build server is down
# NB: keeps track of build number on disk, so if build server is reset this script gets screwy
DATA_FILE = File.join(File.dirname(__FILE__), 'build_poller.data')
class Poller
CURRENT_BUILD_URL = "http://username:password@builddomain.com/job/jobname/api/xml?depth=1&xpath=//build[result=%27SUCCESS%27%20or%20result=%27FAILURE%27][1]"
def self.execute
if File.exist?(DATA_FILE)
last_build = Build.new(*File.read(DATA_FILE).split(':'))
else
last_build = Build.empty
end
easy = Curl::Easy.perform(CURRENT_BUILD_URL)
current_build_info = XmlSimple.xml_in(easy.body_str)
current_build = Build.new(current_build_info['number'][0], current_build_info['result'][0])
if current_build.number > last_build.number
if current_build.failed? || current_build.state != last_build.state
current_build.announce
end
current_build.save
end
end
end
class Build
attr_reader :number, :state
def self.empty
Build.new(-1, 'NONE')
end
def initialize(number, state)
@number = number.to_i
@state = state
end
def announce
case @state
when 'SUCCESS'
`afplay /Users/build/hudson/sounds/build_fixed.wav`
`/Users/build/bin/flag.rb 1`
when 'FAILURE'
`afplay /Users/build/hudson/sounds/build_failed.wav`
`/Users/build/bin/flag.rb 0`
else
raise 'Unrecognized state'
end
end
def failed?
state == 'FAILURE'
end
def save
File.open(DATA_FILE, 'w') {|f| f << self.to_s }
end
def to_s
"#{number}:#{state}"
end
end
Poller.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment