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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
require 'spec_helper'
describe Gitlab::Git::Hook do
include TestRepo
describe '.directory' do
it 'does not raise an KeyError' do
expect { described_class.directory }.not_to raise_error
end
end
describe '#trigger' do
let(:tmp_dir) { Dir.mktmpdir }
let(:hook_names) { %w[pre-receive post-receive update] }
let(:repo) { gitlab_git_from_gitaly(test_repo_read_only) }
let(:push_options) do
Gitlab::Git::PushOptions.new(['ci.skip'])
end
def trigger_with_stub_data(hook, push_options)
hook.trigger('user-1', 'admin', '0' * 40, 'a' * 40, 'master', push_options: push_options)
end
before do
hook_names.each do |f|
path = File.join(tmp_dir, f)
File.write(path, script)
FileUtils.chmod("u+x", path)
end
allow(Gitlab.config.git).to receive(:hooks_directory).and_return(tmp_dir)
end
after do
FileUtils.remove_entry(tmp_dir)
end
context 'when the hooks require environment variables' do
let(:vars) do
{
'GITALY_HOOKS_PAYLOAD' => Base64.strict_encode64({
repository: repo.gitaly_repository.to_json,
binary_directory: Gitlab.config.gitaly.bin_dir,
git_path: Gitlab.config.git.bin_path,
internal_socket: Gitlab.config.gitaly.internal_socket,
internal_socket_token: nil,
user_details: {
userid: 'user-123',
username: 'janedoe',
protocol: 'web'
}
}.to_json),
'PWD' => repo.path,
'GIT_DIR' => repo.path
}
end
let(:script) do
[
"#!/bin/sh",
vars.map do |key, value|
<<-SCRIPT
if [ x$#{key} != x#{value} ]; then
echo "unexpected value: #{key}=$#{key}"
exit 1
fi
SCRIPT
end.join,
"exit 0"
].join("\n")
end
it 'returns true' do
hook_names.each do |hook|
trigger_result = described_class.new(hook, repo)
.trigger('user-123', 'janedoe', '0' * 40, 'a' * 40, 'master', push_options: push_options)
expect(trigger_result.first).to be(true), "#{hook} failed: #{trigger_result.last}"
end
end
end
context 'when the hooks are successful' do
let(:script) { "#!/bin/sh\nexit 0\n" }
it 'returns true' do
hook_names.each do |hook|
trigger_result = described_class.new(hook, repo)
.trigger('user-456', 'admin', '0' * 40, 'a' * 40, 'master', push_options: push_options)
expect(trigger_result.first).to be(true)
end
end
end
context 'when the hooks fail' do
let(:script) { "#!/bin/sh\nexit 1\n" }
it 'returns false' do
hook_names.each do |name|
hook = described_class.new(name, repo)
trigger_result = trigger_with_stub_data(hook, push_options)
expect(trigger_result.first).to be(false)
end
end
end
context 'when push options are passed' do
let(:script) do
<<~HOOK
#!/usr/bin/env ruby
unless ENV['GIT_PUSH_OPTION_COUNT'] == '1' && ENV['GIT_PUSH_OPTION_0'] == 'ci.skip'
abort 'missing GIT_PUSH_OPTION env vars'
end
HOOK
end
context 'for pre-receive and post-receive hooks' do
let(:hooks) do
%w[pre-receive post-receive].map { |name| described_class.new(name, repo) }
end
it 'sets the push options environment variables' do
hooks.each do |hook|
trigger_result = trigger_with_stub_data(hook, push_options)
expect(trigger_result.first).to be(true)
end
end
end
context 'for update hook' do
let(:hook) { described_class.new('update', repo) }
it 'does not set the push options environment variables' do
trigger_result = trigger_with_stub_data(hook, push_options)
expect(trigger_result.first).to be(false)
end
end
end
end
end
|