Move GitHooksService to Gitlab::Git

This commit is contained in:
Jacob Vosmaer 2017-08-22 14:18:09 +02:00
parent 65f83941c3
commit dc7c6bede2
19 changed files with 65 additions and 60 deletions

View File

@ -17,7 +17,7 @@ module Commits
new_commit = create_commit!
success(result: new_commit)
rescue ValidationError, ChangeError, Gitlab::Git::Index::IndexError, Repository::CommitError, GitHooksService::PreReceiveError => ex
rescue ValidationError, ChangeError, Gitlab::Git::Index::IndexError, Repository::CommitError, Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message)
end

View File

@ -14,7 +14,7 @@ class CreateBranchService < BaseService
else
error('Invalid reference name')
end
rescue GitHooksService::PreReceiveError => ex
rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message)
end

View File

@ -16,7 +16,7 @@ class DeleteBranchService < BaseService
else
error('Failed to remove branch')
end
rescue GitHooksService::PreReceiveError => ex
rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message)
end

View File

@ -1,32 +0,0 @@
class GitHooksService
PreReceiveError = Class.new(StandardError)
attr_accessor :oldrev, :newrev, :ref
def execute(committer, repository, oldrev, newrev, ref)
@repository = repository
@gl_id = committer.gl_id
@oldrev = oldrev
@newrev = newrev
@ref = ref
%w(pre-receive update).each do |hook_name|
status, message = run_hook(hook_name)
unless status
raise PreReceiveError, message
end
end
yield(self).tap do
run_hook('post-receive')
end
end
private
def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repository)
hook.trigger(@gl_id, oldrev, newrev, ref)
end
end

View File

@ -121,7 +121,7 @@ class GitOperationService
end
def with_hooks(ref, newrev, oldrev)
GitHooksService.new.execute(
Gitlab::Git::HooksService.new.execute(
committer,
repository,
oldrev,

View File

@ -49,7 +49,7 @@ module MergeRequests
raise MergeError, 'Conflicts detected during merge' unless commit_id
merge_request.update(merge_commit_sha: commit_id)
rescue GitHooksService::PreReceiveError => e
rescue Gitlab::Git::HooksService::PreReceiveError => e
raise MergeError, e.message
rescue StandardError => e
raise MergeError, "Something went wrong during merge: #{e.message}"

View File

@ -13,7 +13,7 @@ module Tags
new_tag = repository.add_tag(current_user, tag_name, target, message)
rescue Rugged::TagError
return error("Tag #{tag_name} already exists")
rescue GitHooksService::PreReceiveError => ex
rescue Gitlab::Git::HooksService::PreReceiveError => ex
return error(ex.message)
end

View File

@ -21,7 +21,7 @@ module Tags
else
error('Failed to remove tag')
end
rescue GitHooksService::PreReceiveError => ex
rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message)
end

View File

@ -13,7 +13,7 @@ class ValidateNewBranchService < BaseService
end
success
rescue GitHooksService::PreReceiveError => ex
rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message)
end
end

View File

@ -15,7 +15,7 @@ class Gitlab::Seeder::CycleAnalytics
# to disable the `pre_receive` hook in order to remove this
# dependency on the GitLab API.
def stub_git_pre_receive!
GitHooksService.class_eval do
Gitlab::Git::HooksService.class_eval do
def run_hook(name)
[true, '']
end

View File

@ -1,6 +1,6 @@
# Gitaly note: JV: looks like this is only used by GitHooksService in
# Gitaly note: JV: looks like this is only used by Gitlab::Git::HooksService in
# app/services. We shouldn't bother migrating this until we know how
# GitHooksService will be migrated.
# Gitlab::Git::HooksService will be migrated.
module Gitlab
module Git

View File

@ -0,0 +1,37 @@
module Gitlab
module Git
class HooksService
PreReceiveError = Class.new(StandardError)
attr_accessor :oldrev, :newrev, :ref
def execute(committer, repository, oldrev, newrev, ref)
@repository = repository
@gl_id = committer.gl_id
@oldrev = oldrev
@newrev = newrev
@ref = ref
%w(pre-receive update).each do |hook_name|
status, message = run_hook(hook_name)
unless status
raise PreReceiveError, message
end
end
yield(self).tap do
run_hook('post-receive')
end
end
private
def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repository)
hook.trigger(@gl_id, oldrev, newrev, ref)
end
end
end
end

View File

@ -36,8 +36,8 @@ feature 'Master deletes tag' do
context 'when pre-receive hook fails', js: true do
before do
allow_any_instance_of(GitHooksService).to receive(:execute)
.and_raise(GitHooksService::PreReceiveError, 'Do not delete tags')
allow_any_instance_of(Gitlab::Git::HooksService).to receive(:execute)
.and_raise(Gitlab::Git::HooksService::PreReceiveError, 'Do not delete tags')
end
scenario 'shows the error message' do

View File

@ -384,7 +384,7 @@ describe Gitlab::GitAccess do
def stub_git_hooks
# Running the `pre-receive` hook is expensive, and not necessary for this test.
allow_any_instance_of(GitHooksService).to receive(:execute) do |service, &block|
allow_any_instance_of(Gitlab::Git::HooksService).to receive(:execute) do |service, &block|
block.call(service)
end
end

View File

@ -847,7 +847,7 @@ describe Repository, models: true do
expect do
repository.add_branch(user, 'new_feature', 'master')
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end
it 'does not create the branch' do
@ -855,7 +855,7 @@ describe Repository, models: true do
expect do
repository.add_branch(user, 'new_feature', 'master')
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
expect(repository.find_branch('new_feature')).to be_nil
end
end
@ -885,7 +885,7 @@ describe Repository, models: true do
context 'when pre hooks were successful' do
it 'runs without errors' do
expect_any_instance_of(GitHooksService).to receive(:execute)
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:execute)
.with(committer, repository, old_rev, blank_sha, 'refs/heads/feature')
expect { repository.rm_branch(user, 'feature') }.not_to raise_error
@ -906,7 +906,7 @@ describe Repository, models: true do
expect do
repository.rm_branch(user, 'feature')
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end
it 'does not delete the branch' do
@ -914,7 +914,7 @@ describe Repository, models: true do
expect do
repository.rm_branch(user, 'feature')
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
expect(repository.find_branch('feature')).not_to be_nil
end
end
@ -926,8 +926,8 @@ describe Repository, models: true do
context 'when pre hooks were successful' do
before do
service = GitHooksService.new
expect(GitHooksService).to receive(:new).and_return(service)
service = Gitlab::Git::HooksService.new
expect(Gitlab::Git::HooksService).to receive(:new).and_return(service)
expect(service).to receive(:execute)
.with(committer, repository, old_rev, new_rev, 'refs/heads/feature')
.and_yield(service).and_return(true)
@ -1030,7 +1030,7 @@ describe Repository, models: true do
GitOperationService.new(committer, repository).with_branch('feature') do
new_rev
end
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end
end

View File

@ -76,7 +76,7 @@ describe Files::UpdateService do
let(:branch_name) { "#{project.default_branch}-new" }
it 'fires hooks only once' do
expect(GitHooksService).to receive(:new).once.and_call_original
expect(Gitlab::Git::HooksService).to receive(:new).once.and_call_original
subject.execute
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
describe GitHooksService do
describe Gitlab::Git::HooksService do
include RepoHelpers
let(:user) { create(:user) }
@ -31,7 +31,7 @@ describe GitHooksService do
expect do
service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end
end
@ -43,7 +43,7 @@ describe GitHooksService do
expect do
service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError)
end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end
end
end

View File

@ -217,7 +217,7 @@ describe MergeRequests::MergeService do
it 'logs and saves error if there is an PreReceiveError exception' do
error_message = 'error message'
allow(service).to receive(:repository).and_raise(GitHooksService::PreReceiveError, error_message)
allow(service).to receive(:repository).and_raise(Gitlab::Git::HooksService::PreReceiveError, error_message)
allow(service).to receive(:execute_hooks)
service.execute(merge_request)

View File

@ -41,7 +41,7 @@ describe Tags::CreateService do
it 'returns an error' do
expect(repository).to receive(:add_tag)
.with(user, 'v1.1.0', 'master', 'Foo')
.and_raise(GitHooksService::PreReceiveError, 'something went wrong')
.and_raise(Gitlab::Git::HooksService::PreReceiveError, 'something went wrong')
response = service.execute('v1.1.0', 'master', 'Foo')