Class: Lita::Handlers::Heroku

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

Instance Method Summary collapse

Instance Method Details

#api(bearer, uri, data = nil) ⇒ Object



68
69
70
71
72
# File 'lib/lita/handlers/heroku.rb', line 68

def api(bearer, uri, data=nil)
  cmd  = %Q{curl -s "https://kolkrabbi.herokuapp.com#{uri}" -H "Authorization: Bearer #{bearer}" -H 'range: name ..; order=asc, max=1000'}
  cmd += %Q{-d '#{data}'} if data
  JSON.parse `#{cmd}`
end

#heroku_binObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lita/handlers/heroku.rb', line 29

def heroku_bin
  @heroku_bin ||= if `type heroku 2>&1 | grep -v "not found"`.empty?
    if `ls /usr/local/heroku/bin/heroku 2> /dev/null`.empty?
      raise "Couldn't find heroku binary; please install the Heroku Toolbelt"
    else
      "/usr/local/heroku/bin/heroku"
    end
  else
    "heroku"
  end
end

#heroku_cmd(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lita/handlers/heroku.rb', line 16

def heroku_cmd(response)
  environment = response.matches[0][0]
  command = response.matches[0][1]
  if command.start_with? "deploy"
    heroku_deploy response
  elsif command.start_with? "psql"
    command = command[5..-1]
    stream_command response, "echo \"#{command}\" | #{heroku_bin} pg:psql -a #{config.app_prefix}#{environment}"
  else
    stream_command response, "#{heroku_bin} #{command} -a #{config.app_prefix}#{environment}"
  end
end

#heroku_deploy(response) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lita/handlers/heroku.rb', line 41

def heroku_deploy(response)
  bearer = config.oauth_token
  app_name, command, branch = response.matches[0].join(" ").split
  app_name = "#{config.app_prefix}#{app_name}" unless app_name.start_with? config.app_prefix
  branch ||= "master"

  apps = JSON.parse `curl -s "https://api.heroku.com/apps" -H "Authorization: Bearer #{bearer}" -H 'Accept: application/vnd.heroku+json; version=3'`
  app = apps.select{|app| app["name"] == app_name }.first
  app_id = app["id"]

  build_response = `curl -s "https://kolkrabbi.herokuapp.com/apps/#{app_id}/github/push" -H "Authorization: Bearer #{bearer}" -d '{"branch":"#{branch}"}'`
  build_response = JSON.parse build_response

  $stdout.puts build_response

  if build_response.key?("build") && build_response["build"]["status"] == "pending"
    response_text = "Deploying #{branch} to #{app_name}. "
    if config.bitly_access_token
      bitly_response = JSON.parse `curl -sGX GET --data-urlencode "longUrl=#{build_response["build"]["output_stream_url"]}" "https://api-ssl.bitly.com/v3/shorten?access_token=#{config.bitly_access_token}"`
      response_text += "To see build log: `curl -L #{bitly_response['data']['url']}`"
    end
    response.reply response_text
  else
    response.reply("Deploy could not be started. Response: #{build_response}")
  end
end