Commit dc7c6bed authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move GitHooksService to Gitlab::Git

parent 65f83941
...@@ -17,7 +17,7 @@ module Commits ...@@ -17,7 +17,7 @@ module Commits
new_commit = create_commit! new_commit = create_commit!
success(result: new_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) error(ex.message)
end end
......
...@@ -14,7 +14,7 @@ class CreateBranchService < BaseService ...@@ -14,7 +14,7 @@ class CreateBranchService < BaseService
else else
error('Invalid reference name') error('Invalid reference name')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
...@@ -16,7 +16,7 @@ class DeleteBranchService < BaseService ...@@ -16,7 +16,7 @@ class DeleteBranchService < BaseService
else else
error('Failed to remove branch') error('Failed to remove branch')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
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
...@@ -121,7 +121,7 @@ class GitOperationService ...@@ -121,7 +121,7 @@ class GitOperationService
end end
def with_hooks(ref, newrev, oldrev) def with_hooks(ref, newrev, oldrev)
GitHooksService.new.execute( Gitlab::Git::HooksService.new.execute(
committer, committer,
repository, repository,
oldrev, oldrev,
......
...@@ -49,7 +49,7 @@ module MergeRequests ...@@ -49,7 +49,7 @@ module MergeRequests
raise MergeError, 'Conflicts detected during merge' unless commit_id raise MergeError, 'Conflicts detected during merge' unless commit_id
merge_request.update(merge_commit_sha: commit_id) merge_request.update(merge_commit_sha: commit_id)
rescue GitHooksService::PreReceiveError => e rescue Gitlab::Git::HooksService::PreReceiveError => e
raise MergeError, e.message raise MergeError, e.message
rescue StandardError => e rescue StandardError => e
raise MergeError, "Something went wrong during merge: #{e.message}" raise MergeError, "Something went wrong during merge: #{e.message}"
......
...@@ -13,7 +13,7 @@ module Tags ...@@ -13,7 +13,7 @@ module Tags
new_tag = repository.add_tag(current_user, tag_name, target, message) new_tag = repository.add_tag(current_user, tag_name, target, message)
rescue Rugged::TagError rescue Rugged::TagError
return error("Tag #{tag_name} already exists") return error("Tag #{tag_name} already exists")
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
return error(ex.message) return error(ex.message)
end end
......
...@@ -21,7 +21,7 @@ module Tags ...@@ -21,7 +21,7 @@ module Tags
else else
error('Failed to remove tag') error('Failed to remove tag')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
...@@ -13,7 +13,7 @@ class ValidateNewBranchService < BaseService ...@@ -13,7 +13,7 @@ class ValidateNewBranchService < BaseService
end end
success success
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
end end
...@@ -15,7 +15,7 @@ class Gitlab::Seeder::CycleAnalytics ...@@ -15,7 +15,7 @@ class Gitlab::Seeder::CycleAnalytics
# to disable the `pre_receive` hook in order to remove this # to disable the `pre_receive` hook in order to remove this
# dependency on the GitLab API. # dependency on the GitLab API.
def stub_git_pre_receive! def stub_git_pre_receive!
GitHooksService.class_eval do Gitlab::Git::HooksService.class_eval do
def run_hook(name) def run_hook(name)
[true, ''] [true, '']
end end
......
# 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 # 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 Gitlab
module Git module Git
......
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
...@@ -36,8 +36,8 @@ feature 'Master deletes tag' do ...@@ -36,8 +36,8 @@ feature 'Master deletes tag' do
context 'when pre-receive hook fails', js: true do context 'when pre-receive hook fails', js: true do
before do before do
allow_any_instance_of(GitHooksService).to receive(:execute) allow_any_instance_of(Gitlab::Git::HooksService).to receive(:execute)
.and_raise(GitHooksService::PreReceiveError, 'Do not delete tags') .and_raise(Gitlab::Git::HooksService::PreReceiveError, 'Do not delete tags')
end end
scenario 'shows the error message' do scenario 'shows the error message' do
......
...@@ -384,7 +384,7 @@ describe Gitlab::GitAccess do ...@@ -384,7 +384,7 @@ describe Gitlab::GitAccess do
def stub_git_hooks def stub_git_hooks
# Running the `pre-receive` hook is expensive, and not necessary for this test. # 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) block.call(service)
end end
end end
......
...@@ -847,7 +847,7 @@ describe Repository, models: true do ...@@ -847,7 +847,7 @@ describe Repository, models: true do
expect do expect do
repository.add_branch(user, 'new_feature', 'master') repository.add_branch(user, 'new_feature', 'master')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
it 'does not create the branch' do it 'does not create the branch' do
...@@ -855,7 +855,7 @@ describe Repository, models: true do ...@@ -855,7 +855,7 @@ describe Repository, models: true do
expect do expect do
repository.add_branch(user, 'new_feature', 'master') 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 expect(repository.find_branch('new_feature')).to be_nil
end end
end end
...@@ -885,7 +885,7 @@ describe Repository, models: true do ...@@ -885,7 +885,7 @@ describe Repository, models: true do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
it 'runs without errors' 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') .with(committer, repository, old_rev, blank_sha, 'refs/heads/feature')
expect { repository.rm_branch(user, 'feature') }.not_to raise_error expect { repository.rm_branch(user, 'feature') }.not_to raise_error
...@@ -906,7 +906,7 @@ describe Repository, models: true do ...@@ -906,7 +906,7 @@ describe Repository, models: true do
expect do expect do
repository.rm_branch(user, 'feature') repository.rm_branch(user, 'feature')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
it 'does not delete the branch' do it 'does not delete the branch' do
...@@ -914,7 +914,7 @@ describe Repository, models: true do ...@@ -914,7 +914,7 @@ describe Repository, models: true do
expect do expect do
repository.rm_branch(user, 'feature') 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 expect(repository.find_branch('feature')).not_to be_nil
end end
end end
...@@ -926,8 +926,8 @@ describe Repository, models: true do ...@@ -926,8 +926,8 @@ describe Repository, models: true do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
before do before do
service = GitHooksService.new service = Gitlab::Git::HooksService.new
expect(GitHooksService).to receive(:new).and_return(service) expect(Gitlab::Git::HooksService).to receive(:new).and_return(service)
expect(service).to receive(:execute) expect(service).to receive(:execute)
.with(committer, repository, old_rev, new_rev, 'refs/heads/feature') .with(committer, repository, old_rev, new_rev, 'refs/heads/feature')
.and_yield(service).and_return(true) .and_yield(service).and_return(true)
...@@ -1030,7 +1030,7 @@ describe Repository, models: true do ...@@ -1030,7 +1030,7 @@ describe Repository, models: true do
GitOperationService.new(committer, repository).with_branch('feature') do GitOperationService.new(committer, repository).with_branch('feature') do
new_rev new_rev
end end
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
......
...@@ -76,7 +76,7 @@ describe Files::UpdateService do ...@@ -76,7 +76,7 @@ describe Files::UpdateService do
let(:branch_name) { "#{project.default_branch}-new" } let(:branch_name) { "#{project.default_branch}-new" }
it 'fires hooks only once' do 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 subject.execute
end end
......
require 'spec_helper' require 'spec_helper'
describe GitHooksService do describe Gitlab::Git::HooksService do
include RepoHelpers include RepoHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -31,7 +31,7 @@ describe GitHooksService do ...@@ -31,7 +31,7 @@ describe GitHooksService do
expect do expect do
service.execute(user, project, @blankrev, @newrev, @ref) service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
...@@ -43,7 +43,7 @@ describe GitHooksService do ...@@ -43,7 +43,7 @@ describe GitHooksService do
expect do expect do
service.execute(user, project, @blankrev, @newrev, @ref) service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
end end
......
...@@ -217,7 +217,7 @@ describe MergeRequests::MergeService do ...@@ -217,7 +217,7 @@ describe MergeRequests::MergeService do
it 'logs and saves error if there is an PreReceiveError exception' do it 'logs and saves error if there is an PreReceiveError exception' do
error_message = 'error message' 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) allow(service).to receive(:execute_hooks)
service.execute(merge_request) service.execute(merge_request)
......
...@@ -41,7 +41,7 @@ describe Tags::CreateService do ...@@ -41,7 +41,7 @@ describe Tags::CreateService do
it 'returns an error' do it 'returns an error' do
expect(repository).to receive(:add_tag) expect(repository).to receive(:add_tag)
.with(user, 'v1.1.0', 'master', 'Foo') .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') response = service.execute('v1.1.0', 'master', 'Foo')
......
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