<commit_before>$:.unshift *Dir["#{File.dirname(__FILE__)}/vendor/**/lib"]
%w( rack sinatra tinder twitter json net/http socket timeout ).each { |f| require f }

post '/campfire/' do
  data       = JSON.parse(params[:data])
  payload    = JSON.parse(params[:payload])

  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  campfire   = Tinder::Campfire.new(data['subdomain'], :ssl => data['ssl'].to_i == 1)
  campfire.login data['email'], data['password']
  room       = campfire.find_room_by_name data['room']
  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}/#{branch}] #{commit['message']} - #{commit['author']['name']} (#{commit['url']})"
    room.speak text
  end
end

post '/irc/' do
  data       = JSON.parse(params[:data])
  payload    = JSON.parse(params[:payload])

  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  room       = data['room']
  room       = "##{room}" unless data['room'][0].chr == '#'
  irc        = TCPSocket.open(data['server'], data['port'])
  irc.puts "USER blah blah blah :blah blah"
  irc.puts "NICK hubbub"
  irc.puts "JOIN #{room}"
  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}/#{branch}] #{commit['message']} - #{commit['author']['name']} (#{commit['url']})"
    irc.puts "PRIVMSG #{room} :#{text}"
  end
  irc.puts "QUIT"
end

post '/lighthouse/' do
  data       = JSON.parse(params[:data])
  payload    = JSON.parse(params[:payload])

  payload['commits'].each do |commit_id, commit|
    added    = commit['added'].map    { |f| ['A', f] }
    removed  = commit['removed'].map  { |f| ['R', f] }
    modified = commit['modified'].map { |f| ['M', f] }
    diff     = YAML.dump(added + removed + modified)

    title = "Changeset [%s] by %s" % [commit_id, commit['author']['name']]
    body  = "#{commit['message']}\n#{commit['url']}"
    changeset_xml = <<-XML.strip
      <changeset>
        <title>#{CGI.escapeHTML(title)}</title>
        <body>#{CGI.escapeHTML(body)}</body>
        <changes type="yaml">#{CGI.escapeHTML(diff)}</changes>
        <revision>#{CGI.escapeHTML(commit_id)}</revision>
        <changed-at type="datetime">#{CGI.escapeHTML(commit['timestamp'])}</changed-at>
      </changeset>
    XML

    account = "http://#{data['subdomain']}.lighthouseapp.com"
    url = URI.parse('%s/projects/%d/changesets.xml' % [account, data['project_id']])
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth data['token'], 'x'
    req.body = changeset_xml
    req.set_content_type('application/xml')
    Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  end
end

post '/twitter/' do
  data       = JSON.parse(params[:data])
  payload    = JSON.parse(params[:payload])

  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  twitter    = Twitter::Base.new(data['username'], data['password'])
  begin
    Timeout::timeout(2) do
      url = Net::HTTP.get "tinyurl.com", "/api-create.php?url=#{commit['url']}"
    end
  rescue
  end
  url ||= commit['url']

  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}] #{url} #{commit['author']['name']} - #{commit['message']}"
    twitter.post text
  end
end
<commit_msg>Exterminate repetition and make people's lives just that little bit easier<commit_after>$:.unshift *Dir["#{File.dirname(__FILE__)}/vendor/**/lib"]
%w( rack sinatra tinder twitter json net/http socket timeout ).each { |f| require f }

module GitHub
  def service(name, &block)
    post "/#{name}/" do
      data = JSON.parse(params[:data])
      payload = JSON.parse(params[:payload])
      yield data, payload, params
    end
  end
end
include GitHub

service :campfire do |data, payload|
  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  campfire   = Tinder::Campfire.new(data['subdomain'], :ssl => data['ssl'].to_i == 1)
  campfire.login data['email'], data['password']
  room       = campfire.find_room_by_name data['room']
  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}/#{branch}] #{commit['message']} - #{commit['author']['name']} (#{commit['url']})"
    room.speak text
  end
end

service :irc do |data, payload|
  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  room       = data['room']
  room       = "##{room}" unless data['room'][0].chr == '#'
  irc        = TCPSocket.open(data['server'], data['port'])
  irc.puts "USER blah blah blah :blah blah"
  irc.puts "NICK hubbub"
  irc.puts "JOIN #{room}"
  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}/#{branch}] #{commit['message']} - #{commit['author']['name']} (#{commit['url']})"
    irc.puts "PRIVMSG #{room} :#{text}"
  end
  irc.puts "QUIT"
end

service :lighthouse do |data, payload|
  payload['commits'].each do |commit_id, commit|
    added    = commit['added'].map    { |f| ['A', f] }
    removed  = commit['removed'].map  { |f| ['R', f] }
    modified = commit['modified'].map { |f| ['M', f] }
    diff     = YAML.dump(added + removed + modified)

    title = "Changeset [%s] by %s" % [commit_id, commit['author']['name']]
    body  = "#{commit['message']}\n#{commit['url']}"
    changeset_xml = <<-XML.strip
      <changeset>
        <title>#{CGI.escapeHTML(title)}</title>
        <body>#{CGI.escapeHTML(body)}</body>
        <changes type="yaml">#{CGI.escapeHTML(diff)}</changes>
        <revision>#{CGI.escapeHTML(commit_id)}</revision>
        <changed-at type="datetime">#{CGI.escapeHTML(commit['timestamp'])}</changed-at>
      </changeset>
    XML

    account = "http://#{data['subdomain']}.lighthouseapp.com"
    url = URI.parse('%s/projects/%d/changesets.xml' % [account, data['project_id']])
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth data['token'], 'x'
    req.body = changeset_xml
    req.set_content_type('application/xml')
    Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  end
end

service :twitter do |data, payload|
  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  twitter    = Twitter::Base.new(data['username'], data['password'])
  begin
    Timeout::timeout(2) do
      url = Net::HTTP.get "tinyurl.com", "/api-create.php?url=#{commit['url']}"
    end
  rescue
  end
  url ||= commit['url']

  payload['commits'].each do |commit|
    commit = commit.last
    text   = "[#{repository}] #{url} #{commit['author']['name']} - #{commit['message']}"
    twitter.post text
  end
end

{'fraction_non_alphanumeric': 0.20694841329523236, 'fraction_numerical': 0.0009023913370431644, 'mean_word_length': 3.686398872445384, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '721644', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2244, 'n_words': 449}