class Autobot::Runner

Public Class Methods

check_binaries() click to toggle source

Public: Check if needed binaries are found: if any of these doesn’t exist the program exits.

Returns nothing.

# File lib/autobot/runner.rb, line 46
def self.check_binaries
  puts 'Checking binaries...'

  abort('Missing npm, exiting...') unless which('npm')
  abort('Missing heroku toolbelt, exiting...') unless which('heroku')
end
create_empty_hubot(app_path) click to toggle source

Public: Clones a virgin hubot from GitHub repository in a local repository.

app_path - The path of your app.

Returns nothing.

# File lib/autobot/runner.rb, line 95
def self.create_empty_hubot(app_path)
  temp_dir = Dir.mktmpdir
  system("git clone https://github.com/github/hubot.git #{temp_dir}")

  Dir.chdir(temp_dir)

  # Install needed node.js packages and create an instance of hubot locally
  system('npm install && make package')

  # Copy hubot and clean tmpdir
  FileUtils.cp_r("./hubot", app_path)
  FileUtils.rm_rf(temp_dir)
end
create_hubot(app_name) click to toggle source

Public: Creates an hubot instance starting with an application name.

app_name - The name of your app.

Returns nothing.

# File lib/autobot/runner.rb, line 16
def self.create_hubot(app_name)
  check_binaries

  print 'Insert your heroku api token: '
  api_token = $stdin.gets
  heroku = heroku_login(api_token)

  heroku_app, is_new = get_or_create_heroku_app(heroku, app_name)
  git_remote = heroku_app.data[:body]['git_url']
  app_path = "#{Dir.pwd}/#{app_name}"

  if Dir.exists?(app_path)
    push_hubot_app(app_name, app_path, heroku, git_remote)
  else
    if is_new
      create_empty_hubot(app_path)
    else
      system("git clone #{repository_url} #{app_path}")
    end

    puts "I've just cloned the repository: make every change you want and commit. I'll do the rest."
  end

  puts 'Autobot shutting down...'
end
get_or_create_heroku_app(heroku, app_name) click to toggle source

Public: Checks if an app exists: if no it creates one.

heroku - An heroku client instance. app_name - The name of your app.

Returns the duplicated String.

# File lib/autobot/runner.rb, line 115
def self.get_or_create_heroku_app(heroku, app_name)
  begin
    [ heroku.get_app(app_name), false ]
  rescue Heroku::API::Errors::Forbidden
    nil
  rescue Heroku::API::Errors::NotFound
    puts "I didn't find #{app_name} on Heroku. I will create it for you!"
    heroku.post_app('name' => app_name)
    [ heroku.get_app(app_name), true ]
  end
end
heroku_login(api_token) click to toggle source

Public: Makes login to heroku.

api_token - The API Token of your heroku account.

Returns an heroku client instance.

# File lib/autobot/runner.rb, line 132
def self.heroku_login(api_token)
  heroku = Heroku::API.new(api_key: api_token)
  begin
    heroku.get_user
    puts 'Authentication successful'
    heroku
  rescue Heroku::API::Errors::Unauthorized
    abort('Your api token is not valid. Please verify it and retry to launch autobot!')
  end
end
push_hubot_app(app_name, app_path, heroku, git_remote) click to toggle source

Public: Pushes local repository to heroku adding a remote if it doesn’t exist.

app_name - The name of your app. app_path - The path of your app. heroku - An heroku client instance. git_remote - The URL of the remote repository.

Returns nothing.

# File lib/autobot/runner.rb, line 61
def self.push_hubot_app(app_name, app_path, heroku, git_remote)
  Dir.chdir(app_path)

  # Add heroku application as a remote
  system("git remote add herokuapp #{git_remote}")

  # Make sure we are at the latest version
  system("git pull herokuapp master")

  # Push our commits to heroku remote
  system("git push herokuapp master")

  return unless acceptor('Would you like to insert campfire environment variables?')

  # Ask and add as config campfire data
  puts 'Insert hubot campfire token'
  hubot_token  = $stdin.gets

  puts 'Insert hubot campfire rooms'
  hubot_rooms  = $stdin.gets

  puts 'Insert hubot campfire domain (e.g. the third level domain in your campfire account)'
  hubot_domain = $stdin.gets

  heroku.put_config_vars(app_name, 'HUBOT_CAMPFIRE_TOKEN' => hubot_token,
                                  'HUBOT_CAMPFIRE_ROOMS' => hubot_rooms,
                                  'HUBOT_CAMPFIRE_ACCOUNT' => hubot_domain)
end