Commit 5bf417a4 authored by James Lopez's avatar James Lopez

Merge branch '12643-use-create-ref-when-importing-mrs' into 'master'

Use create_branch instead of fetch_source_branch when importing MRs

See merge request gitlab-org/gitlab!22693
parents 957f8763 0b0db84b
...@@ -16,7 +16,7 @@ module Gitlab ...@@ -16,7 +16,7 @@ module Gitlab
if fork_merge_request? && @diff_head_sha if fork_merge_request? && @diff_head_sha
@merge_request.source_project_id = @relation_hash['project_id'] @merge_request.source_project_id = @relation_hash['project_id']
fetch_ref unless branch_exists?(@merge_request.source_branch) create_source_branch unless branch_exists?(@merge_request.source_branch)
create_target_branch unless branch_exists?(@merge_request.target_branch) create_target_branch unless branch_exists?(@merge_request.target_branch)
end end
...@@ -34,17 +34,18 @@ module Gitlab ...@@ -34,17 +34,18 @@ module Gitlab
@merge_request @merge_request
end end
def create_target_branch # When the exported MR was in a fork, the source branch does not exist in
@project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha) # the imported bundle - although the commits usually do - so it must be
# 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)
rescue => err
Rails.logger.warn("Import/Export warning: Failed to create source branch #{@merge_request.source_branch} => #{@diff_head_sha} for MR #{@merge_request.iid}: #{err}") # rubocop:disable Gitlab/RailsLogger
end end
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/1295 def create_target_branch
def fetch_ref @project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha)
target_ref = Gitlab::Git::BRANCH_REF_PREFIX + @merge_request.source_branch
unless @project.repository.fetch_source_branch!(@project.repository, @diff_head_sha, target_ref)
Rails.logger.warn("Import/Export warning: Failed to create #{target_ref} for MR: #{@merge_request.iid}") # rubocop:disable Gitlab/RailsLogger
end
end end
def branch_exists?(branch_name) def branch_exists?(branch_name)
......
...@@ -40,8 +40,6 @@ describe Gitlab::ImportExport::MergeRequestParser do ...@@ -40,8 +40,6 @@ describe Gitlab::ImportExport::MergeRequestParser do
allow(instance).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false) allow(instance).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false)
allow(instance).to receive(:fork_merge_request?).and_return(true) allow(instance).to receive(:fork_merge_request?).and_return(true)
end end
allow(Gitlab::GitalyClient).to receive(:migrate).and_call_original
allow(Gitlab::GitalyClient).to receive(:migrate).with(:fetch_ref).and_return([nil, 0])
expect(parsed_merge_request).to eq(merge_request) expect(parsed_merge_request).to eq(merge_request)
end end
......
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