siebertm (owner)

Forks

Revisions

gist: 120627 Download_button fork
public
Description:
post-receive hook for integrity, using more than one project and https, daemonized
Public Clone URL: git://gist.github.com/120627.git
post-receive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/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