Class: Lita::Handlers::Gerrit

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/gerrit.rb

Instance Method Summary collapse

Instance Method Details

#build_notification(request, response) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lita/handlers/gerrit.rb', line 81

def build_notification(request, response)
  target = Source.new(room: request.env["router.params"][:room])
  notification = MultiJson.load(request.body.read)
  build = notification["build"]
  params = build["parameters"]

  message = "[jenkins] [#{params["GERRIT_PROJECT"]}] Build %s for \"#{params["GERRIT_CHANGE_SUBJECT"]}\" by #{params["GERRIT_PATCHSET_UPLOADER_NAME"]}"

  if build["phase"] == "FINALIZED"
    case build["status"]
    when "FAILURE"
      message = message % "FAILED"
      message += " (#{build["full_url"]})"
    when "SUCCESS"
      message = message % "OK"
    else
      message = message % "UNKNOWN"
    end

    robot.send_message(target, message)
  end

rescue Exception => e
  robot.send_message(target, "[jenkins] failed to process Gerrit build event (#{e.message})")
end

#change_details(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lita/handlers/gerrit.rb', line 16

def change_details(response)
  change_id = response.matches.flatten.first
  change_uri = "#{config.url.chomp("/")}/a/changes/#{change_id}"
  change_link = "#{config.url.chomp("/")}/#{change_id}"

  http_resp = HTTParty.get(change_uri, :digest_auth => {
    username: config.username,
    password: config.password
  })

  case http_resp.code
  when 200
    change = MultiJson.load(http_resp.body.lines.to_a[1..-1].join)
    message = "[gerrit] [#{change["project"]}] \"#{change["subject"]}\" by #{change["owner"]["name"]}. #{change_link}"
  when 404
    message = "[gerrit] Change ##{change_id} does not exist"
  else
    raise "Failed to fetch #{change_uri} (#{http_resp.code})"
  end

  response.reply(message)
rescue Exception => e
  response.reply("[gerrit] Error: #{e.message}")
end

#hook(request, response) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lita/handlers/gerrit.rb', line 47

def hook(request, response)
  if request.params.has_key?("action")
    action = request.params["action"].gsub("-", "_").to_sym

    unless respond_to?(action, true)
      raise "Action #{action} is not supported by Gerrit handler"
    end
  else
    raise "Action must be defined in hook's parameters"
  end

  if request.params.has_key?("room")
    room = request.params["room"]
  elsif config.default_room
    room = config.default_room
  else
    raise "Room must be defined. Either fix your hook or specify a default room ('config.handlers.gerrit.default_room')"
  end

  # build message from action and params
  message = send(action, request.params)
  target = Source.new(room: room)

  robot.send_message(target, message)
rescue Exception => e
  Lita.logger.error(e.message)
end