Commit 0b0db84b authored by Nick Thomas's avatar Nick Thomas

Use create_branch instead of fetch_source_branch when importing MRs

When importing, we want to create `refs/heads/:source_branch` in the
repository for each MR. Since the imported project already contains
the SHA, there's no need to use `fetch_source_branch` - we can just
create the ref directly.
parent 30ecbead
......@@ -16,7 +16,7 @@ module Gitlab
if fork_merge_request? && @diff_head_sha
@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)
end
......@@ -34,17 +34,18 @@ module Gitlab
@merge_request
end
def create_target_branch
@project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha)
# When the exported MR was in a fork, the source branch does not exist in
# 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
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/1295
def fetch_ref
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
def create_target_branch
@project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha)
end
def branch_exists?(branch_name)
......
......@@ -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(:fork_merge_request?).and_return(true)
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)
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