debian-mirror-gitlab/lib/bulk_imports/clients/graphql.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
1.8 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module BulkImports
module Clients
class Graphql
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-11-18 22:05:49 +05:30
rescue *Gitlab::HTTP::HTTP_ERRORS => e
raise ::BulkImports::NetworkError, e
2021-01-29 00:20:46 +05:30
end
end
private_constant :HTTP
attr_reader :client
2021-09-30 23:02:18 +05:30
delegate :query, :parse, to: :client
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
def initialize(url: Gitlab::Saas.com_url, token: nil)
2021-01-29 00:20:46 +05:30
@url = Gitlab::Utils.append_path(url, '/api/graphql')
@token = token
2021-09-30 23:02:18 +05:30
@client = Graphlient::Client.new(@url, options(http: HTTP))
@compatible_instance_version = false
end
def execute(*args)
validate_instance_version!
client.execute(*args)
2021-01-29 00:20:46 +05:30
end
def options(extra = {})
return extra unless @token
{
headers: {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@token}"
}
}.merge(extra)
end
2021-09-30 23:02:18 +05:30
def validate_instance_version!
return if @compatible_instance_version
response = client.execute('{ metadata { version } }')
version = Gitlab::VersionInfo.parse(response.data.metadata.version)
2021-11-18 22:05:49 +05:30
if version.major < BulkImport::MIN_MAJOR_VERSION
2021-09-30 23:02:18 +05:30
raise ::BulkImports::Error.unsupported_gitlab_version
else
@compatible_instance_version = true
end
end
2021-01-29 00:20:46 +05:30
end
end
end