summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/clients
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bulk_imports/clients')
-rw-r--r--lib/bulk_imports/clients/graphql.rb27
-rw-r--r--lib/bulk_imports/clients/http.rb35
2 files changed, 46 insertions, 16 deletions
diff --git a/lib/bulk_imports/clients/graphql.rb b/lib/bulk_imports/clients/graphql.rb
index ca549c4be14..0adc2b1c57f 100644
--- a/lib/bulk_imports/clients/graphql.rb
+++ b/lib/bulk_imports/clients/graphql.rb
@@ -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
diff --git a/lib/bulk_imports/clients/http.rb b/lib/bulk_imports/clients/http.rb
index c5f12d8c2ba..6c363a3552f 100644
--- a/lib/bulk_imports/clients/http.rb
+++ b/lib/bulk_imports/clients/http.rb
@@ -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)
+ def initialize(url:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION)
+ @url = url
@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,19 +113,15 @@ 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
- end
-
- def base_uri
- @base_uri ||= "#{@uri.scheme}://#{@uri.host}:#{@uri.port}"
+ raise(::BulkImports::Error, e)
end
def api_url
- Gitlab::Utils.append_path(base_uri, "/api/#{@api_version}")
+ Gitlab::Utils.append_path(@url, "/api/#{@api_version}")
end
end
end