diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_access.rb | 2 | ||||
-rw-r--r-- | lib/gitlab_custom_hook.rb | 42 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 7 |
3 files changed, 48 insertions, 3 deletions
diff --git a/lib/gitlab_access.rb b/lib/gitlab_access.rb index 804d06d..78d353c 100644 --- a/lib/gitlab_access.rb +++ b/lib/gitlab_access.rb @@ -23,7 +23,7 @@ class GitlabAccess # reset GL_ID env since we stop git push here ENV['GL_ID'] = nil puts "GitLab: You are not allowed to access some of the refs!" - exit 1 + return false end end diff --git a/lib/gitlab_custom_hook.rb b/lib/gitlab_custom_hook.rb new file mode 100644 index 0000000..d6fa83d --- /dev/null +++ b/lib/gitlab_custom_hook.rb @@ -0,0 +1,42 @@ +class GitlabCustomHook + def pre_receive(refs, repo_path) + if receive('pre-receive', refs, repo_path) + return true + else + # reset GL_ID env since we stop git push here + ENV['GL_ID'] = nil + return false + end + end + + def post_receive(refs, repo_path) + receive('post-receive', refs, repo_path) + end + + def update(ref_name, old_value, new_value, repo_path) + hook = hook_file('update', repo_path) + return true if hook.nil? + system(*hook, ref_name, old_value, new_value) ? true : false + end + + private + + def receive(type, refs, repo_path) + unless type == 'pre-receive' || type == 'post-receive' + puts 'GitLab: An unexpected error occurred ' \ + '(invalid pre/post-receive hook type)' + return false + end + + hook = hook_file(type, repo_path) + return true if hook.nil? + cmd = "#{hook} #{refs}" + system(*cmd) ? true : false + end + + def hook_file(hook_type, repo_path) + hook_path = File.join(repo_path.strip, 'custom_hooks') + hook_file = "#{hook_path}/#{hook_type}" + hook_file if File.exist?(hook_file) + end +end diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index bd80408..b4770d9 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -23,9 +23,12 @@ class GitlabPostReceive def update_redis queue = "#{config.redis_namespace}:queue:post_receive" msg = JSON.dump({'class' => 'PostReceive', 'args' => [@repo_path, @actor, @changes]}) - unless system(*config.redis_command, 'rpush', queue, msg, err: '/dev/null', out: '/dev/null') + if system(*config.redis_command, 'rpush', queue, msg, + err: '/dev/null', out: '/dev/null') + return true + else puts "GitLab: An unexpected error occurred (redis-cli returned #{$?.exitstatus})." - exit 1 + return false end end end |