diff options
author | Valery Sizov <valery@gitlab.com> | 2016-10-03 19:15:14 +0300 |
---|---|---|
committer | Valery Sizov <valery@gitlab.com> | 2016-10-05 11:42:25 +0300 |
commit | c0b473418296764764cb716686cd870e7018b242 (patch) | |
tree | 2ef6c9418ef1960f782a002bd8f1513b36efda46 | |
parent | 0b4fd0af16555b2bdc28f6b18781d72226c5d56c (diff) | |
download | gitlab-shell-add_gl_id.tar.gz |
added GL_IDadd_gl_id
-rw-r--r-- | CHANGELOG | 3 | ||||
-rwxr-xr-x | hooks/post-receive | 2 | ||||
-rwxr-xr-x | hooks/pre-receive | 2 | ||||
-rwxr-xr-x | hooks/update | 5 | ||||
-rw-r--r-- | lib/gitlab_custom_hook.rb | 12 | ||||
-rw-r--r-- | spec/gitlab_custom_hook_spec.rb | 33 | ||||
-rwxr-xr-x | spec/support/gl_id_test_hook | 2 |
7 files changed, 52 insertions, 7 deletions
@@ -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$' |