Skip to content

Instantly share code, notes, and snippets.

@ejholmes
Created January 31, 2012 07:41
Show Gist options
  • Save ejholmes/1709372 to your computer and use it in GitHub Desktop.
Save ejholmes/1709372 to your computer and use it in GitHub Desktop.
Git post-receive hook for sending commit data to a github-services server.
#!/usr/bin/env ruby
require 'rubygems'
require 'grit'
require 'net/http'
require 'json'
rev_old, rev_new, ref = STDIN.read.split(" ")
repo = Grit::Repo.new File.expand_path(".")
payload = {
:before => rev_old,
:after => rev_new,
:ref => ref,
:compare => '',
:commits => [],
:repository => {
:name => ENV["GL_REPO"],
:url => '',
:pledgie => '',
:description => '',
:homepage => '',
:watchers => '',
:forks => '',
:private => '',
:owner => {
:name => '',
:email => ''
}
}
}
repo.commits(ref, 50).each do |commit|
break if commit.id == rev_old
added_files = []
removed_files = []
modified_files = []
parent = commit.parents.first.id if commit.parents.first.is_a?(Grit::Commit)
Grit::Commit.diff(repo, parent, commit.id).each do |diff|
if diff.new_file
added_files.push(diff.a_path)
elsif diff.deleted_file
removed_files.push(diff.a_path)
else
modified_files.push(diff.a_path)
end
end
payload[:commits].push(
:id => commit.id,
:message => commit.message,
:timestamp => commit.committed_date.xmlschema,
:added => added_files,
:removed => removed_files,
:modified => modified_files,
:author => {
:name => commit.author.name,
:email => commit.author.email
}
)
end
payload[:commits].reverse!
services = `git config --get github-services.services`.split(' ')
server = `git config --get github-services.server`
server = 'github-services.heroku.com' if server == ''
http = Net::HTTP.new(server.gsub(/\n/, ''), 80)
services.each do |service|
config = `git config --get-regexp github-services.#{service}`
data = {}
config.split(/\n/).each do |line|
key = /(.*?\.){2}([\w]*)/.match(line)[2]
value = /\s.*/.match(line)[0].gsub(/^\s/, '')
data[key.to_sym] = value
puts data
end
path = "/#{service}/push"
http.post(path, "data=#{data.to_json}&payload=#{payload.to_json}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment