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
|
require 'open3'
require_relative 'gitlab_init'
require_relative 'gitlab_metrics'
class GitlabCustomHook
attr_reader :vars
def initialize(repo_path, key_id)
@repo_path = repo_path
@vars = { 'GL_ID' => key_id }
end
def pre_receive(changes)
GitlabMetrics.measure("pre-receive-hook") do
find_hooks('pre-receive').all? do |hook|
call_receive_hook(hook, changes)
end
end
end
def post_receive(changes)
GitlabMetrics.measure("post-receive-hook") do
find_hooks('post-receive').all? do |hook|
call_receive_hook(hook, changes)
end
end
end
def update(ref_name, old_value, new_value)
GitlabMetrics.measure("update-hook") do
find_hooks('update').all? do |hook|
system(vars, hook, ref_name, old_value, new_value)
end
end
end
private
def find_hooks(hook_name)
[
hook_file(hook_name, @repo_path),
hook_file(hook_name, ROOT_PATH)
].compact
end
def call_receive_hook(hook, changes)
# Prepare the hook subprocess. Attach a pipe to its stdin, and merge
# both its stdout and stderr into our own stdout.
stdin_reader, stdin_writer = IO.pipe
hook_pid = spawn(vars, hook, in: stdin_reader, err: :out)
stdin_reader.close
# Submit changes to the hook via its stdin.
begin
IO.copy_stream(StringIO.new(changes), stdin_writer)
rescue Errno::EPIPE
# It is not an error if the hook does not consume all of its input.
end
# Close the pipe to let the hook know there is no further input.
stdin_writer.close
Process.wait(hook_pid)
$?.success?
end
def hook_file(hook_type, repo_path)
hook_path = File.join(repo_path.strip, 'custom_hooks')
hook_file = "#{hook_path}/#{hook_type}"
hook_file if File.executable?(hook_file)
end
end
|