Commit b9380cdf authored by Illya Klymov's avatar Illya Klymov

Address reviewer comments

- make tests more precise
- remove owner_name from serializing
parent ad362640
...@@ -24,9 +24,7 @@ class Import::BitbucketController < Import::BaseController ...@@ -24,9 +24,7 @@ class Import::BitbucketController < Import::BaseController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def status def status
if Feature.enabled?(:new_import_ui) return super if Feature.enabled?(:new_import_ui)
return super
end
bitbucket_client = Bitbucket::Client.new(credentials) bitbucket_client = Bitbucket::Client.new(credentials)
repos = bitbucket_client.repos(filter: sanitized_filter_param) repos = bitbucket_client.repos(filter: sanitized_filter_param)
......
...@@ -9,6 +9,8 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -9,6 +9,8 @@ class Import::BitbucketServerController < Import::BaseController
before_action :bitbucket_auth, except: [:new, :configure] before_action :bitbucket_auth, except: [:new, :configure]
before_action :validate_import_params, only: [:create] before_action :validate_import_params, only: [:create]
rescue_from BitbucketServer::Connection::ConnectionError, with: :bitbucket_connection_error
# As a basic sanity check to prevent URL injection, restrict project # As a basic sanity check to prevent URL injection, restrict project
# repository input and repository slugs to allowed characters. For Bitbucket: # repository input and repository slugs to allowed characters. For Bitbucket:
# #
...@@ -61,14 +63,7 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -61,14 +63,7 @@ class Import::BitbucketServerController < Import::BaseController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def status def status
if Feature.enabled?(:new_import_ui) return super if Feature.enabled?(:new_import_ui)
begin
return super
rescue BitbucketServer::Connection::ConnectionError => error
render json: { error: _("Unable to connect to server: %{error}") % { error: error } }, status: :unprocessable_entity
return
end
end
@collection = client.repos(page_offset: page_offset, limit: limit_per_page, filter: sanitized_filter_param) @collection = client.repos(page_offset: page_offset, limit: limit_per_page, filter: sanitized_filter_param)
@repos, @incompatible_repos = @collection.partition { |repo| repo.valid? } @repos, @incompatible_repos = @collection.partition { |repo| repo.valid? }
...@@ -78,10 +73,6 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -78,10 +73,6 @@ class Import::BitbucketServerController < Import::BaseController
already_added_projects_names = @already_added_projects.pluck(:import_source) already_added_projects_names = @already_added_projects.pluck(:import_source)
@repos.reject! { |repo| already_added_projects_names.include?(repo.browse_url) } @repos.reject! { |repo| already_added_projects_names.include?(repo.browse_url) }
rescue BitbucketServer::Connection::ConnectionError => error
flash[:alert] = _("Unable to connect to server: %{error}") % { error: error }
clear_session_data
redirect_to new_import_bitbucket_server_path
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -200,4 +191,17 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -200,4 +191,17 @@ class Import::BitbucketServerController < Import::BaseController
def sanitized_filter_param def sanitized_filter_param
sanitize(params[:filter]) sanitize(params[:filter])
end end
def bitbucket_connection_error(error)
respond_to do |format|
format.json do
render json: { error: _("Unable to connect to server: %{error}") % { error: error } }, status: :unprocessable_entity
end
format.html do
flash[:alert] = _("Unable to connect to server: %{error}") % { error: error }
clear_session_data
redirect_to new_import_bitbucket_server_path
end
end
end
end end
...@@ -50,9 +50,7 @@ class Import::FogbugzController < Import::BaseController ...@@ -50,9 +50,7 @@ class Import::FogbugzController < Import::BaseController
return redirect_to new_import_fogbugz_path return redirect_to new_import_fogbugz_path
end end
if Feature.enabled?(:new_import_ui) return super if Feature.enabled?(:new_import_ui)
return super
end
@repos = client.repos @repos = client.repos
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
class Import::BaseProviderRepoEntity < Grape::Entity class Import::BaseProviderRepoEntity < Grape::Entity
expose :id expose :id
expose :full_name expose :full_name
expose :owner_name
expose :sanitized_name expose :sanitized_name
expose :provider_link expose :provider_link
end end
...@@ -7,10 +7,6 @@ class Import::BitbucketProviderRepoEntity < Import::BaseProviderRepoEntity ...@@ -7,10 +7,6 @@ class Import::BitbucketProviderRepoEntity < Import::BaseProviderRepoEntity
repo.full_name repo.full_name
end end
expose :owner_name, override: true do |repo|
repo.owner
end
expose :sanitized_name, override: true do |repo| expose :sanitized_name, override: true do |repo|
repo.name.gsub(/[^\s\w.-]/, '') repo.name.gsub(/[^\s\w.-]/, '')
end end
......
# frozen_string_literal: true # frozen_string_literal: true
class Import::BitbucketServerProviderRepoEntity < Import::BitbucketProviderRepoEntity class Import::BitbucketServerProviderRepoEntity < Import::BitbucketProviderRepoEntity
expose :owner_name, override: true do
''
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
......
...@@ -7,10 +7,6 @@ class Import::FogbugzProviderRepoEntity < Import::BaseProviderRepoEntity ...@@ -7,10 +7,6 @@ class Import::FogbugzProviderRepoEntity < Import::BaseProviderRepoEntity
repo.name repo.name
end end
expose :owner_name, override: true do
''
end
expose :sanitized_name, override: true do |repo| expose :sanitized_name, override: true do |repo|
repo.safe_name repo.safe_name
end end
......
...@@ -3,10 +3,6 @@ ...@@ -3,10 +3,6 @@
class Import::GithubishProviderRepoEntity < Import::BaseProviderRepoEntity class Import::GithubishProviderRepoEntity < Import::BaseProviderRepoEntity
include ImportHelper include ImportHelper
expose :owner_name, override: true do |provider_repo, options|
owner_name(provider_repo, options[:provider])
end
expose :sanitized_name, override: true do |provider_repo| expose :sanitized_name, override: true do |provider_repo|
sanitize_project_name(provider_repo[:name]) sanitize_project_name(provider_repo[:name])
end end
......
...@@ -56,7 +56,7 @@ RSpec.describe Import::BitbucketController do ...@@ -56,7 +56,7 @@ RSpec.describe Import::BitbucketController do
describe "GET status" do describe "GET status" do
before do before do
@repo = double(name: 'vim', slug: 'vim', owner: 'asd', full_name: 'asd/vim', clone_url: "http://test.host/demo/url.git", "valid?" => true) @repo = double(name: 'vim', slug: 'vim', owner: 'asd', full_name: 'asd/vim', clone_url: 'http://test.host/demo/url.git', 'valid?' => true)
assign_session_tokens assign_session_tokens
stub_feature_flags(new_import_ui: false) stub_feature_flags(new_import_ui: false)
end end
......
...@@ -5,13 +5,13 @@ require 'spec_helper' ...@@ -5,13 +5,13 @@ require 'spec_helper'
describe Import::BitbucketProviderRepoEntity do describe Import::BitbucketProviderRepoEntity do
let(:repo_data) do let(:repo_data) do
{ {
"name" => "repo_name", 'name' => 'repo_name',
"full_name" => "owner/repo_name", 'full_name' => 'owner/repo_name',
"links" => { 'links' => {
"clone" => [ 'clone' => [
{ {
"href" => "https://bitbucket.org/owner/repo_name", 'href' => 'https://bitbucket.org/owner/repo_name',
"name" => "https" 'name' => 'https'
} }
] ]
} }
...@@ -21,5 +21,14 @@ describe Import::BitbucketProviderRepoEntity do ...@@ -21,5 +21,14 @@ describe Import::BitbucketProviderRepoEntity do
subject { described_class.new(repo).as_json } subject { described_class.new(repo).as_json }
it_behaves_like 'exposes required fields for import entity' it_behaves_like 'exposes required fields for import entity' do
let(:expected_values) do
{
id: 'owner/repo_name',
full_name: 'owner/repo_name',
sanitized_name: 'repo_name',
provider_link: 'https://bitbucket.org/owner/repo_name'
}
end
end
end end
...@@ -5,15 +5,15 @@ require 'spec_helper' ...@@ -5,15 +5,15 @@ require 'spec_helper'
describe Import::BitbucketServerProviderRepoEntity do describe Import::BitbucketServerProviderRepoEntity do
let(:repo_data) do let(:repo_data) do
{ {
"name" => "demo", 'name' => 'test',
"project" => { 'project' => {
"name" => "demo" 'name' => 'demo'
}, },
"links" => { 'links' => {
"self" => [ 'self' => [
{ {
"href" => "http://local.bitbucket.server/demo/demo.git", 'href' => 'http://local.bitbucket.server/demo/test.git',
"name" => "http" 'name' => 'http'
} }
] ]
} }
...@@ -23,5 +23,14 @@ describe Import::BitbucketServerProviderRepoEntity do ...@@ -23,5 +23,14 @@ describe Import::BitbucketServerProviderRepoEntity do
subject { described_class.new(repo).as_json } subject { described_class.new(repo).as_json }
it_behaves_like 'exposes required fields for import entity' it_behaves_like 'exposes required fields for import entity' do
let(:expected_values) do
{
id: 'demo/test',
full_name: 'demo/test',
sanitized_name: 'test',
provider_link: 'http://local.bitbucket.server/demo/test.git'
}
end
end
end end
...@@ -3,15 +3,25 @@ ...@@ -3,15 +3,25 @@
require 'spec_helper' require 'spec_helper'
describe Import::FogbugzProviderRepoEntity do describe Import::FogbugzProviderRepoEntity do
let(:provider_url) { 'https://demo.fogbugz.com/' }
let(:repo_data) do let(:repo_data) do
{ {
"ixProject" => "foo", 'ixProject' => 'foo',
"sProject" => "demo" 'sProject' => 'demo'
} }
end end
let(:repo) { Gitlab::FogbugzImport::Repository.new(repo_data) } let(:repo) { Gitlab::FogbugzImport::Repository.new(repo_data) }
subject { described_class.new(repo).as_json } subject { described_class.represent(repo, { provider_url: provider_url }).as_json }
it_behaves_like 'exposes required fields for import entity' it_behaves_like 'exposes required fields for import entity' do
let(:expected_values) do
{
id: 'foo',
full_name: 'demo',
sanitized_name: 'demo',
provider_link: 'https://demo.fogbugz.com/demo'
}
end
end
end end
...@@ -3,16 +3,25 @@ ...@@ -3,16 +3,25 @@
require 'spec_helper' require 'spec_helper'
describe Import::GithubishProviderRepoEntity do describe Import::GithubishProviderRepoEntity do
let(:provider_url) { 'https://github.com/' }
let(:repo) do let(:repo) do
{ {
id: 1, id: 1,
full_name: 'full/name', full_name: 'full/name',
name: 'name', name: 'name'
owner: { login: 'owner' }
} }
end end
subject { described_class.new(repo).as_json } subject { described_class.represent(repo, { provider_url: provider_url }).as_json }
it_behaves_like 'exposes required fields for import entity' it_behaves_like 'exposes required fields for import entity' do
let(:expected_values) do
{
id: 1,
full_name: 'full/name',
sanitized_name: 'name',
provider_link: 'https://github.com/full/name'
}
end
end
end end
# frozen_string_literal: true # frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
describe Import::ProviderRepoSerializer do describe Import::ProviderRepoSerializer do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
......
...@@ -2,24 +2,20 @@ ...@@ -2,24 +2,20 @@
RSpec.shared_examples 'exposes required fields for import entity' do RSpec.shared_examples 'exposes required fields for import entity' do
describe 'exposes required fields' do describe 'exposes required fields' do
it 'exposes id' do it 'correctly exposes id' do
expect(subject).to include(:id) expect(subject[:id]).to eql(expected_values[:id])
end end
it 'exposes full name' do it 'correctly exposes full name' do
expect(subject).to include(:full_name) expect(subject[:full_name]).to eql(expected_values[:full_name])
end end
it 'exposes owner name' do it 'correctly exposes sanitized name' do
expect(subject).to include(:owner_name) expect(subject[:sanitized_name]).to eql(expected_values[:sanitized_name])
end end
it 'exposes sanitized name' do it 'correctly exposes provider link' do
expect(subject).to include(:sanitized_name) expect(subject[:provider_link]).to eql(expected_values[:provider_link])
end
it 'exposes provider link' do
expect(subject).to include(:provider_link)
end end
end end
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