<commit_before>require 'json'

def is_commit_push?(payload)
  payload["ref"] == "refs/heads/master"
end

def is_tag_push?(payload)
  payload["ref"] && payload["ref"] =~ /^refs\/tags\// && payload["created"]
end

def extract_tag_name(payload)
  payload["ref"].sub(/^ref\/tags\//, '')
end

def find_project(payload)
  repo_name = payload["repository"]["name"]
  projects = YAML.load_file("projects.yml")
  projects[repo_name]
end

def schedule_build(project, tag_name = nil)
  command = "/tools/silence-unless-failed ./autobuild-with-pbuilder #{project['git_url']} #{project['name']}"
  if tag_name
    command << " --tag=#{tag_name}"
  end
  IO.popen("at now", "w") do |f|
    f.puts("cd /srv/passenger_autobuilder/app")
    f.puts(command)
  end
  true
end

def process_request(request, payload)
  if is_master_commit_push?(payload)
    if project = find_project(payload)
      schedule_build(project)
    else
      false
    end
  elsif is_tag_push?(payload) && (tag_name = extract_tag_name)
    if project = find_project(payload)
      schedule_build(project, tag_name)
    else
      false
    end
  else
    false
  end
end

app = lambda do |env|
  request = Rack::Request.new(env)
  payload = JSON.parse(request.params["payload"])
  if process_request(request, payload)
    [200, { "Content-Type" => "text/plain" }, ["ok"]]
  else
    [500, { "Content-Type" => "text/plain" }, ["Internal server error"]]
  end
end

run app
<commit_msg>Fix some bugs in the webhook<commit_after>require 'json'

def is_master_commit_push?(payload)
  payload["ref"] == "refs/heads/master"
end

def is_tag_push?(payload)
  payload["ref"] && payload["ref"] =~ /^refs\/tags\// && payload["created"]
end

def extract_tag_name(payload)
  payload["ref"].sub(/^ref\/tags\//, '')
end

def find_project(payload)
  repo_name = payload["repository"]["name"]
  projects = YAML.load_file("projects.yml")
  projects[repo_name]
end

def schedule_build(project, tag_name = nil)
  command = "/tools/silence-unless-failed chpst -l /tmp/passenger_autobuilder.lock " +
    "./autobuild-with-pbuilder #{project['git_url']} #{project['name']}"
  if tag_name
    command << " --tag=#{tag_name}"
  end
  puts "Executing command: #{command}"
  IO.popen("at now", "w") do |f|
    f.puts("cd /srv/passenger_autobuilder/app")
    f.puts(command)
  end
  true
end

def process_request(request, payload)
  if is_master_commit_push?(payload)
    if project = find_project(payload)
      schedule_build(project)
    else
      false
    end
  elsif is_tag_push?(payload) && (tag_name = extract_tag_name)
    if project = find_project(payload)
      schedule_build(project, tag_name)
    else
      false
    end
  else
    false
  end
end

app = lambda do |env|
  request = Rack::Request.new(env)
  payload = JSON.parse(request.params["payload"])
  if process_request(request, payload)
    [200, { "Content-Type" => "text/plain" }, ["ok"]]
  else
    [500, { "Content-Type" => "text/plain" }, ["Internal server error"]]
  end
end

run app

{'fraction_non_alphanumeric': 0.15603073838957568, 'fraction_numerical': 0.004009355162044771, 'mean_word_length': 3.9324546952224053, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3902411', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1066, 'n_words': 222}