debian-mirror-gitlab/lib/gitlab/git/hook.rb

109 lines
2.8 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# Gitaly note: JV: looks like this is only used by Gitlab::Git::HooksService in
2017-09-10 17:25:29 +05:30
# app/services. We shouldn't bother migrating this until we know how
2018-03-17 18:26:18 +05:30
# Gitlab::Git::HooksService will be migrated.
2017-09-10 17:25:29 +05:30
2015-09-25 12:07:36 +05:30
module Gitlab
module Git
class Hook
2016-08-24 12:49:21 +05:30
GL_PROTOCOL = 'web'.freeze
2018-03-17 18:26:18 +05:30
attr_reader :name, :path, :repository
2015-09-25 12:07:36 +05:30
2018-03-17 18:26:18 +05:30
def initialize(name, repository)
2015-09-25 12:07:36 +05:30
@name = name
2018-03-17 18:26:18 +05:30
@repository = repository
2015-09-25 12:07:36 +05:30
@path = File.join(repo_path.strip, 'hooks', name)
end
2018-03-17 18:26:18 +05:30
def repo_path
repository.path
end
2015-09-25 12:07:36 +05:30
def exists?
File.exist?(path)
end
2018-03-17 18:26:18 +05:30
def trigger(gl_id, gl_username, oldrev, newrev, ref)
2016-08-24 12:49:21 +05:30
return [true, nil] unless exists?
2015-09-25 12:07:36 +05:30
2016-09-29 09:46:39 +05:30
Bundler.with_clean_env do
case name
when "pre-receive", "post-receive"
2018-03-17 18:26:18 +05:30
call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
2016-09-29 09:46:39 +05:30
when "update"
2018-03-17 18:26:18 +05:30
call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
2016-09-29 09:46:39 +05:30
end
2015-12-23 02:04:40 +05:30
end
end
private
2018-03-17 18:26:18 +05:30
def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
2015-09-25 12:07:36 +05:30
changes = [oldrev, newrev, ref].join(" ")
exit_status = false
2016-08-24 12:49:21 +05:30
exit_message = nil
2015-09-25 12:07:36 +05:30
vars = {
'GL_ID' => gl_id,
2018-03-17 18:26:18 +05:30
'GL_USERNAME' => gl_username,
2016-08-24 12:49:21 +05:30
'PWD' => repo_path,
2017-09-10 17:25:29 +05:30
'GL_PROTOCOL' => GL_PROTOCOL,
2018-03-17 18:26:18 +05:30
'GL_REPOSITORY' => repository.gl_repository
2015-09-25 12:07:36 +05:30
}
options = {
chdir: repo_path
}
2016-08-24 12:49:21 +05:30
Open3.popen3(vars, path, options) do |stdin, stdout, stderr, wait_thr|
2015-09-25 12:07:36 +05:30
exit_status = true
stdin.sync = true
# in git, pre- and post- receive hooks may just exit without
# reading stdin. We catch the exception to avoid a broken pipe
# warning
begin
# inject all the changes as stdin to the hook
changes.lines do |line|
stdin.puts line
end
rescue Errno::EPIPE
end
stdin.close
unless wait_thr.value == 0
exit_status = false
2016-08-24 12:49:21 +05:30
exit_message = retrieve_error_message(stderr, stdout)
2015-09-25 12:07:36 +05:30
end
end
2016-08-24 12:49:21 +05:30
[exit_status, exit_message]
2015-09-25 12:07:36 +05:30
end
2015-12-23 02:04:40 +05:30
2018-03-17 18:26:18 +05:30
def call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
env = {
'GL_ID' => gl_id,
'GL_USERNAME' => gl_username,
'PWD' => repo_path
}
options = {
chdir: repo_path
}
args = [ref, oldrev, newrev]
stdout, stderr, status = Open3.capture3(env, path, *args, options)
[status.success?, (stderr.presence || stdout).gsub(/\R/, "<br>").html_safe]
2015-12-23 02:04:40 +05:30
end
2016-08-24 12:49:21 +05:30
def retrieve_error_message(stderr, stdout)
2018-03-17 18:26:18 +05:30
err_message = stderr.read
err_message = err_message.blank? ? stdout.read : err_message
err_message.gsub(/\R/, "<br>").html_safe
2016-08-24 12:49:21 +05:30
end
2015-09-25 12:07:36 +05:30
end
end
end