Skip to content

Instantly share code, notes, and snippets.

@siebertm
Created May 30, 2009 20:33
Show Gist options
  • Save siebertm/120627 to your computer and use it in GitHub Desktop.
Save siebertm/120627 to your computer and use it in GitHub Desktop.
post-receive hook for integrity, using more than one project and https, daemonized
#!/usr/bin/env ruby
require 'rubygems'
require 'net/https'
require 'uri'
require 'json'
require 'enumerator'
require "daemons"
# EDIT POST_RECEIVE_URL
POST_RECEIVE_URLS = [
'https://user:pass@integrity.example.com/project1/push',
'https://user:pass@integrity.example.com/project2/push',
]
old_head, new_head, ref = STDIN.gets.split
revision_text = `git rev-list --pretty=format:"Author: %an <%ae>%nDate: %aD%n%n%s%n" #{new_head} ^#{old_head}`
revisions = []
revision_text.split( /\n/ ).each_slice( 6 ) { |s|
sha1 = s[0][ /commit (\w+)/, 1 ]
s[1] =~ /Author: ([^<]+) <(.+?)>/
author_name, author_email = $1, $2
timestamp = s[2][ /Date: +(.+)$/, 1 ]
message = s[4..-1].join.strip
revisions << {
'id' => sha1,
'author' => {
'email' => author_email,
'name' => author_name,
},
'message' => message,
'timestamp' => timestamp
}
}
if revisions.empty?
exit 0
end
payload = {
'payload' => {
"ref" => ref,
"commits" => revisions,
}.to_json
}
Daemons.daemonize
POST_RECEIVE_URLS.each do |u|
begin
url = URI.parse(u)
req = Net::HTTP::Post.new(url.path)
req.form_data = payload
req.basic_auth url.user, url.password if url.user
h = Net::HTTP.new(url.host, url.port)
h.use_ssl = true
h.ssl_timeout = 6000
h.timeout = 6000
h.start {|http|
http.request(req)
}
rescue Timeout::Error
# we got a timeout, integrity is building, so dont disturb it, wait before starting next build
sleep 60
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment