summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2016-10-05 08:55:07 +0000
committerSean McGivern <sean@mcgivern.me.uk>2016-10-05 08:55:07 +0000
commitfad9498c1b15e96bf553064d77dc33c17ff7baa7 (patch)
tree9b9e6cd85dd28ed8e6240cebfc958e867ca02313
parentc04e2ae0e8a7d8501f57424f4a118d05db2527bd (diff)
parentc0b473418296764764cb716686cd870e7018b242 (diff)
downloadgitlab-shell-fad9498c1b15e96bf553064d77dc33c17ff7baa7.tar.gz
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
-rw-r--r--CHANGELOG3
-rwxr-xr-xhooks/post-receive2
-rwxr-xr-xhooks/pre-receive2
-rwxr-xr-xhooks/update5
-rw-r--r--lib/gitlab_custom_hook.rb12
-rw-r--r--spec/gitlab_custom_hook_spec.rb33
-rwxr-xr-xspec/support/gl_id_test_hook2
7 files changed, 52 insertions, 7 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 54ccc82..7f5ca75 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,6 @@
+v3.6.3
+ - Re-exposing GL_ID to custom hooks
+
v3.6.2
- Enable GIT_TRACE/GIT_TRACE_PACKET/GIT_TRACE_PERFORMANCE by providing the git_trace_log_file config key
diff --git a/hooks/post-receive b/hooks/post-receive
index b7bc85e..b84d0d1 100755
--- a/hooks/post-receive
+++ b/hooks/post-receive
@@ -11,7 +11,7 @@ require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/gitlab_post_receive'
if GitlabPostReceive.new(repo_path, key_id, refs).exec &&
- GitlabCustomHook.new.post_receive(refs, repo_path)
+ GitlabCustomHook.new(key_id).post_receive(refs, repo_path)
exit 0
else
exit 1
diff --git a/hooks/pre-receive b/hooks/pre-receive
index 09fa42b..4d9a4e9 100755
--- a/hooks/pre-receive
+++ b/hooks/pre-receive
@@ -17,7 +17,7 @@ require_relative '../lib/gitlab_access'
# other hand, we run GitlabPostReceive first because the push is already done
# and we don't want to skip it if the custom hook fails.
if GitlabAccess.new(repo_path, key_id, refs, protocol).exec &&
- GitlabCustomHook.new.pre_receive(refs, repo_path) &&
+ GitlabCustomHook.new(key_id).pre_receive(refs, repo_path) &&
GitlabReferenceCounter.new(repo_path).increase
exit 0
else
diff --git a/hooks/update b/hooks/update
index f1ac8e7..223575d 100755
--- a/hooks/update
+++ b/hooks/update
@@ -3,14 +3,15 @@
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
-ref_name = ARGV[0]
+ref_name = ARGV[0]
old_value = ARGV[1]
new_value = ARGV[2]
repo_path = Dir.pwd
+key_id = ENV.delete('GL_ID')
require_relative '../lib/gitlab_custom_hook'
-if GitlabCustomHook.new.update(ref_name, old_value, new_value, repo_path)
+if GitlabCustomHook.new(key_id).update(ref_name, old_value, new_value, repo_path)
exit 0
else
exit 1
diff --git a/lib/gitlab_custom_hook.rb b/lib/gitlab_custom_hook.rb
index fe6cff3..4edb48b 100644
--- a/lib/gitlab_custom_hook.rb
+++ b/lib/gitlab_custom_hook.rb
@@ -1,6 +1,12 @@
require 'open3'
class GitlabCustomHook
+ attr_reader :vars
+
+ def initialize(key_id)
+ @vars = { 'GL_ID' => key_id }
+ end
+
def pre_receive(changes, repo_path)
hook = hook_file('pre-receive', repo_path)
return true if hook.nil?
@@ -11,7 +17,7 @@ class GitlabCustomHook
def post_receive(changes, repo_path)
hook = hook_file('post-receive', repo_path)
return true if hook.nil?
-
+
call_receive_hook(hook, changes)
end
@@ -19,7 +25,7 @@ class GitlabCustomHook
hook = hook_file('update', repo_path)
return true if hook.nil?
- system(hook, ref_name, old_value, new_value)
+ system(vars, hook, ref_name, old_value, new_value)
end
private
@@ -28,7 +34,7 @@ class GitlabCustomHook
# 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(hook, in: stdin_reader, err: :out)
+ hook_pid = spawn(vars, hook, in: stdin_reader, err: :out)
stdin_reader.close
# Submit changes to the hook via its stdin.
diff --git a/spec/gitlab_custom_hook_spec.rb b/spec/gitlab_custom_hook_spec.rb
new file mode 100644
index 0000000..f93c8b4
--- /dev/null
+++ b/spec/gitlab_custom_hook_spec.rb
@@ -0,0 +1,33 @@
+# coding: utf-8
+require 'spec_helper'
+require 'pry'
+require 'gitlab_custom_hook'
+
+describe GitlabCustomHook do
+ let(:gitlab_custom_hook) { GitlabCustomHook.new('key_1') }
+ let(:hook_path) { File.join(ROOT_PATH, 'spec/support/gl_id_test_hook') }
+
+ context 'pre_receive hook' do
+ it 'passes GL_ID variable to hook' do
+ allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
+
+ expect(gitlab_custom_hook.pre_receive('changes', 'repo_path')).to be_true
+ end
+ end
+
+ context 'post_receive hook' do
+ it 'passes GL_ID variable to hook' do
+ allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
+
+ expect(gitlab_custom_hook.post_receive('changes', 'repo_path')).to be_true
+ end
+ end
+
+ context 'update hook' do
+ it 'passes GL_ID variable to hook' do
+ allow(gitlab_custom_hook).to receive(:hook_file).and_return(hook_path)
+
+ expect(gitlab_custom_hook.update('master', '', '', 'repo_path')).to be_true
+ end
+ end
+end
diff --git a/spec/support/gl_id_test_hook b/spec/support/gl_id_test_hook
new file mode 100755
index 0000000..bf5874d
--- /dev/null
+++ b/spec/support/gl_id_test_hook
@@ -0,0 +1,2 @@
+#!/bin/sh
+printenv GL_ID | grep -q '^key_1$'