<commit_before>begin
  require 'dotenv'
rescue LoadError
end

require 'sinatra/base'
require 'json'
require 'redis'
require 'openssl'
require 'base64'

require_relative '../config/redis'
require_relative './helpers'
require_relative './cloner'

class RepositorySync < Sinatra::Base
  set :root, File.dirname(__FILE__)
  Dotenv.load if Sinatra::Base.development?

  configure do
    configure_redis
  end

  SUPPORTED_SYNC_METHODS = ['squash', 'merge', 'replace_contents']

  get '/' do
    'You\'ll want to make a POST to /sync. Check the documentation for more info.'
  end

  post '/sync' do
    # trim trailing slashes
    request.path_info.sub!(/\/$/, '')

    # ensure there's a payload
    request.body.rewind
    payload_body = request.body.read.to_s
    halt 500, 'Missing body payload!' if payload_body.nil? || payload_body.empty?

    # ensure signature is correct
    github_signature = request.env['HTTP_X_HUB_SIGNATURE']
    halt 500, 'Signatures didn\'t match!' unless signatures_match?(payload_body, github_signature)

    @destination_repo = params[:dest_repo]
    halt 500, 'Missing `dest_repo` argument' if @destination_repo.nil?

    @payload = JSON.parse(payload_body)
    halt 202, "Payload was not for master, was for #{@payload['ref']}, aborting." unless master_branch?(@payload)

    @sync_method = params[:sync_method] || "merge"
    @sync_method = "squash" if params[:squash]
    halt 400, "sync_method #{@sync_method} not supported" unless SUPPORTED_SYNC_METHODS.include?(@sync_method)

    # keep some important vars
    process_payload(@payload)
    @destination_hostname = params[:destination_hostname] || 'github.com'

    Resque.enqueue(CloneJob, @after_sha, @destination_hostname, @destination_repo, @originating_hostname, @originating_repo, @sync_method)
  end

  helpers Helpers<commit_msg>Remove backwards compatibility.<commit_after>begin
  require 'dotenv'
rescue LoadError
end

require 'sinatra/base'
require 'json'
require 'redis'
require 'openssl'
require 'base64'

require_relative '../config/redis'
require_relative './helpers'
require_relative './cloner'

class RepositorySync < Sinatra::Base
  set :root, File.dirname(__FILE__)
  Dotenv.load if Sinatra::Base.development?

  configure do
    configure_redis
  end

  SUPPORTED_SYNC_METHODS = ['squash', 'merge', 'replace_contents']

  get '/' do
    'You\'ll want to make a POST to /sync. Check the documentation for more info.'
  end

  post '/sync' do
    # trim trailing slashes
    request.path_info.sub!(/\/$/, '')

    # ensure there's a payload
    request.body.rewind
    payload_body = request.body.read.to_s
    halt 500, 'Missing body payload!' if payload_body.nil? || payload_body.empty?

    # ensure signature is correct
    github_signature = request.env['HTTP_X_HUB_SIGNATURE']
    halt 500, 'Signatures didn\'t match!' unless signatures_match?(payload_body, github_signature)

    @destination_repo = params[:dest_repo]
    halt 500, 'Missing `dest_repo` argument' if @destination_repo.nil?

    @payload = JSON.parse(payload_body)
    halt 202, "Payload was not for master, was for #{@payload['ref']}, aborting." unless master_branch?(@payload)

    @sync_method = params[:sync_method] || "merge"
    halt 400, "sync_method #{@sync_method} not supported" unless SUPPORTED_SYNC_METHODS.include?(@sync_method)

    # keep some important vars
    process_payload(@payload)
    @destination_hostname = params[:destination_hostname] || 'github.com'

    Resque.enqueue(CloneJob, @after_sha, @destination_hostname, @destination_repo, @originating_hostname, @originating_repo, @sync_method)
  end

  helpers Helpers
{'fraction_non_alphanumeric': 0.11378072355702845, 'fraction_numerical': 0.009389671361502348, 'mean_word_length': 4.529770992366412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2161837', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1113, 'n_words': 295}