Class: Lita::Handlers::DockerHub

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

Instance Method Summary collapse

Instance Method Details

#receive(request, response) ⇒ Object



8
9
10
11
12
13
14
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
45
# File 'lib/lita/handlers/docker_hub.rb', line 8

def receive(request, response)
  response.headers["Content-Type"] = "application/json"

  body = parse(request.body.read)
  repo_name = body.fetch("repository", {}).fetch("repo_name", nil)
  repo_url = body.fetch("repository", {}).fetch("repo_url", nil)
  tag = body.fetch("push_data", {}).fetch("tag", nil)
  # pusher = body.fetch("push_data", {}).fetch("pusher", nil)
  # pushed_at = body.fetch("push_data", {}).fetch("pushed_at", nil)

  if repo_name.present? && repo_url.present? && tag.present?
    # build_time = time_interval(Time.at(pushed_at), Time.now)

    target = find_room_by_name(config.room)
    message = render_template("build", repo_name: repo_name,
                                       tag: tag)

    case robot.config.robot.adapter
    when :slack
      attachment = ::Lita::Adapters::Slack::Attachment.new(message,
        title: "Docker Hub",
        title_link: repo_url,
        color: "#36a64f")
      robot.chat_service.send_attachment(target, [attachment])
    else
      robot.send_message(target, message)
    end

    response.write("ok")
  else
    response.write("error")
  end
rescue => error
  Lita.logger.error error.message
  Lita.logger.debug error.backtrace.join("\n")

  response.write("error")
end