Class: HudsonToPissWhistle

Inherits:
Object
  • Object
show all
Defined in:
lib/hudson_to_pisswhistle.rb

Class Method Summary collapse

Class Method Details

.change_dots_to_hyphens_in_keys(hash) ⇒ Object

Hudson’s XML keys contain dots, but BSON doesn’t like that.



8
9
10
11
12
13
# File 'lib/hudson_to_pisswhistle.rb', line 8

def self.change_dots_to_hyphens_in_keys(hash)
  hash.inject({}) do |h, (k,v)|
    h[k.to_s.gsub(".", "-")] = v.is_a?(Hash) ? change_dots_to_hyphens_in_keys(v) : v
    h
  end
end

.run(net_client = HTTParty) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hudson_to_pisswhistle.rb', line 15

def self.run(net_client=HTTParty)
  %w(STREAM OAUTH_TOKEN WORKSPACE BUILD_ID).each do |setting|
    raise "#{setting} was not set" unless ENV[setting]
  end

  build_directory = File.expand_path(File.join(ENV["WORKSPACE"], "..", "builds", ENV["BUILD_ID"]))
  build_log = File.join(build_directory, "build.xml")

  # puts "sleeping while i wait for build.xml to appear"
  x = 0
  until File.exists?(build_log) || x > 10 do
    sleep(1)
    # puts "chekcing for #{build_log}"
    x += 1
  end
  # puts "awake again #{x}"

  data = Crack::XML.parse(File.read(build_log))

  data = change_dots_to_hyphens_in_keys(data)

  data["message"] = File.read(File.join(build_directory, "changelog.xml")).strip

  # legacy CI message compatibility
  data["result"] = data["build"]["result"]

  stream_url = "http://#{ENV["HOST"] || "localhost"}/#{ENV["STREAM"]}/messages"
  query = {:type => "hudson", :oauth_token => ENV["OAUTH_TOKEN"], :payload => data.to_json}
  net_client.post(stream_url, :query => query)
end