Commit d05522de authored by Dirk Hörner's avatar Dirk Hörner Committed by Sean McGivern

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.
parent 2d774eea
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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) }
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment