Implement commit transaction with pre-receive and post-receive hooks for web editor

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2015-08-13 12:57:24 +02:00
parent 69c193e78a
commit 34690142bf
No known key found for this signature in database
GPG Key ID: 161B5D6A44D3D88A
9 changed files with 122 additions and 13 deletions

View File

@ -38,7 +38,7 @@ gem "browser", '~> 0.8.0'
# Extracting information from a git repository
# Provide access to Gitlab::Git library
gem "gitlab_git", '~> 7.2.12'
gem "gitlab_git", '~> 7.2.13'
# Ruby/Rack Git Smart-HTTP Server Handler
# GitLab fork with a lot of changes (improved thread-safety, better memory usage etc)

View File

@ -271,7 +271,7 @@ GEM
mime-types (~> 1.19)
gitlab_emoji (0.1.0)
gemojione (~> 2.0)
gitlab_git (7.2.12)
gitlab_git (7.2.13)
activesupport (~> 4.0)
charlock_holmes (~> 0.6)
gitlab-linguist (~> 3.0)
@ -783,7 +783,7 @@ DEPENDENCIES
gitlab-grack (~> 2.0.2)
gitlab-linguist (~> 3.0.1)
gitlab_emoji (~> 0.1)
gitlab_git (~> 7.2.12)
gitlab_git (~> 7.2.13)
gitlab_meta (= 7.0)
gitlab_omniauth-ldap (= 1.2.1)
gollum-lib (~> 4.0.2)
@ -875,4 +875,4 @@ DEPENDENCIES
wikicloth (= 0.8.1)
BUNDLED WITH
1.10.4
1.10.6

View File

@ -373,7 +373,7 @@ class Repository
options[:author] = committer
options[:commit] = {
message: message,
branch: ref
branch: ref,
}
options[:file] = {

View File

@ -0,0 +1,37 @@
require 'securerandom'
class CommitService
def self.transaction(project, current_user, ref)
repository = project.repository
path_to_repo = repository.path_to_repo
# Create temporary ref
random_string = SecureRandom.hex
tmp_ref = "refs/tmp/#{random_string}/head"
target = repository.find_branch(ref).target
repository.rugged.references.create(tmp_ref, target)
# Make commit in tmp ref
sha = yield(tmp_ref)
unless sha
raise 'Failed to create commit'
end
# Run GitLab pre-receive hook
status = PreCommitService.new(project, current_user).execute(sha, ref)
if status
# Update head
repository.rugged.references.update(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
# Run GitLab post receive hook
PostCommitService.new(project, current_user).execute(sha, ref)
else
# Remove tmp ref and return error to user
repository.rugged.references.delete(tmp_ref)
raise 'Commit was rejected by pre-reveive hook'
end
end
end

View File

@ -22,7 +22,6 @@ module Files
end
if sha = commit
after_commit(sha, @target_branch)
success
else
error("Something went wrong. Your changes were not committed")
@ -33,10 +32,6 @@ module Files
private
def after_commit(sha, branch)
PostCommitService.new(project, current_user).execute(sha, branch)
end
def current_branch
@current_branch ||= params[:current_branch]
end

View File

@ -3,7 +3,9 @@ require_relative "base_service"
module Files
class CreateService < Files::BaseService
def commit
repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch)
CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref)
end
end
def validate

View File

@ -3,7 +3,9 @@ require_relative "base_service"
module Files
class DeleteService < Files::BaseService
def commit
repository.remove_file(current_user, @file_path, @commit_message, @target_branch)
CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.remove_file(current_user, @file_path, @commit_message, tmp_ref)
end
end
end
end

View File

@ -3,7 +3,9 @@ require_relative "base_service"
module Files
class UpdateService < Files::BaseService
def commit
repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch)
CommitService.transaction(project, current_user, @target_branch) do |tmp_ref|
repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref)
end
end
end
end

View File

@ -0,0 +1,71 @@
class PreCommitService < BaseService
include Gitlab::Popen
attr_reader :changes, :repo_path
def execute(sha, branch)
commit = repository.commit(sha)
full_ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
old_sha = commit.parent_id || Gitlab::Git::BLANK_SHA
@changes = "#{old_sha} #{sha} #{full_ref}"
@repo_path = repository.path_to_repo
pre_receive
end
private
def pre_receive
hook = hook_file('pre-receive', repo_path)
return true if hook.nil?
call_receive_hook(hook)
end
def call_receive_hook(hook)
# function will return true if succesful
exit_status = false
vars = {
'GL_ID' => Gitlab::ShellEnv.gl_id(current_user),
'PWD' => repo_path
}
options = {
chdir: repo_path
}
# we combine both stdout and stderr as we don't know what stream
# will be used by the custom hook
Open3.popen2e(vars, hook, options) do |stdin, stdout_stderr, wait_thr|
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
# need to close stdin before reading stdout
stdin.close
# only output stdut_stderr if scripts doesn't return 0
unless wait_thr.value == 0
exit_status = false
end
end
exit_status
end
def hook_file(hook_type, repo_path)
hook_path = File.join(repo_path.strip, 'hooks')
hook_file = "#{hook_path}/#{hook_type}"
hook_file if File.exist?(hook_file)
end
end