2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe MergeRequests::FfMergeService do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
source_branch: 'flatten-dir',
|
|
|
|
target_branch: 'improve/awesome',
|
2019-07-31 22:56:46 +05:30
|
|
|
assignees: [user2],
|
2018-03-17 18:26:18 +05:30
|
|
|
author: create(:user))
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:project) { merge_request.project }
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:valid_merge_params) { { sha: merge_request.diff_head_sha } }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user2)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'valid params' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:service) { described_class.new(project: project, current_user: user, params: valid_merge_params) }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
def execute_ff_merge
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
service.execute(merge_request)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
before do
|
|
|
|
allow(service).to receive(:execute_hooks)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it "does not create merge commit" do
|
|
|
|
execute_ff_merge
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
source_branch_sha = merge_request.source_project.repository.commit(merge_request.source_branch).sha
|
|
|
|
target_branch_sha = merge_request.target_project.repository.commit(merge_request.target_branch).sha
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(source_branch_sha).to eq(target_branch_sha)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'keeps the merge request valid' do
|
|
|
|
expect { execute_ff_merge }
|
|
|
|
.not_to change { merge_request.valid? }
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'updates the merge request to merged' do
|
|
|
|
expect { execute_ff_merge }
|
|
|
|
.to change { merge_request.merged? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'sends email to user2 about merge of new merge_request' do
|
|
|
|
execute_ff_merge
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
email = ActionMailer::Base.deliveries.last
|
|
|
|
expect(email.to.first).to eq(user2.email)
|
|
|
|
expect(email.subject).to include(merge_request.title)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'creates resource event about merge_request merge' do
|
|
|
|
execute_ff_merge
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
event = merge_request.resource_state_events.last
|
|
|
|
expect(event.state).to eq('merged')
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'does not update squash_commit_sha if it is not a squash' do
|
|
|
|
expect(merge_request).to receive(:update_and_mark_in_progress_merge_commit_sha).twice.and_call_original
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect { execute_ff_merge }.not_to change { merge_request.squash_commit_sha }
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(merge_request.merge_commit_sha).to be_nil
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(merge_request.in_progress_merge_commit_sha).to be_nil
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'updates squash_commit_sha if it is a squash' do
|
|
|
|
expect(merge_request).to receive(:update_and_mark_in_progress_merge_commit_sha).twice.and_call_original
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
merge_request.update!(squash: true)
|
|
|
|
|
|
|
|
expect { execute_ff_merge }
|
|
|
|
.to change { merge_request.squash_commit_sha }
|
|
|
|
.from(nil)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(merge_request.merge_commit_sha).to be_nil
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(merge_request.in_progress_merge_commit_sha).to be_nil
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
context 'error handling' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:service) { described_class.new(project: project, current_user: user, params: valid_merge_params.merge(commit_message: 'Awesome message')) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(Gitlab::AppLogger).to receive(:error)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs and saves error if there is an exception' do
|
|
|
|
error_message = 'error message'
|
|
|
|
|
|
|
|
allow(service).to receive(:repository).and_raise("error message")
|
|
|
|
allow(service).to receive(:execute_hooks)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(Gitlab::AppLogger).to have_received(:error).with(a_string_matching(error_message))
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs and saves error if there is an PreReceiveError exception' do
|
|
|
|
error_message = 'error message'
|
2020-10-24 23:57:45 +05:30
|
|
|
raw_message = 'The truth is out there'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
pre_receive_error = Gitlab::Git::PreReceiveError.new(raw_message, fallback_message: error_message)
|
2020-10-24 23:57:45 +05:30
|
|
|
allow(service).to receive(:repository).and_raise(pre_receive_error)
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(service).to receive(:execute_hooks)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
|
|
|
|
expect(merge_request.merge_error).to include(error_message)
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(Gitlab::AppLogger).to have_received(:error).with(a_string_matching(error_message))
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
it 'does not update squash_commit_sha if squash merge is not successful' do
|
|
|
|
merge_request.update!(squash: true)
|
|
|
|
|
|
|
|
expect(project.repository.raw).to receive(:ff_merge) do
|
|
|
|
raise 'Merge error'
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { service.execute(merge_request) }.not_to change { merge_request.squash_commit_sha }
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|