2017-08-22 08:18:09 -04:00
|
|
|
# Gitaly note: JV: looks like this is only used by Gitlab::Git::HooksService in
|
2017-07-12 10:31:36 -04:00
|
|
|
# app/services. We shouldn't bother migrating this until we know how
|
2017-08-22 08:18:09 -04:00
|
|
|
# Gitlab::Git::HooksService will be migrated.
|
2017-07-12 10:31:36 -04:00
|
|
|
|
2015-08-14 10:00:36 -04:00
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Hook
|
2016-06-23 10:53:21 -04:00
|
|
|
GL_PROTOCOL = 'web'.freeze
|
2017-08-22 07:54:14 -04:00
|
|
|
attr_reader :name, :path, :repository
|
2015-08-14 10:00:36 -04:00
|
|
|
|
2017-08-22 07:54:14 -04:00
|
|
|
def initialize(name, repository)
|
2015-08-14 10:00:36 -04:00
|
|
|
@name = name
|
2017-08-22 07:54:14 -04:00
|
|
|
@repository = repository
|
2015-08-14 10:00:36 -04:00
|
|
|
@path = File.join(repo_path.strip, 'hooks', name)
|
|
|
|
end
|
|
|
|
|
2017-08-22 07:54:14 -04:00
|
|
|
def repo_path
|
|
|
|
repository.path
|
|
|
|
end
|
|
|
|
|
2015-08-14 10:00:36 -04:00
|
|
|
def exists?
|
|
|
|
File.exist?(path)
|
|
|
|
end
|
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
def trigger(gl_id, gl_username, oldrev, newrev, ref)
|
2016-06-17 01:51:17 -04:00
|
|
|
return [true, nil] unless exists?
|
2015-08-14 10:00:36 -04:00
|
|
|
|
2016-09-14 09:15:26 -04:00
|
|
|
Bundler.with_clean_env do
|
|
|
|
case name
|
|
|
|
when "pre-receive", "post-receive"
|
2017-08-02 14:14:50 -04:00
|
|
|
call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
|
2016-09-14 09:15:26 -04:00
|
|
|
when "update"
|
2017-08-02 14:14:50 -04:00
|
|
|
call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
|
2016-09-14 09:15:26 -04:00
|
|
|
end
|
2015-11-24 06:35:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
|
2015-08-14 10:00:36 -04:00
|
|
|
changes = [oldrev, newrev, ref].join(" ")
|
|
|
|
|
|
|
|
exit_status = false
|
2016-07-04 08:30:22 -04:00
|
|
|
exit_message = nil
|
2015-08-14 10:00:36 -04:00
|
|
|
|
|
|
|
vars = {
|
|
|
|
'GL_ID' => gl_id,
|
2017-08-02 14:14:50 -04:00
|
|
|
'GL_USERNAME' => gl_username,
|
2016-06-20 21:40:56 -04:00
|
|
|
'PWD' => repo_path,
|
2017-06-29 15:22:40 -04:00
|
|
|
'GL_PROTOCOL' => GL_PROTOCOL,
|
2017-08-22 07:54:14 -04:00
|
|
|
'GL_REPOSITORY' => repository.gl_repository
|
2015-08-14 10:00:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
chdir: repo_path
|
|
|
|
}
|
|
|
|
|
2016-07-04 13:04:29 -04:00
|
|
|
Open3.popen3(vars, path, options) do |stdin, stdout, stderr, wait_thr|
|
2015-08-14 10:00:36 -04:00
|
|
|
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-07-04 13:04:29 -04:00
|
|
|
exit_message = retrieve_error_message(stderr, stdout)
|
2015-08-14 10:00:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-04 08:30:22 -04:00
|
|
|
[exit_status, exit_message]
|
2015-08-14 10:00:36 -04:00
|
|
|
end
|
2015-11-24 06:35:07 -05:00
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
def call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
|
2018-02-06 13:19:48 -05:00
|
|
|
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-11-24 06:35:07 -05:00
|
|
|
end
|
2016-07-04 13:04:29 -04:00
|
|
|
|
|
|
|
def retrieve_error_message(stderr, stdout)
|
2017-09-11 22:50:12 -04:00
|
|
|
err_message = stderr.read
|
|
|
|
err_message = err_message.blank? ? stdout.read : err_message
|
|
|
|
err_message.gsub(/\R/, "<br>").html_safe
|
2016-07-04 13:04:29 -04:00
|
|
|
end
|
2015-08-14 10:00:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|