Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created March 17, 2010 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonid-shevtsov/335528 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/335528 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Lighthouse + Git
# post-receive hook
LIGHTHOUSE_TOKEN = 'owner token here (the default one)'
LIGHTHOUSE_ACCOUNT = 'account (the subdomain)'
LIGHTHOUSE_PROJECT = 'project id (look in /projects/[id])'
# Store individual user tokens here.
# They are required so that changesets are mapped to the correct Lighthouse username
# See http://help.lighthouseapp.com/faqs/api/scm-integration
token_map = {
'committer@email.com' => 'his token',
'other@committer.com' => 'another token',
#etc
}
require 'yaml'
require 'cgi'
require 'net/http'
git = `which git`.strip
NEWREV = `#{git} log -1 | sed -e 's/commit //' -e '2,$d'`.chomp
OLDREV = `cat /tmp/git-old-rev`.chomp
revs = `#{git} log --pretty=format:%H #{OLDREV}..#{NEWREV}`.split(/\n/).reverse
if revs.size.zero?
puts "Already up-to-date."
exit 1
end
puts "* Pushing #{revs.size} revisions to lighthouse"
revs.each do |revision|
next if revision =~ /^Merge branch/
author = `#{git} show --pretty=format:"%an" #{revision} | sed q`.chomp
author_email = `#{git} show --pretty=format:"%ae" #{revision} | sed q`.chomp
author_token = token_map[author_email] || (LIGHTHOUSE_TOKEN and puts("unknown committer email #{author_email}"))
log = `#{git} show --pretty=format:"%s" #{revision} | sed q`.chomp
date = `#{git} show --pretty=format:"%aD" #{revision} | sed q`.chomp
changed = `#{git} diff-tree -r --name-status #{revision} | sed -n '$p'`
changes = changed.split("\n").inject([]) { |memo, line| memo << [$1, $2] if line.strip =~ /(\w)\s+(.*)/ }.to_yaml
xml = <<-EOF
<changeset>
<title>#{CGI.escapeHTML("%s committed new changes" % [author])}</title>
<body>
#{CGI.escapeHTML(log)}\n
</body>
<changes type="yaml">#{CGI.escapeHTML(changes)}</changes>
<revision>#{CGI.escapeHTML(revision)}</revision>
<changed-at type="datetime">#{CGI.escapeHTML(date.split('(').first.strip)}</changed-at>
</changeset>
EOF
request = Net::HTTP::Post.new("/projects/#{LIGHTHOUSE_PROJECT}/changesets.xml")
request.set_content_type "application/xml"
request.basic_auth author_token, "x"
request.body = xml
response = Net::HTTP.start("#{LIGHTHOUSE_ACCOUNT}.lighthouseapp.com").request(request)
puts " Pushed rev #{revision} (#{log}). response: #{response.message}"
sleep 0.3
end
#!/bin/sh
rev=$(git log -1 | sed -e 's/commit //' -e '2,$d')
echo $rev > /tmp/git-old-rev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment