Commit 37fbda6f authored by Illya Klymov's avatar Illya Klymov

Fix use of project_name instead of project_key for import

Correctly generate key and slug for bitbucket server importer
parent 9e404d35
# frozen_string_literal: true # frozen_string_literal: true
class Import::BitbucketServerProviderRepoEntity < Import::BitbucketProviderRepoEntity class Import::BitbucketServerProviderRepoEntity < Import::BitbucketProviderRepoEntity
expose :id, override: true do |repo|
"#{repo.project_key}/#{repo.slug}"
end
expose :provider_link, override: true do |repo, options| expose :provider_link, override: true do |repo, options|
repo.browse_url repo.browse_url
end end
......
---
title: Fix failing bitbucket server import when project slug differs from name
merge_request: 39433
author:
type: fixed
...@@ -33,15 +33,18 @@ RSpec.describe Import::BitbucketServerController do ...@@ -33,15 +33,18 @@ RSpec.describe Import::BitbucketServerController do
describe 'POST create' do describe 'POST create' do
let(:project_name) { "my-project_123" } let(:project_name) { "my-project_123" }
# rubocop: disable CodeReuse/ActiveRecord
before do before do
allow(controller).to receive(:client).and_return(client) allow(controller).to receive(:client).and_return(client)
repo = double(name: project_name) repo = double(name: project_name)
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(repo) allow(client).to receive(:repo).with(project_key, repo_slug).and_return(repo)
assign_session_tokens assign_session_tokens
end end
# rubocop: enable CodeReuse/ActiveRecord
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
# rubocop: disable CodeReuse/ActiveRecord
it 'returns the new project' do it 'returns the new project' do
allow(Gitlab::BitbucketServerImport::ProjectCreator) allow(Gitlab::BitbucketServerImport::ProjectCreator)
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything) .to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
...@@ -51,10 +54,12 @@ RSpec.describe Import::BitbucketServerController do ...@@ -51,10 +54,12 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
# rubocop: enable CodeReuse/ActiveRecord
context 'with project key with tildes' do context 'with project key with tildes' do
let(:project_key) { '~someuser_123' } let(:project_key) { '~someuser_123' }
# rubocop: disable CodeReuse/ActiveRecord
it 'successfully creates a project' do it 'successfully creates a project' do
allow(Gitlab::BitbucketServerImport::ProjectCreator) allow(Gitlab::BitbucketServerImport::ProjectCreator)
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything) .to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
...@@ -64,6 +69,7 @@ RSpec.describe Import::BitbucketServerController do ...@@ -64,6 +69,7 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
it 'returns an error when an invalid project key is used' do it 'returns an error when an invalid project key is used' do
...@@ -78,6 +84,7 @@ RSpec.describe Import::BitbucketServerController do ...@@ -78,6 +84,7 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:unprocessable_entity) expect(response).to have_gitlab_http_status(:unprocessable_entity)
end end
# rubocop: disable CodeReuse/ActiveRecord
it 'returns an error when the project cannot be found' do it 'returns an error when the project cannot be found' do
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(nil) allow(client).to receive(:repo).with(project_key, repo_slug).and_return(nil)
...@@ -85,7 +92,9 @@ RSpec.describe Import::BitbucketServerController do ...@@ -85,7 +92,9 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:unprocessable_entity) expect(response).to have_gitlab_http_status(:unprocessable_entity)
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
it 'returns an error when the project cannot be saved' do it 'returns an error when the project cannot be saved' do
allow(Gitlab::BitbucketServerImport::ProjectCreator) allow(Gitlab::BitbucketServerImport::ProjectCreator)
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything) .to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
...@@ -95,7 +104,9 @@ RSpec.describe Import::BitbucketServerController do ...@@ -95,7 +104,9 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:unprocessable_entity) expect(response).to have_gitlab_http_status(:unprocessable_entity)
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
it "returns an error when the server can't be contacted" do it "returns an error when the server can't be contacted" do
allow(client).to receive(:repo).with(project_key, repo_slug).and_raise(::BitbucketServer::Connection::ConnectionError) allow(client).to receive(:repo).with(project_key, repo_slug).and_raise(::BitbucketServer::Connection::ConnectionError)
...@@ -103,6 +114,7 @@ RSpec.describe Import::BitbucketServerController do ...@@ -103,6 +114,7 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:unprocessable_entity) expect(response).to have_gitlab_http_status(:unprocessable_entity)
end end
# rubocop: enable CodeReuse/ActiveRecord
it_behaves_like 'project import rate limiter' it_behaves_like 'project import rate limiter'
end end
...@@ -139,8 +151,6 @@ RSpec.describe Import::BitbucketServerController do ...@@ -139,8 +151,6 @@ RSpec.describe Import::BitbucketServerController do
describe 'GET status' do describe 'GET status' do
render_views render_views
let(:repos) { instance_double(BitbucketServer::Collection) }
before do before do
allow(controller).to receive(:client).and_return(client) allow(controller).to receive(:client).and_return(client)
...@@ -157,14 +167,14 @@ RSpec.describe Import::BitbucketServerController do ...@@ -157,14 +167,14 @@ RSpec.describe Import::BitbucketServerController do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['incompatible_repos'].length).to eq(1) expect(json_response['incompatible_repos'].length).to eq(1)
expect(json_response.dig("incompatible_repos", 0, "id")).to eq(@invalid_repo.full_name) expect(json_response.dig("incompatible_repos", 0, "id")).to eq("#{@invalid_repo.project_key}/#{@invalid_repo.slug}")
expect(json_response['provider_repos'].length).to eq(1) expect(json_response['provider_repos'].length).to eq(1)
expect(json_response.dig("provider_repos", 0, "id")).to eq(@repo.full_name) expect(json_response.dig("provider_repos", 0, "id")).to eq(@repo.full_name)
end end
it_behaves_like 'import controller status' do it_behaves_like 'import controller status' do
let(:repo) { @repo } let(:repo) { @repo }
let(:repo_id) { @repo.full_name } let(:repo_id) { "#{@repo.project_key}/#{@repo.slug}" }
let(:import_source) { @repo.browse_url } let(:import_source) { @repo.browse_url }
let(:provider_name) { 'bitbucket_server' } let(:provider_name) { 'bitbucket_server' }
let(:client_repos_field) { :repos } let(:client_repos_field) { :repos }
......
...@@ -6,8 +6,10 @@ RSpec.describe Import::BitbucketServerProviderRepoEntity do ...@@ -6,8 +6,10 @@ RSpec.describe Import::BitbucketServerProviderRepoEntity do
let(:repo_data) do let(:repo_data) do
{ {
'name' => 'test', 'name' => 'test',
'slug' => 'TEST',
'project' => { 'project' => {
'name' => 'demo' 'name' => 'demo',
'key' => 'DEM'
}, },
'links' => { 'links' => {
'self' => [ 'self' => [
...@@ -27,7 +29,7 @@ RSpec.describe Import::BitbucketServerProviderRepoEntity do ...@@ -27,7 +29,7 @@ RSpec.describe Import::BitbucketServerProviderRepoEntity do
it_behaves_like 'exposes required fields for import entity' do it_behaves_like 'exposes required fields for import entity' do
let(:expected_values) do let(:expected_values) do
{ {
id: 'demo/test', id: 'DEM/TEST',
full_name: 'demo/test', full_name: 'demo/test',
sanitized_name: 'test', sanitized_name: 'test',
provider_link: 'http://local.bitbucket.server/demo/test.git' provider_link: 'http://local.bitbucket.server/demo/test.git'
......
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