Commit 006f6c73 authored by Stan Hu's avatar Stan Hu

Fix wrong default branch imported with Bitbucket Server

When importing a repository from Bitbucket Server where HEAD of that
repo is something different than the default HEAD as created by Gitaly
(which currently is "main", so it would be triggered by importing any
repo whose HEAD points to "master"), then the end result is a corrupt
repository. This is because the import will create repos by first
calling CreateRepository, which initializes HEAD to "main", and then
calling FetchRemote, which only fetches refs but won't ever update HEAD.

We fix this by doing a standard import of the repository with a
follow-up fetch to map the pull requests to merge request references.

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/346008

Changelog: fixed
parent 132cc71d
......@@ -19,7 +19,8 @@ module Gitlab
end
def self.refmap
[:heads, :tags, '+refs/pull-requests/*/to:refs/merge-requests/*/head']
# We omit :heads and :tags since these are fetched in the import_repository
['+refs/pull-requests/*/to:refs/merge-requests/*/head']
end
# Unlike GitHub, you can't grab the commit SHAs for pull requests that
......@@ -140,7 +141,7 @@ module Gitlab
def import_repository
log_info(stage: 'import_repository', message: 'starting import')
project.ensure_repository
project.repository.import_repository(project.import_url)
project.repository.fetch_as_mirror(project.import_url, refmap: self.class.refmap)
log_info(stage: 'import_repository', message: 'finished import')
......
......@@ -27,12 +27,18 @@ RSpec.describe Gitlab::BitbucketServerImport::Importer do
end
describe '#import_repository' do
let(:repo_url) { 'http://bitbucket:test@my-bitbucket' }
before do
expect(project.repository).to receive(:import_repository).with(repo_url)
end
it 'adds a remote' do
expect(subject).to receive(:import_pull_requests)
expect(subject).to receive(:delete_temp_branches)
expect(project.repository).to receive(:fetch_as_mirror)
.with('http://bitbucket:test@my-bitbucket',
refmap: [:heads, :tags, '+refs/pull-requests/*/to:refs/merge-requests/*/head'])
.with(repo_url,
refmap: ['+refs/pull-requests/*/to:refs/merge-requests/*/head'])
subject.execute
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