Class: Honeybadger::CLI::Heroku

Inherits:
Thor
  • Object
show all
Includes:
Helpers
Defined in:
lib/honeybadger/cli/heroku.rb

Instance Method Summary collapse

Methods included from Helpers

#load_rails, #load_rails_env, #rails?, #rails_framework_opts, #rails_test, #send_test, #standalone_test, #test_exception_class

Instance Method Details

#install(api_key) ⇒ Object



39
40
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
67
68
69
70
71
72
73
74
75
# File 'lib/honeybadger/cli/heroku.rb', line 39

def install(api_key)
  say("Installing Honeybadger #{VERSION} for Heroku")

  load_rails(verbose: true)

  ENV['HONEYBADGER_LOGGING_LEVEL']     = '2'
  ENV['HONEYBADGER_LOGGING_TTY_LEVEL'] = '0'
  ENV['HONEYBADGER_LOGGING_PATH']      = 'STDOUT'
  ENV['HONEYBADGER_REPORT_DATA']       = 'true'

  ENV['HONEYBADGER_API_KEY'] = api_key

  app = options[:app] || detect_heroku_app(false)
  say("Adding config HONEYBADGER_API_KEY=#{api_key} to Heroku.", :magenta)
  unless write_heroku_env({'HONEYBADGER_API_KEY' => api_key}, app)
    say('Unable to update heroku config. Do you need to specify an app name?', :red)
    exit(1)
  end

  if env = heroku_var('RAILS_ENV', app, heroku_var('RACK_ENV', app))
    say('Installing deploy notification addon', :magenta)
    invoke :install_deploy_notification, [], { app: app, api_key: api_key, environment: env }
  else
    say('Skipping deploy notification installation: we were unable to determine the environment name from your Heroku app.', :yellow)
    say("To install manually, try `honeybadger heroku install_deploy_notification#{app ? " -a #{app}" : ""} -k #{api_key} --environment ENVIRONMENT`", :yellow)
  end

  config = Config.new(rails_framework_opts)
  Honeybadger.start(config) unless load_rails_env(verbose: true)
  say('Sending test notice')
  unless Agent.instance && send_test(false)
    say("Honeybadger is installed, but failed to send a test notice. Try `HONEYBADGER_API_KEY=#{api_key} honeybadger test`.", :red)
    exit(1)
  end

  say("Installation complete. Happy 'badgering!", :green)
end

#install_deploy_notificationObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/honeybadger/cli/heroku.rb', line 15

def install_deploy_notification
  app       = options.has_key?('app') ? options['app'] : detect_heroku_app(false)
  rails_env = options['environment'] || heroku_var('RAILS_ENV', app)
  api_key   = options['api_key'] || heroku_var('HONEYBADGER_API_KEY', app)

  unless api_key =~ /\S/
    say("Unable to detect your API key from Heroku.", :red)
    say('Have you configured multiple Heroku apps? Try using --app APP', :red) unless app
    exit(1)
  end

  unless rails_env =~ /\S/
    say("Unable to detect your environment from Heroku. Use --environment ENVIRONMENT.", :red)
    say('Have you configured multiple Heroku apps? Try using --app APP', :red) unless app
    exit(1)
  end

  cmd = %Q(heroku addons:create deployhooks:http --url="https://api.honeybadger.io/v1/deploys?deploy[environment]=#{rails_env}&deploy[local_username]={{user}}&deploy[revision]={{head}}&api_key=#{api_key}"#{app ? " --app #{app}" : ''})

  say("Running: `#{cmd}`")
  say(run(cmd))
end