diff options
author | Dirk Hörner <dirker@gmail.com> | 2016-09-05 11:59:25 +0200 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2016-12-01 11:40:11 +0000 |
commit | d05522de85dcdfa91349c0d9fc78bf72931d6a39 (patch) | |
tree | 30326e051959be5a7ddf6539b3c63fa48aa7330c | |
parent | 2d774eeae8ccfb211cc6ab6aeab5db600f3fdc7c (diff) | |
download | gitlab-shell-d05522de85dcdfa91349c0d9fc78bf72931d6a39.tar.gz |
custom_hook: refactor to pull repo_path into class
This commit takes the GitlabCustomHook a bit clother to the other hook
handling classes by receiving the repo_path as argument to initialize()
instead of passing it to each method.
-rwxr-xr-x | hooks/post-receive | 2 | ||||
-rwxr-xr-x | hooks/pre-receive | 2 | ||||
-rwxr-xr-x | hooks/update | 2 | ||||
-rw-r--r-- | lib/gitlab_custom_hook.rb | 15 | ||||
-rw-r--r-- | spec/gitlab_custom_hook_spec.rb | 8 |
5 files changed, 15 insertions, 14 deletions
diff --git a/hooks/post-receive b/hooks/post-receive index b84d0d1..7877306 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(key_id).post_receive(refs, repo_path) + GitlabCustomHook.new(repo_path, key_id).post_receive(refs) exit 0 else exit 1 diff --git a/hooks/pre-receive b/hooks/pre-receive index 4d9a4e9..1b16fd0 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(key_id).pre_receive(refs, repo_path) && + GitlabCustomHook.new(repo_path, key_id).pre_receive(refs) && GitlabReferenceCounter.new(repo_path).increase exit 0 else diff --git a/hooks/update b/hooks/update index 223575d..4c2fc08 100755 --- a/hooks/update +++ b/hooks/update @@ -11,7 +11,7 @@ key_id = ENV.delete('GL_ID') require_relative '../lib/gitlab_custom_hook' -if GitlabCustomHook.new(key_id).update(ref_name, old_value, new_value, repo_path) +if GitlabCustomHook.new(repo_path, key_id).update(ref_name, old_value, new_value) exit 0 else exit 1 diff --git a/lib/gitlab_custom_hook.rb b/lib/gitlab_custom_hook.rb index e84d702..0187e1e 100644 --- a/lib/gitlab_custom_hook.rb +++ b/lib/gitlab_custom_hook.rb @@ -5,26 +5,27 @@ require_relative 'gitlab_metrics' class GitlabCustomHook attr_reader :vars - def initialize(key_id) + def initialize(repo_path, key_id) + @repo_path = repo_path @vars = { 'GL_ID' => key_id } end - def pre_receive(changes, repo_path) - hook = hook_file('pre-receive', repo_path) + def pre_receive(changes) + hook = hook_file('pre-receive', @repo_path) return true if hook.nil? GitlabMetrics.measure("pre-receive-hook") { call_receive_hook(hook, changes) } end - def post_receive(changes, repo_path) - hook = hook_file('post-receive', repo_path) + def post_receive(changes) + hook = hook_file('post-receive', @repo_path) return true if hook.nil? GitlabMetrics.measure("post-receive-hook") { call_receive_hook(hook, changes) } end - def update(ref_name, old_value, new_value, repo_path) - hook = hook_file('update', repo_path) + def update(ref_name, old_value, new_value) + hook = hook_file('update', @repo_path) return true if hook.nil? GitlabMetrics.measure("update-hook") { system(vars, hook, ref_name, old_value, new_value) } diff --git a/spec/gitlab_custom_hook_spec.rb b/spec/gitlab_custom_hook_spec.rb index f93c8b4..328b925 100644 --- a/spec/gitlab_custom_hook_spec.rb +++ b/spec/gitlab_custom_hook_spec.rb @@ -4,14 +4,14 @@ require 'pry' require 'gitlab_custom_hook' describe GitlabCustomHook do - let(:gitlab_custom_hook) { GitlabCustomHook.new('key_1') } + let(:gitlab_custom_hook) { GitlabCustomHook.new('repo_path', '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 + expect(gitlab_custom_hook.pre_receive('changes')).to be_true end end @@ -19,7 +19,7 @@ describe GitlabCustomHook 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 + expect(gitlab_custom_hook.post_receive('changes')).to be_true end end @@ -27,7 +27,7 @@ describe GitlabCustomHook 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 + expect(gitlab_custom_hook.update('master', '', '')).to be_true end end end |