<commit_before>require 'sinatra'
require 'json'
require 'pp'

set :bind, '0.0.0.0'

get '/hi' do
  "HI THERE"
end

post '/pr' do
  puts "-------------------------"
  res = JSON.parse(request.body.read)
  pp res
  puts "action: #{res['action']}"
  puts "SHA: #{res['pull_request']['head']['sha']}"
  puts "REF: #{res['pull_request']['head']['ref']}"
  puts "CLONE URL: #{res['pull_request']['head']['repo']['clone_url']}"
  puts "REPO NAME: #{res['pull_request']['head']['repo']['name']}"
  puts "-------------------------"

  action = res['action']
  clone_url = res['pull_request']['head']['repo']['clone_url']
  repo_name = res['pull_request']['head']['repo']['name']
  sha = res['pull_request']['head']['sha']

  if action == 'opened' #todo what about reopened?
    puts 'DOIN WORK'
    `mkdir projects`
    Dir.chdir 'projects'
    `git clone #{clone_url} #{repo_name}`
    Dir.chdir repo_name
    `git checkout #{sha}`
    `docker-machine create --driver digitalocean --digitalocean-access-token #{ENV['DO_TOKEN']} todo_allow_name_to_be_configurable`
    puts 'DO TOKEN: ', ENV['DO_TOKEN']
    #TODO: clean up 
  else
    puts 'DO NOTHING'
  end
end
<commit_msg>total wip<commit_after>require 'sinatra'
require 'json'
require 'pp'
require 'pry'
require 'yaml'

set :bind, '0.0.0.0'

get '/hi' do
  "HI THERE"
end

post '/pr' do
  puts "-------------------------"
  res = JSON.parse(request.body.read)
  pp res
  puts "action: #{res['action']}"
  puts "SHA: #{res['pull_request']['head']['sha']}"
  puts "REF: #{res['pull_request']['head']['ref']}"
  puts "CLONE URL: #{res['pull_request']['head']['repo']['git_url']}"
  puts "REPO NAME: #{res['pull_request']['head']['repo']['name']}"
  puts "-------------------------"

  action = res['action']
  clone_url = res['pull_request']['head']['repo']['git_url']
  repo_name = res['pull_request']['head']['repo']['name']
  sha = res['pull_request']['head']['sha']

  if %w(opened reopened).include?(action)
    puts 'DOIN WORK'
    `mkdir projects`
    Dir.chdir 'projects'
    `git clone #{clone_url} #{repo_name}`
    Dir.chdir repo_name
    `git checkout #{sha}`
    machine_name = "zazzle" # todo allow_name_to_be_configurable
    puts 'about to provision...\n'
    provision_machine(machine_name)
    build_and_run
  end
end

def build_and_run
  puts "deploying... to #{ENV['DOCKER_HOST']}"
  `docker-compose up -d`
  puts "done deploying...\n\n"
end

def provision_machine(machine_name)
  if name = ENV['DOCKER_MACHINE_NAME'].to_s.empty?
    puts "creating #{machine_name}...\n"
    `docker-machine create --driver digitalocean --digitalocean-access-token #{ENV['DO_TOKEN']} #{machine_name}`
  else
    puts "using existing machine: #{ENV['DOCKER_MACHINE_NAME']}"
  end
  #TODO: clean up 
  puts "machine created, setting envs...\n"
  env_lines = `docker-machine env #{machine_name}`
  env_lines.each_line do |line|
    k, v = line.to_s.gsub("export ", "").strip.split("=")
    if k && v 
      ENV[k]=YAML.load(v)
    end
  end
  binding.pry
end

{'fraction_non_alphanumeric': 0.22251832111925382, 'fraction_numerical': 0.002664890073284477, 'mean_word_length': 3.655813953488372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7499702', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1078, 'n_words': 239}