Commit 6679251f authored by Tetiana Chupryna's avatar Tetiana Chupryna

Merge branch 'import_source_branch_from_open_fork_mrs' into 'master'

Fix branches being imported from closed and merged fork MRs

See merge request gitlab-org/gitlab!65554
parents edd5e0a5 1b843ee1
......@@ -39,7 +39,9 @@ module Gitlab
# created manually. Ignore failures so we get the merge request itself if
# the commits are missing.
def create_source_branch
@project.repository.create_branch(@merge_request.source_branch, @diff_head_sha)
if @merge_request.open?
@project.repository.create_branch(@merge_request.source_branch, @diff_head_sha)
end
rescue StandardError => err
Gitlab::Import::Logger.warn(
message: 'Import warning: Failed to create source branch',
......
......@@ -13,9 +13,11 @@ RSpec.describe Gitlab::ImportExport::MergeRequestParser do
create(:merge_request, source_project: forked_project, target_project: project)
end
let(:diff_head_sha) { SecureRandom.hex(20) }
let(:parsed_merge_request) do
described_class.new(project,
'abcd',
diff_head_sha,
merge_request,
merge_request.as_json).parse!
end
......@@ -34,14 +36,34 @@ RSpec.describe Gitlab::ImportExport::MergeRequestParser do
expect(project.repository.branch_exists?(parsed_merge_request.target_branch)).to be true
end
it 'parses a MR that has no source branch' do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:branch_exists?).and_call_original
allow(instance).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false)
allow(instance).to receive(:fork_merge_request?).and_return(true)
# Source and target branch are only created when: fork_merge_request
context 'fork merge request' do
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:fork_merge_request?).and_return(true)
end
end
it 'parses a MR that has no source branch' do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:branch_exists?).and_call_original
allow(instance).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false)
end
expect(parsed_merge_request).to eq(merge_request)
end
expect(parsed_merge_request).to eq(merge_request)
it 'parses a MR that is closed' do
merge_request.update!(state: :closed, source_branch: 'new_branch')
expect(project.repository.branch_exists?(parsed_merge_request.source_branch)).to be false
end
it 'parses a MR that is merged' do
merge_request.update!(state: :merged, source_branch: 'new_branch')
expect(project.repository.branch_exists?(parsed_merge_request.source_branch)).to be false
end
end
context 'when the merge request has diffs' do
......
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