2020-10-23 15:08:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Clients
|
|
|
|
class Graphql
|
2020-11-13 09:09:30 +00:00
|
|
|
class HTTP < Graphlient::Adapters::HTTP::Adapter
|
|
|
|
def execute(document:, operation_name: nil, variables: {}, context: {})
|
|
|
|
response = ::Gitlab::HTTP.post(
|
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
follow_redirects: false,
|
|
|
|
body: {
|
|
|
|
query: document.to_query_string,
|
|
|
|
operationName: operation_name,
|
|
|
|
variables: variables
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
|
|
|
|
::Gitlab::Json.parse(response.body)
|
2021-09-30 06:09:27 +00:00
|
|
|
rescue *Gitlab::HTTP::HTTP_ERRORS => e
|
|
|
|
raise ::BulkImports::NetworkError, e
|
2020-11-13 09:09:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private_constant :HTTP
|
|
|
|
|
2020-10-23 15:08:42 +00:00
|
|
|
attr_reader :client
|
|
|
|
|
2021-06-25 12:07:44 +00:00
|
|
|
delegate :query, :parse, to: :client
|
2020-10-23 15:08:42 +00:00
|
|
|
|
2021-06-14 03:09:53 +00:00
|
|
|
def initialize(url: Gitlab::Saas.com_url, token: nil)
|
2020-10-23 15:08:42 +00:00
|
|
|
@url = Gitlab::Utils.append_path(url, '/api/graphql')
|
|
|
|
@token = token
|
2021-06-25 12:07:44 +00:00
|
|
|
@client = Graphlient::Client.new(@url, options(http: HTTP))
|
|
|
|
@compatible_instance_version = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(*args)
|
|
|
|
validate_instance_version!
|
|
|
|
|
|
|
|
client.execute(*args)
|
2020-10-23 15:08:42 +00:00
|
|
|
end
|
|
|
|
|
2020-11-13 09:09:30 +00:00
|
|
|
def options(extra = {})
|
|
|
|
return extra unless @token
|
2020-10-23 15:08:42 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Authorization' => "Bearer #{@token}"
|
|
|
|
}
|
2020-11-13 09:09:30 +00:00
|
|
|
}.merge(extra)
|
2020-10-23 15:08:42 +00:00
|
|
|
end
|
2021-06-25 12:07:44 +00:00
|
|
|
|
|
|
|
def validate_instance_version!
|
|
|
|
return if @compatible_instance_version
|
|
|
|
|
|
|
|
response = client.execute('{ metadata { version } }')
|
|
|
|
version = Gitlab::VersionInfo.parse(response.data.metadata.version)
|
|
|
|
|
2021-10-13 12:12:20 +00:00
|
|
|
if version.major < BulkImport::MIN_MAJOR_VERSION
|
2021-06-25 12:07:44 +00:00
|
|
|
raise ::BulkImports::Error.unsupported_gitlab_version
|
|
|
|
else
|
|
|
|
@compatible_instance_version = true
|
|
|
|
end
|
|
|
|
end
|
2020-10-23 15:08:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|