<commit_before>require "bundler/setup"
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
Dotenv.load

configure do
  MongoMapper.setup({'production' => {'uri' => ENV['MONGOHQ_URL']}}, 'production')
end

class HookDelivery
  include MongoMapper::Document

  key :headers, String
  key :payload, String
  timestamps!

  def secrets_match?
    return false unless ENV["SECRET_TOKEN"]

    signature = "sha1=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), ENV["SECRET_TOKEN"], payload)
    Rack::Utils.secure_compare(signature, headers["X-Hub-Signature"])
  end

  def headers
    JSON.parse(super)
  end

  def payload
    JSON.parse(super)
  end

  def pretty_headers
    JSON.pretty_generate(headers)
  end

  def pretty_payload
    JSON.pretty_generate(payload)
  end
end

post "/" do
  headers = {
    "X-GitHub-Event"    => request["X-GitHub-Event"],
    "X-GitHub-Delivery" => request["X-GitHub-Delivery"],
    "X-Hub-Signature"   => request["X-Hub-Signature"]
  }.to_json
  payload = request.body.read

  HookDelivery.create(
    :payload => payload,
    :headers => headers
  )
<commit_msg>Get HTTP headers through request.env<commit_after>require "bundler/setup"
ENV["RACK_ENV"] ||= "development"
Bundler.require(:default, ENV["RACK_ENV"].to_sym)
Dotenv.load

configure do
  MongoMapper.setup({'production' => {'uri' => ENV['MONGOHQ_URL']}}, 'production')
end

class HookDelivery
  include MongoMapper::Document

  key :headers, String
  key :payload, String
  timestamps!

  def secrets_match?
    return false unless ENV["SECRET_TOKEN"]

    signature = "sha1=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), ENV["SECRET_TOKEN"], payload)
    Rack::Utils.secure_compare(signature, headers["X-Hub-Signature"])
  end

  def headers
    JSON.parse(super)
  end

  def payload
    JSON.parse(super)
  end

  def pretty_headers
    JSON.pretty_generate(headers)
  end

  def pretty_payload
    JSON.pretty_generate(payload)
  end
end

post "/" do
  headers = {
    "X-GitHub-Event"    => request.env["X-GitHub-Event"],
    "X-GitHub-Delivery" => request.env["X-GitHub-Delivery"],
    "X-Hub-Signature"   => request.env["X-Hub-Signature"]
  }.to_json
  payload = request.body.read

  HookDelivery.create(
    :payload => payload,
    :headers => headers
  )

{'fraction_non_alphanumeric': 0.14452617914322805, 'fraction_numerical': 0.0017308524448290783, 'mean_word_length': 4.081318681318681, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5231705', 'n_tokens_mistral': 833, 'n_tokens_neox': 798, 'n_words': 150}