Commit 1b843ee1 authored by Daniel Diniz's avatar Daniel Diniz Committed by Tetiana Chupryna

Fix branches being imported from closed and merged fork MRs

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