-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-receive
executable file
·69 lines (58 loc) · 1.74 KB
/
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
#!/usr/bin/env ruby
require 'rubygems'
require 'grit'
require 'json'
require 'net/https'
$repo = Grit::Repo.new(".")
$config = Grit::Config.new($repo)
def generate_payload
old_commit, new_commit, ref = gets.split()
commits = $repo.commits_between(old_commit, new_commit)
commits.map! do |commit|
{
:removed => filter(commit.diffs) { |d| d.deleted_file },
:added => filter(commit.diffs) { |d| d.new_file },
:modified => filter(commit.diffs) { |d| !d.deleted_file && !d.new_file },
:id => commit.sha,
:message => commit.message,
:timestamp => commit.authored_date.iso8601,
:author => {
:name => commit.author.name,
:email => commit.author.email
},
}
end
{
:before => old_commit,
:after => new_commit,
:ref => ref,
:commits => commits,
:ref_name => ref.to_s.sub(/\Arefs\/(heads|tags)\//, ''),
:repository => {
:name => File.basename(Dir.pwd).sub(/\.git$/,'')
}
}
end
def filter(diffs)
diffs.select { |e| yield e }.map { |diff| diff.b_path }
end
# Gets all 'hookdata' prefixed key-value pairs from git config
def hookdata
Hash[*$config.keys.grep(/httphook\.(?!url$)/).map do |key|
[key.sub(/.*\./, '').to_sym, $config[key]]
end.flatten]
end
def post_data(url, data)
uri = URI.parse(url)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(data)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
end
res = http.start { |http| http.request(req) }
end
url = $config['httphook.url'] or raise 'destination POST URL not set'
data = { :payload => generate_payload.to_json }.merge(hookdata)
post_data(url, data)