Commit bbaba3d1 authored by Stan Hu's avatar Stan Hu

Merge branch 'osw-bypass-push-rules-for-merge-to-ref' into 'master'

Bypass push rules for merge to ref service

Closes gitlab-ce#64184

See merge request gitlab-org/gitlab-ee!14577
parents fc7218ff 1fb48a0b
......@@ -28,6 +28,17 @@ module MergeRequests
private
def check_source
unless source
raise_error('No source for merge')
end
end
# Overridden in EE.
def check_size_limit
# No-op
end
# Overridden in EE.
def error_check!
# No-op
......
......@@ -48,13 +48,13 @@ module MergeRequests
def error_check!
super
check_source
error =
if @merge_request.should_be_rebased?
'Only fast-forward merge is allowed for your project. Please update your source branch'
elsif !@merge_request.mergeable?
'Merge request is not mergeable'
elsif !source
'No source for merge'
end
raise_error(error) if error
......
......@@ -16,7 +16,7 @@ module MergeRequests
def execute(merge_request)
@merge_request = merge_request
validate!
error_check!
commit_id = commit
......@@ -39,21 +39,9 @@ module MergeRequests
merge_request.diff_head_sha
end
def validate!
error_check!
end
override :error_check!
def error_check!
super
error =
if !hooks_validation_pass?(merge_request)
hooks_validation_error(merge_request)
elsif source.blank?
'No source for merge'
end
raise_error(error) if error
check_source
end
##
......
---
title: Bypass push rules for merge to ref service
merge_request:
author:
type: fixed
......@@ -18,15 +18,25 @@ describe MergeRequests::MergeToRefService do
allow(project).to receive(:above_size_limit?).and_return(true)
end
it 'returns the correct error message' do
it 'bypasses the repository limit check' do
result = service.execute(merge_request)
expected_error =
'This merge request cannot be merged, because this repository ' \
'has exceeded its size limit'
expect(result[:status]).to eq(:success)
end
end
context 'when no commit message is explicitly given and push rule is set' do
before do
create(:push_rule, :commit_message, project: project)
end
let(:service) { described_class.new(project, user) }
it 'uses the default commit message' do
result = service.execute(merge_request)
expect(result[:status]).to eq(:error)
expect(result[:message]).to start_with(expected_error)
expect(result[:status]).to eq(:success)
expect(project.commit(result[:commit_id]).message).to eq(merge_request.default_merge_commit_message)
end
end
end
......
......@@ -214,6 +214,19 @@ describe MergeRequests::MergeService do
allow(Rails.logger).to receive(:error)
end
context 'when source is missing' do
it 'logs and saves error' do
allow(merge_request).to receive(:diff_head_sha) { nil }
error_message = 'No source for merge'
service.execute(merge_request)
expect(merge_request.merge_error).to eq(error_message)
expect(Rails.logger).to have_received(:error).with(a_string_matching(error_message))
end
end
it 'logs and saves error if there is an exception' do
error_message = 'error message'
......
......@@ -191,6 +191,19 @@ describe MergeRequests::MergeToRefService do
it { expect(todo).not_to be_done }
end
context 'when source is missing' do
it 'returns error' do
allow(merge_request).to receive(:diff_head_sha) { nil }
error_message = 'No source for merge'
result = service.execute(merge_request)
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq(error_message)
end
end
context 'when target ref is passed as a parameter' do
let(:params) { { commit_message: 'merge train', target_ref: target_ref } }
......
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