-
-
Save levinalex/120631 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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