diff options
Diffstat (limited to 'lib/bulk_imports/common')
4 files changed, 73 insertions, 70 deletions
diff --git a/lib/bulk_imports/common/extractors/graphql_extractor.rb b/lib/bulk_imports/common/extractors/graphql_extractor.rb index 7d58032cfcc..c0cef61d2b2 100644 --- a/lib/bulk_imports/common/extractors/graphql_extractor.rb +++ b/lib/bulk_imports/common/extractors/graphql_extractor.rb @@ -6,15 +6,16 @@ module BulkImports class GraphqlExtractor def initialize(query) @query = query[:query] - @query_string = @query.to_s - @variables = @query.variables end def extract(context) - @context = context + client = graphql_client(context) Enumerator.new do |yielder| - result = graphql_client.execute(parsed_query, query_variables(context.entity)) + result = client.execute( + client.parse(query.to_s), + query.variables(context.entity) + ) yielder << result.original_hash.deep_dup end @@ -22,23 +23,17 @@ module BulkImports private - def graphql_client + attr_reader :query + + def graphql_client(context) @graphql_client ||= BulkImports::Clients::Graphql.new( - url: @context.configuration.url, - token: @context.configuration.access_token + url: context.configuration.url, + token: context.configuration.access_token ) end def parsed_query - @parsed_query ||= graphql_client.parse(@query.to_s) - end - - def query_variables(entity) - return unless @variables - - @variables.transform_values do |entity_attribute| - entity.public_send(entity_attribute) # rubocop:disable GitlabSecurity/PublicSend - end + @parsed_query ||= graphql_client.parse(query.to_s) end end end diff --git a/lib/bulk_imports/common/transformers/graphql_cleaner_transformer.rb b/lib/bulk_imports/common/transformers/graphql_cleaner_transformer.rb deleted file mode 100644 index dce0fac6999..00000000000 --- a/lib/bulk_imports/common/transformers/graphql_cleaner_transformer.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -# Cleanup GraphQL original response hash from unnecessary nesting -# 1. Remove ['data']['group'] or ['data']['project'] hash nesting -# 2. Remove ['edges'] & ['nodes'] array wrappings -# 3. Remove ['node'] hash wrapping -# -# @example -# data = {"data"=>{"group"=> { -# "name"=>"test", -# "fullName"=>"test", -# "description"=>"test", -# "labels"=>{"edges"=>[{"node"=>{"title"=>"label1"}}, {"node"=>{"title"=>"label2"}}, {"node"=>{"title"=>"label3"}}]}}}} -# -# BulkImports::Common::Transformers::GraphqlCleanerTransformer.new.transform(nil, data) -# -# {"name"=>"test", "fullName"=>"test", "description"=>"test", "labels"=>[{"title"=>"label1"}, {"title"=>"label2"}, {"title"=>"label3"}]} -module BulkImports - module Common - module Transformers - class GraphqlCleanerTransformer - EDGES = 'edges' - NODE = 'node' - - def initialize(options = {}) - @options = options - end - - def transform(_, data) - return data unless data.is_a?(Hash) - - data = data.dig('data', 'group') || data.dig('data', 'project') || data - - clean_edges_and_nodes(data) - end - - def clean_edges_and_nodes(data) - case data - when Array - data.map(&method(:clean_edges_and_nodes)) - when Hash - if data.key?(NODE) - clean_edges_and_nodes(data[NODE]) - else - data.transform_values { |value| clean_edges_and_nodes(value.try(:fetch, EDGES, value) || value) } - end - else - data - end - end - end - end - end -end diff --git a/lib/bulk_imports/common/transformers/hash_key_digger.rb b/lib/bulk_imports/common/transformers/hash_key_digger.rb new file mode 100644 index 00000000000..b4897b5b2bf --- /dev/null +++ b/lib/bulk_imports/common/transformers/hash_key_digger.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module BulkImports + module Common + module Transformers + class HashKeyDigger + def initialize(options = {}) + @key_path = options[:key_path] + end + + def transform(_, data) + raise ArgumentError, "Given data must be a Hash" unless data.is_a?(Hash) + + data.dig(*Array.wrap(key_path)) + end + + private + + attr_reader :key_path + end + end + end +end diff --git a/lib/bulk_imports/common/transformers/prohibited_attributes_transformer.rb b/lib/bulk_imports/common/transformers/prohibited_attributes_transformer.rb new file mode 100644 index 00000000000..858c4c8976b --- /dev/null +++ b/lib/bulk_imports/common/transformers/prohibited_attributes_transformer.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module BulkImports + module Common + module Transformers + class ProhibitedAttributesTransformer + PROHIBITED_REFERENCES = Regexp.union( + /\Acached_markdown_version\Z/, + /\Aid\Z/, + /_id\Z/, + /_ids\Z/, + /_html\Z/, + /attributes/, + /\Aremote_\w+_(url|urls|request_header)\Z/ # carrierwave automatically creates these attribute methods for uploads + ).freeze + + def initialize(options = {}) + @options = options + end + + def transform(context, data) + data.each_with_object({}) do |(key, value), result| + prohibited = prohibited_key?(key) + + unless prohibited + result[key] = value.is_a?(Hash) ? transform(context, value) : value + end + end + end + + private + + def prohibited_key?(key) + key.to_s =~ PROHIBITED_REFERENCES + end + end + end + end +end |