2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe MergeRequests::RebaseService do
|
2017-12-20 04:01:21 -05:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2019-06-21 12:56:47 -04:00
|
|
|
let(:rebase_jid) { 'fake-rebase-jid' }
|
2017-12-20 04:01:21 -05:00
|
|
|
let(:merge_request) do
|
2019-06-21 12:56:47 -04:00
|
|
|
create :merge_request,
|
2017-12-20 04:01:21 -05:00
|
|
|
source_branch: 'feature_conflict',
|
2019-06-21 12:56:47 -04:00
|
|
|
target_branch: 'master',
|
|
|
|
rebase_jid: rebase_jid
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
let(:project) { merge_request.project }
|
|
|
|
let(:repository) { project.repository.raw }
|
2020-01-16 16:08:24 -05:00
|
|
|
let(:skip_ci) { false }
|
2017-12-20 04:01:21 -05:00
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
subject(:service) { described_class.new(project: project, current_user: user) }
|
2017-12-20 04:01:21 -05:00
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2019-05-14 15:37:15 -04:00
|
|
|
shared_examples 'sequence of failure and success' do
|
|
|
|
it 'properly clears the error message' do
|
|
|
|
allow(repository).to receive(:gitaly_operation_client).and_raise('Something went wrong')
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
2019-06-21 12:56:47 -04:00
|
|
|
merge_request.reload
|
2019-05-14 15:37:15 -04:00
|
|
|
|
2019-06-21 12:56:47 -04:00
|
|
|
expect(merge_request.reload.merge_error).to eq(described_class::REBASE_ERROR)
|
|
|
|
expect(merge_request.rebase_jid).to eq(nil)
|
2019-05-14 15:37:15 -04:00
|
|
|
|
|
|
|
allow(repository).to receive(:gitaly_operation_client).and_call_original
|
2019-06-21 12:56:47 -04:00
|
|
|
merge_request.update!(rebase_jid: rebase_jid)
|
2019-05-14 15:37:15 -04:00
|
|
|
|
|
|
|
service.execute(merge_request)
|
2019-06-21 12:56:47 -04:00
|
|
|
merge_request.reload
|
2019-05-14 15:37:15 -04:00
|
|
|
|
2019-06-21 12:56:47 -04:00
|
|
|
expect(merge_request.merge_error).to eq(nil)
|
|
|
|
expect(merge_request.rebase_jid).to eq(nil)
|
2019-05-14 15:37:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sequence of failure and success'
|
|
|
|
|
2018-07-03 05:12:03 -04:00
|
|
|
context 'when unexpected error occurs' do
|
2020-05-07 11:09:29 -04:00
|
|
|
let(:exception) { RuntimeError.new('Something went wrong') }
|
|
|
|
let(:merge_request_ref) { merge_request.to_reference(full: true) }
|
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
before do
|
2020-05-07 11:09:29 -04:00
|
|
|
allow(repository).to receive(:gitaly_operation_client).and_raise(exception)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 10:49:39 -05:00
|
|
|
it 'saves a generic error message' do
|
2020-05-07 11:09:29 -04:00
|
|
|
service.execute(merge_request)
|
2017-12-20 04:01:21 -05:00
|
|
|
|
2019-06-21 12:56:47 -04:00
|
|
|
expect(merge_request.reload.merge_error).to eq(described_class::REBASE_ERROR)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
expect(service.execute(merge_request)).to match(status: :error,
|
2018-01-09 10:49:39 -05:00
|
|
|
message: described_class::REBASE_ERROR)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
it 'logs the error' do
|
|
|
|
expect(service).to receive(:log_error).with(exception: exception, message: described_class::REBASE_ERROR, save_message_on_model: true).and_call_original
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(exception,
|
|
|
|
class: described_class.to_s,
|
|
|
|
merge_request: merge_request_ref,
|
|
|
|
merge_request_id: merge_request.id,
|
|
|
|
message: described_class::REBASE_ERROR,
|
|
|
|
save_message_on_model: true).and_call_original
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
2018-07-03 05:12:03 -04:00
|
|
|
context 'with git command failure' do
|
2017-12-20 04:01:21 -05:00
|
|
|
before do
|
2018-07-03 05:12:03 -04:00
|
|
|
allow(repository).to receive(:gitaly_operation_client).and_raise(Gitlab::Git::Repository::GitError, 'Something went wrong')
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 10:49:39 -05:00
|
|
|
it 'saves a generic error message' do
|
2017-12-20 04:01:21 -05:00
|
|
|
subject.execute(merge_request)
|
|
|
|
|
2018-01-09 10:49:39 -05:00
|
|
|
expect(merge_request.reload.merge_error).to eq described_class::REBASE_ERROR
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
expect(service.execute(merge_request)).to match(status: :error,
|
2018-01-09 10:49:39 -05:00
|
|
|
message: described_class::REBASE_ERROR)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid params' do
|
2019-05-02 13:30:07 -04:00
|
|
|
shared_examples_for 'a service that can execute a successful rebase' do
|
2018-01-03 08:04:58 -05:00
|
|
|
before do
|
2020-01-16 16:08:24 -05:00
|
|
|
service.execute(merge_request, skip_ci: skip_ci)
|
2018-01-03 08:04:58 -05:00
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
|
2018-01-03 08:04:58 -05:00
|
|
|
it 'rebases source branch' do
|
|
|
|
parent_sha = merge_request.source_project.repository.commit(merge_request.source_branch).parents.first.sha
|
|
|
|
target_branch_sha = merge_request.target_project.repository.commit(merge_request.target_branch).sha
|
|
|
|
expect(parent_sha).to eq(target_branch_sha)
|
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
|
2018-01-03 08:04:58 -05:00
|
|
|
it 'records the new SHA on the merge request' do
|
|
|
|
head_sha = merge_request.source_project.repository.commit(merge_request.source_branch).sha
|
|
|
|
expect(merge_request.reload.rebase_commit_sha).to eq(head_sha)
|
|
|
|
end
|
|
|
|
|
2018-09-25 04:37:32 -04:00
|
|
|
it 'logs correct author and committer' do
|
2018-01-03 08:04:58 -05:00
|
|
|
head_commit = merge_request.source_project.repository.commit(merge_request.source_branch)
|
|
|
|
|
|
|
|
expect(head_commit.author_email).to eq('dmitriy.zaporozhets@gmail.com')
|
|
|
|
expect(head_commit.author_name).to eq('Dmitriy Zaporozhets')
|
|
|
|
expect(head_commit.committer_email).to eq(user.email)
|
|
|
|
expect(head_commit.committer_name).to eq(user.name)
|
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
2020-02-04 01:09:13 -05:00
|
|
|
it_behaves_like 'a service that can execute a successful rebase'
|
2020-01-16 16:08:24 -05:00
|
|
|
|
2021-07-15 02:09:39 -04:00
|
|
|
it 'clears rebase_jid' do
|
|
|
|
expect { service.execute(merge_request) }
|
|
|
|
.to change(merge_request, :rebase_jid)
|
|
|
|
.from(rebase_jid)
|
|
|
|
.to(nil)
|
|
|
|
end
|
|
|
|
|
2020-01-16 16:08:24 -05:00
|
|
|
context 'when skip_ci flag is set' do
|
|
|
|
let(:skip_ci) { true }
|
|
|
|
|
|
|
|
it_behaves_like 'a service that can execute a successful rebase'
|
|
|
|
end
|
2019-05-02 13:30:07 -04:00
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
context 'fork' do
|
2018-07-03 05:12:03 -04:00
|
|
|
describe 'successful fork rebase' do
|
2018-01-03 08:04:58 -05:00
|
|
|
let(:forked_project) do
|
|
|
|
fork_project(project, user, repository: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request_from_fork) do
|
|
|
|
forked_project.repository.create_file(
|
|
|
|
user,
|
|
|
|
'new-file-to-target',
|
|
|
|
'',
|
|
|
|
message: 'Add new file to target',
|
|
|
|
branch_name: 'master')
|
|
|
|
|
|
|
|
create(:merge_request,
|
|
|
|
source_branch: 'master', source_project: forked_project,
|
|
|
|
target_branch: 'master', target_project: project)
|
|
|
|
end
|
|
|
|
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'rebases source branch', :sidekiq_might_not_need_inline do
|
2018-01-03 08:04:58 -05:00
|
|
|
parent_sha = forked_project.repository.commit(merge_request_from_fork.source_branch).parents.first.sha
|
|
|
|
target_branch_sha = project.repository.commit(merge_request_from_fork.target_branch).sha
|
|
|
|
expect(parent_sha).to eq(target_branch_sha)
|
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|