Commit b5cbdf2d authored by Nikola Milojevic's avatar Nikola Milojevic

Merge branch 'georgekoltsov/upp-bulk-import-min-required-version' into 'master'

Add source version check to Bulk Import

See merge request gitlab-org/gitlab!63886
parents 36110f35 80c22fd6
......@@ -10,7 +10,7 @@ class Import::BulkImportsController < ApplicationController
POLLING_INTERVAL = 3_000
rescue_from BulkImports::Clients::HTTP::ConnectionError, with: :bulk_import_connection_error
rescue_from BulkImports::Error, with: :bulk_import_connection_error
def configure
session[access_token_key] = configure_params[access_token_key]&.strip
......
......@@ -4,6 +4,8 @@
# projects to a GitLab instance. It associates the import with the responsible
# user.
class BulkImport < ApplicationRecord
MINIMUM_GITLAB_MAJOR_VERSION = 14
belongs_to :user, optional: false
has_one :configuration, class_name: 'BulkImports::Configuration'
......
......@@ -23,15 +23,19 @@ module BulkImports
attr_reader :client
delegate :query, :parse, :execute, to: :client
delegate :query, :parse, to: :client
def initialize(url: Gitlab::Saas.com_url, token: nil)
@url = Gitlab::Utils.append_path(url, '/api/graphql')
@token = token
@client = Graphlient::Client.new(
@url,
options(http: HTTP)
)
@client = Graphlient::Client.new(@url, options(http: HTTP))
@compatible_instance_version = false
end
def execute(*args)
validate_instance_version!
client.execute(*args)
end
def options(extra = {})
......@@ -44,6 +48,19 @@ module BulkImports
}
}.merge(extra)
end
def validate_instance_version!
return if @compatible_instance_version
response = client.execute('{ metadata { version } }')
version = Gitlab::VersionInfo.parse(response.data.metadata.version)
if version.major < BulkImport::MINIMUM_GITLAB_MAJOR_VERSION
raise ::BulkImports::Error.unsupported_gitlab_version
else
@compatible_instance_version = true
end
end
end
end
end
......@@ -7,14 +7,13 @@ module BulkImports
DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 30
ConnectionError = Class.new(StandardError)
def initialize(uri:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION)
@uri = URI.parse(uri)
@token = token&.strip
@page = page
@per_page = per_page
@api_version = api_version
@compatible_instance_version = false
end
def get(resource, query = {})
......@@ -53,10 +52,28 @@ module BulkImports
Gitlab::Utils.append_path(api_url, resource)
end
def validate_instance_version!
return if @compatible_instance_version
response = with_error_handling do
Gitlab::HTTP.get(resource_url(:version), default_options)
end
version = Gitlab::VersionInfo.parse(response.parsed_response['version'])
if version.major < BulkImport::MINIMUM_GITLAB_MAJOR_VERSION
raise ::BulkImports::Error.unsupported_gitlab_version
else
@compatible_instance_version = true
end
end
private
# rubocop:disable GitlabSecurity/PublicSend
def request(method, resource, options = {}, &block)
validate_instance_version!
with_error_handling do
Gitlab::HTTP.public_send(
method,
......@@ -96,11 +113,11 @@ module BulkImports
def with_error_handling
response = yield
raise ConnectionError, "Error #{response.code}" unless response.success?
raise(::BulkImports::Error, "Error #{response.code}") unless response.success?
response
rescue *Gitlab::HTTP::HTTP_ERRORS => e
raise ConnectionError, e
raise(::BulkImports::Error, e)
end
def base_uri
......
# frozen_string_literal: true
module BulkImports
class Error < StandardError
def self.unsupported_gitlab_version
self.new("Unsupported GitLab Version. Minimum Supported Gitlab Version #{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.")
end
end
end
......@@ -149,7 +149,7 @@ RSpec.describe Import::BulkImportsController do
context 'when connection error occurs' do
before do
allow(controller).to receive(:client).and_return(client)
allow(client).to receive(:get).and_raise(BulkImports::Clients::HTTP::ConnectionError)
allow(client).to receive(:get).and_raise(BulkImports::Error)
end
it 'returns 422' do
......
......@@ -24,6 +24,7 @@ RSpec.describe 'Import/Export - Connect to another instance', :js do
pat = 'demo-pat'
stub_path = 'stub-group'
total = 37
stub_request(:get, "%{url}/api/v4/groups?page=1&per_page=20&top_level_only=true&min_access_level=50&search=" % { url: source_url }).to_return(
body: [{
id: 2595438,
......@@ -32,7 +33,7 @@ RSpec.describe 'Import/Export - Connect to another instance', :js do
path: stub_path,
full_name: 'Stub',
full_path: stub_path
}].to_json,
}].to_json,
headers: {
'Content-Type' => 'application/json',
'X-Next-Page' => 2,
......@@ -43,6 +44,10 @@ RSpec.describe 'Import/Export - Connect to another instance', :js do
}
)
allow_next_instance_of(BulkImports::Clients::HTTP) do |client|
allow(client).to receive(:validate_instance_version!).and_return(true)
end
expect(page).to have_content 'Import groups from another instance of GitLab'
expect(page).to have_content 'Not all related objects are migrated'
......@@ -53,6 +58,8 @@ RSpec.describe 'Import/Export - Connect to another instance', :js do
expect(page).to have_content 'Showing 1-1 of %{total} groups from %{url}' % { url: source_url, total: total }
expect(page).to have_content stub_path
wait_for_all_requests
end
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::Clients::Graphql do
let_it_be(:config) { create(:bulk_import_configuration) }
subject { described_class.new(url: config.url, token: config.access_token) }
describe '#execute' do
let(:query) { '{ metadata { version } }' }
let(:graphql_client_double) { double }
let(:response_double) { double }
before do
stub_const('BulkImports::MINIMUM_COMPATIBLE_MAJOR_VERSION', version)
allow(graphql_client_double).to receive(:execute)
allow(subject).to receive(:client).and_return(graphql_client_double)
allow(graphql_client_double).to receive(:execute).with(query).and_return(response_double)
allow(response_double).to receive_message_chain(:data, :metadata, :version).and_return(version)
end
context 'when source instance is compatible' do
let(:version) { '14.0.0' }
it 'marks source instance as compatible' do
subject.execute('test')
expect(subject.instance_variable_get(:@compatible_instance_version)).to eq(true)
end
end
context 'when source instance is incompatible' do
let(:version) { '13.0.0' }
it 'raises an error' do
expect { subject.execute('test') }.to raise_error(::BulkImports::Error, "Unsupported GitLab Version. Minimum Supported Gitlab Version #{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.")
end
end
end
end
......@@ -8,7 +8,15 @@ RSpec.describe BulkImports::Clients::HTTP do
let(:uri) { 'http://gitlab.example' }
let(:token) { 'token' }
let(:resource) { 'resource' }
let(:version) { "#{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.0.0" }
let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }
let(:version_response) { double(code: 200, success?: true, parsed_response: { 'version' => version }) }
before do
allow(Gitlab::HTTP).to receive(:get)
.with('http://gitlab.example:80/api/v4/version', anything)
.and_return(version_response)
end
subject { described_class.new(uri: uri, token: token) }
......@@ -21,20 +29,20 @@ RSpec.describe BulkImports::Clients::HTTP do
context 'error handling' do
context 'when error occurred' do
it 'raises ConnectionError' do
it 'raises BulkImports::Error' do
allow(Gitlab::HTTP).to receive(method).and_raise(Errno::ECONNREFUSED)
expect { subject.public_send(method, resource) }.to raise_exception(described_class::ConnectionError)
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::Error)
end
end
context 'when response is not success' do
it 'raises ConnectionError' do
it 'raises BulkImports::Error' do
response_double = double(code: 503, success?: false)
allow(Gitlab::HTTP).to receive(method).and_return(response_double)
expect { subject.public_send(method, resource) }.to raise_exception(described_class::ConnectionError)
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::Error)
end
end
end
......@@ -167,4 +175,12 @@ RSpec.describe BulkImports::Clients::HTTP do
subject.stream(resource)
end
end
context 'when source instance is incompatible' do
let(:version) { '13.0.0' }
it 'raises an error' do
expect { subject.get(resource) }.to raise_error(::BulkImports::Error, "Unsupported GitLab Version. Minimum Supported Gitlab Version #{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.")
end
end
end
......@@ -6,12 +6,17 @@ RSpec.describe BulkImports::ExportRequestWorker do
let_it_be(:bulk_import) { create(:bulk_import) }
let_it_be(:config) { create(:bulk_import_configuration, bulk_import: bulk_import) }
let_it_be(:entity) { create(:bulk_import_entity, source_full_path: 'foo/bar', bulk_import: bulk_import) }
let_it_be(:version_url) { 'https://gitlab.example:443/api/v4/version' }
let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }
let(:job_args) { [entity.id] }
describe '#perform' do
before do
allow(Gitlab::HTTP)
.to receive(:get)
.with(version_url, anything)
.and_return(double(code: 200, success?: true, parsed_response: { 'version' => Gitlab::VERSION }))
allow(Gitlab::HTTP).to receive(:post).and_return(response_double)
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