Commit 3ef7cdf9 authored by Drew Blessing's avatar Drew Blessing

Support for custom hooks

parent 00b3d2cb
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
# This file was placed here by GitLab. It makes sure that your pushed commits # This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly. # will be processed properly.
# You can add your own hooks to this file, but be careful when updating gitlab-shell!
changes = ARGF.read refs = ARGF.read
key_id = ENV['GL_ID'] key_id = ENV['GL_ID']
repo_path = Dir.pwd repo_path = Dir.pwd
require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/gitlab_post_receive' require_relative '../lib/gitlab_post_receive'
if GitlabPostReceive.new(repo_path, key_id, changes).exec if GitlabPostReceive.new(repo_path, key_id, refs).exec &&
GitlabCustomHook.new.post_receive(refs, repo_path)
exit 0 exit 0
else else
exit 1 exit 1
......
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
# This file was placed here by GitLab. It makes sure that your pushed commits # This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly. # will be processed properly.
# You can add your own hooks to this file, but be careful when updating gitlab-shell!
refs = ARGF.read refs = ARGF.read
key_id = ENV['GL_ID'] key_id = ENV['GL_ID']
repo_path = Dir.pwd repo_path = Dir.pwd
require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/gitlab_access' require_relative '../lib/gitlab_access'
if GitlabAccess.new(repo_path, key_id, refs).exec if GitlabAccess.new(repo_path, key_id, refs).exec &&
GitlabCustomHook.new.pre_receive(refs, repo_path)
exit 0 exit 0
else else
exit 1 exit 1
......
#!/usr/bin/env ruby
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
ref_name = ARGV[0]
old_value = ARGV[1]
new_value = ARGV[2]
repo_path = Dir.pwd
require_relative '../lib/gitlab_custom_hook'
if GitlabCustomHook.new.update(ref_name, old_value, new_value, repo_path)
exit 0
else
exit 1
end
...@@ -23,7 +23,7 @@ class GitlabAccess ...@@ -23,7 +23,7 @@ class GitlabAccess
# reset GL_ID env since we stop git push here # reset GL_ID env since we stop git push here
ENV['GL_ID'] = nil ENV['GL_ID'] = nil
puts "GitLab: You are not allowed to access some of the refs!" puts "GitLab: You are not allowed to access some of the refs!"
exit 1 return false
end end
end end
......
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
...@@ -23,9 +23,12 @@ class GitlabPostReceive ...@@ -23,9 +23,12 @@ class GitlabPostReceive
def update_redis def update_redis
queue = "#{config.redis_namespace}:queue:post_receive" queue = "#{config.redis_namespace}:queue:post_receive"
msg = JSON.dump({'class' => 'PostReceive', 'args' => [@repo_path, @actor, @changes]}) 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})." puts "GitLab: An unexpected error occurred (redis-cli returned #{$?.exitstatus})."
exit 1 return false
end end
end 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