Skip to content

Commit fad9498

Browse files
committed
Merge branch 'add_gl_id' into 'master'
Re-exposing GL_ID to custom hooks closes https://gitlab.com/gitlab-org/gitlab-ee/issues/995 closes https://gitlab.com/gitlab-org/gitlab-shell/issues/53 See merge request !95
2 parents c04e2ae + c0b4734 commit fad9498

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v3.6.3
2+
- Re-exposing GL_ID to custom hooks
3+
14
v3.6.2
25
- Enable GIT_TRACE/GIT_TRACE_PACKET/GIT_TRACE_PERFORMANCE by providing the git_trace_log_file config key
36

hooks/post-receive

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require_relative '../lib/gitlab_custom_hook'
1111
require_relative '../lib/gitlab_post_receive'
1212

1313
if GitlabPostReceive.new(repo_path, key_id, refs).exec &&
14-
GitlabCustomHook.new.post_receive(refs, repo_path)
14+
GitlabCustomHook.new(key_id).post_receive(refs, repo_path)
1515
exit 0
1616
else
1717
exit 1

hooks/pre-receive

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require_relative '../lib/gitlab_access'
1717
# other hand, we run GitlabPostReceive first because the push is already done
1818
# and we don't want to skip it if the custom hook fails.
1919
if GitlabAccess.new(repo_path, key_id, refs, protocol).exec &&
20-
GitlabCustomHook.new.pre_receive(refs, repo_path) &&
20+
GitlabCustomHook.new(key_id).pre_receive(refs, repo_path) &&
2121
GitlabReferenceCounter.new(repo_path).increase
2222
exit 0
2323
else

hooks/update

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
# This file was placed here by GitLab. It makes sure that your pushed commits
44
# will be processed properly.
55

6-
ref_name = ARGV[0]
6+
ref_name = ARGV[0]
77
old_value = ARGV[1]
88
new_value = ARGV[2]
99
repo_path = Dir.pwd
10+
key_id = ENV.delete('GL_ID')
1011

1112
require_relative '../lib/gitlab_custom_hook'
1213

13-
if GitlabCustomHook.new.update(ref_name, old_value, new_value, repo_path)
14+
if GitlabCustomHook.new(key_id).update(ref_name, old_value, new_value, repo_path)
1415
exit 0
1516
else
1617
exit 1

lib/gitlab_custom_hook.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
require 'open3'
22

33
class GitlabCustomHook
4+
attr_reader :vars
5+
6+
def initialize(key_id)
7+
@vars = { 'GL_ID' => key_id }
8+
end
9+
410
def pre_receive(changes, repo_path)
511
hook = hook_file('pre-receive', repo_path)
612
return true if hook.nil?
@@ -11,15 +17,15 @@ def pre_receive(changes, repo_path)
1117
def post_receive(changes, repo_path)
1218
hook = hook_file('post-receive', repo_path)
1319
return true if hook.nil?
14-
20+
1521
call_receive_hook(hook, changes)
1622
end
1723

1824
def update(ref_name, old_value, new_value, repo_path)
1925
hook = hook_file('update', repo_path)
2026
return true if hook.nil?
2127

22-
system(hook, ref_name, old_value, new_value)
28+
system(vars, hook, ref_name, old_value, new_value)
2329
end
2430

2531
private
@@ -28,7 +34,7 @@ def call_receive_hook(hook, changes)
2834
# Prepare the hook subprocess. Attach a pipe to its stdin, and merge
2935
# both its stdout and stderr into our own stdout.
3036
stdin_reader, stdin_writer = IO.pipe
31-
hook_pid = spawn(hook, in: stdin_reader, err: :out)
37+
hook_pid = spawn(vars, hook, in: stdin_reader, err: :out)
3238
stdin_reader.close
3339

3440
# Submit changes to the hook via its stdin.

spec/gitlab_custom_hook_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# coding: utf-8
2+
require 'spec_helper'
3+
require 'pry'
4+
require 'gitlab_custom_hook'
5+
6+
describe GitlabCustomHook do
7+
let(:gitlab_custom_hook) { GitlabCustomHook.new('key_1') }
8+
let(:hook_path) { File.join(ROOT_PATH, 'spec/support/gl_id_test_hook') }
9+
10+
context 'pre_receive hook' do
11+
it 'passes GL_ID variable to hook' do
12+
allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
13+
14+
expect(gitlab_custom_hook.pre_receive('changes', 'repo_path')).to be_true
15+
end
16+
end
17+
18+
context 'post_receive hook' do
19+
it 'passes GL_ID variable to hook' do
20+
allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
21+
22+
expect(gitlab_custom_hook.post_receive('changes', 'repo_path')).to be_true
23+
end
24+
end
25+
26+
context 'update hook' do
27+
it 'passes GL_ID variable to hook' do
28+
allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
29+
30+
expect(gitlab_custom_hook.update('master', '', '', 'repo_path')).to be_true
31+
end
32+
end
33+
end

spec/support/gl_id_test_hook

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
printenv GL_ID | grep -q '^key_1$'

0 commit comments

Comments
 (0)