2019-03-29 01:07:03 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe MergeRequests::PushOptionsHandlerService do
|
2019-04-08 05:07:53 +00:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2019-03-29 01:07:03 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2019-04-08 05:07:53 +00:00
|
|
|
let(:forked_project) { fork_project(project, user, repository: true) }
|
2019-03-29 01:07:03 +00:00
|
|
|
let(:service) { described_class.new(project, user, changes, push_options) }
|
2019-04-08 05:07:53 +00:00
|
|
|
let(:source_branch) { 'fix' }
|
2019-03-29 01:07:03 +00:00
|
|
|
let(:target_branch) { 'feature' }
|
|
|
|
let(:new_branch_changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
|
|
|
|
let(:existing_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
|
|
|
|
let(:deleted_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 #{Gitlab::Git::BLANK_SHA} refs/heads/#{source_branch}" }
|
|
|
|
let(:default_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{project.default_branch}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a service that can create a merge request' do
|
|
|
|
subject(:last_mr) { MergeRequest.last }
|
|
|
|
|
|
|
|
it 'creates a merge request' do
|
|
|
|
expect { service.execute }.to change { MergeRequest.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the correct target branch' do
|
|
|
|
branch = push_options[:target] || project.default_branch
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(last_mr.target_branch).to eq(branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the MR to the user' do
|
|
|
|
service.execute
|
|
|
|
|
2019-04-09 19:30:52 +00:00
|
|
|
expect(last_mr.assignees).to contain_exactly(user)
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
context 'when project has been forked' do
|
|
|
|
let(:forked_project) { fork_project(project, user, repository: true) }
|
|
|
|
let(:service) { described_class.new(forked_project, user, changes, push_options) }
|
2019-03-29 01:07:03 +00:00
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
before do
|
|
|
|
allow(forked_project).to receive(:empty_repo?).and_return(false)
|
|
|
|
end
|
2019-03-29 01:07:03 +00:00
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'sets the correct source project' do
|
|
|
|
service.execute
|
2019-03-29 01:07:03 +00:00
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
expect(last_mr.source_project).to eq(forked_project)
|
|
|
|
end
|
2019-03-29 01:07:03 +00:00
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'sets the correct target project' do
|
|
|
|
service.execute
|
2019-03-29 01:07:03 +00:00
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
expect(last_mr.target_project).to eq(project)
|
|
|
|
end
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a service that can set the target of a merge request' do
|
|
|
|
subject(:last_mr) { MergeRequest.last }
|
|
|
|
|
|
|
|
it 'sets the target_branch' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(last_mr.target_branch).to eq(target_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-05 13:22:58 +00:00
|
|
|
shared_examples_for 'a service that can set the merge request to merge when pipeline succeeds' do
|
|
|
|
subject(:last_mr) { MergeRequest.last }
|
|
|
|
|
|
|
|
it 'sets merge_when_pipeline_succeeds' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(last_mr.merge_when_pipeline_succeeds).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets merge_user to the user' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(last_mr.merge_user).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-29 01:07:03 +00:00
|
|
|
shared_examples_for 'a service that does not create a merge request' do
|
|
|
|
it do
|
|
|
|
expect { service.execute }.not_to change { MergeRequest.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a service that does not update a merge request' do
|
|
|
|
it do
|
|
|
|
expect { service.execute }.not_to change { MergeRequest.maximum(:updated_at) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a service that does nothing' do
|
|
|
|
include_examples 'a service that does not create a merge request'
|
|
|
|
include_examples 'a service that does not update a merge request'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '`create` push option' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
|
|
|
|
context 'with a new branch' do
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch but no open MR' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch that has a merge request open' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a deleted branch' do
|
|
|
|
let(:changes) { deleted_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the project default branch' do
|
|
|
|
let(:changes) { default_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-05 13:22:58 +00:00
|
|
|
describe '`merge_when_pipeline_succeeds` push option' do
|
|
|
|
let(:push_options) { { merge_when_pipeline_succeeds: true } }
|
|
|
|
|
|
|
|
context 'with a new branch' do
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
|
|
|
|
it 'adds an error to the service' do
|
|
|
|
error = "A merge_request.create push option is required to create a merge request for branch #{source_branch}"
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to include(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when coupled with the `create` push option' do
|
|
|
|
let(:push_options) { { create: true, merge_when_pipeline_succeeds: true } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the merge request to merge when pipeline succeeds'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch but no open MR' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
|
|
|
|
it 'adds an error to the service' do
|
|
|
|
error = "A merge_request.create push option is required to create a merge request for branch #{source_branch}"
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to include(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when coupled with the `create` push option' do
|
|
|
|
let(:push_options) { { create: true, merge_when_pipeline_succeeds: true } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the merge request to merge when pipeline succeeds'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch that has a merge request open' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the merge request to merge when pipeline succeeds'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a deleted branch' do
|
|
|
|
let(:changes) { deleted_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the project default branch' do
|
|
|
|
let(:changes) { default_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-29 01:07:03 +00:00
|
|
|
describe '`target` push option' do
|
|
|
|
let(:push_options) { { target: target_branch } }
|
|
|
|
|
|
|
|
context 'with a new branch' do
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
|
|
|
|
it 'adds an error to the service' do
|
|
|
|
error = "A merge_request.create push option is required to create a merge request for branch #{source_branch}"
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to include(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when coupled with the `create` push option' do
|
|
|
|
let(:push_options) { { create: true, target: target_branch } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the target of a merge request'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch but no open MR' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
|
|
|
|
it 'adds an error to the service' do
|
|
|
|
error = "A merge_request.create push option is required to create a merge request for branch #{source_branch}"
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to include(error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when coupled with the `create` push option' do
|
|
|
|
let(:push_options) { { create: true, target: target_branch } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the target of a merge request'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing branch that has a merge request open' do
|
|
|
|
let(:changes) { existing_branch_changes }
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does not create a merge request'
|
|
|
|
it_behaves_like 'a service that can set the target of a merge request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a deleted branch' do
|
|
|
|
let(:changes) { deleted_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the project default branch' do
|
|
|
|
let(:changes) { default_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'multiple pushed branches' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) do
|
|
|
|
[
|
|
|
|
new_branch_changes,
|
2019-04-08 05:07:53 +00:00
|
|
|
"#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/feature_conflict"
|
2019-03-29 01:07:03 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a merge request per branch' do
|
|
|
|
expect { service.execute }.to change { MergeRequest.count }.by(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are too many pushed branches' do
|
|
|
|
let(:limit) { MergeRequests::PushOptionsHandlerService::LIMIT }
|
|
|
|
let(:changes) do
|
|
|
|
TestEnv::BRANCH_SHA.to_a[0..limit].map do |x|
|
|
|
|
"#{Gitlab::Git::BLANK_SHA} #{x.first} refs/heads/#{x.last}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'records an error' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(["Too many branches pushed (#{limit + 1} were pushed, limit is #{limit})"])
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'no push options' do
|
|
|
|
let(:push_options) { {} }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that does nothing'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'no user' do
|
|
|
|
let(:user) { nil }
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'records an error' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(['User is required'])
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unauthorized user' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'records an error' do
|
2019-03-29 01:07:03 +00:00
|
|
|
Members::DestroyService.new(user).execute(ProjectMember.find_by!(user_id: user.id))
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(['User access was denied'])
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 22:55:07 +00:00
|
|
|
describe 'handling unexpected exceptions' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
let(:exception) { StandardError.new('My standard error') }
|
|
|
|
|
|
|
|
def run_service_with_exception
|
|
|
|
allow_any_instance_of(
|
|
|
|
MergeRequests::BuildService
|
|
|
|
).to receive(:execute).and_raise(exception)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records an error' do
|
|
|
|
run_service_with_exception
|
|
|
|
|
|
|
|
expect(service.errors).to eq(['An unknown error occurred'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes to Gitlab::AppLogger' do
|
|
|
|
expect(Gitlab::AppLogger).to receive(:error).with(exception)
|
|
|
|
|
|
|
|
run_service_with_exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-29 01:07:03 +00:00
|
|
|
describe 'when target is not a valid branch name' do
|
|
|
|
let(:push_options) { { create: true, target: 'my-branch' } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'records an error' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(['Branch my-branch does not exist'])
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when MRs are not enabled' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
it 'records an error' do
|
2019-03-29 01:07:03 +00:00
|
|
|
expect(project).to receive(:merge_requests_enabled?).and_return(false)
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(["Merge requests are not enabled for project #{project.full_path}"])
|
2019-03-29 01:07:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when MR has ActiveRecord errors' do
|
|
|
|
let(:push_options) { { create: true } }
|
|
|
|
let(:changes) { new_branch_changes }
|
|
|
|
|
|
|
|
it 'adds the error to its errors property' do
|
|
|
|
invalid_merge_request = MergeRequest.new
|
|
|
|
invalid_merge_request.errors.add(:base, 'my error')
|
|
|
|
|
|
|
|
expect_any_instance_of(
|
|
|
|
MergeRequests::CreateService
|
|
|
|
).to receive(:execute).and_return(invalid_merge_request)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(service.errors).to eq(['my error'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|