Commit cda6fddf authored by Kassio Borges's avatar Kassio Borges

Simplify subgroups pipeline on BulkImports

To better leverage the BulkImports pipeline structure, instead of
creating a nested array, for the subgroups pages, prefer to create a
flat array and let the `Pipeline#run` handle the iteration of the
extracted data.
parent 6a0db429
...@@ -3,15 +3,11 @@ ...@@ -3,15 +3,11 @@
module BulkImports module BulkImports
module Common module Common
module Loaders module Loaders
class EntitiesLoader class EntityLoader
def initialize(*args); end def initialize(*args); end
def load(context, entities) def load(context, entity)
bulk_import = context.entity.bulk_import context.entity.bulk_import.entities.create!(entity)
entities.each do |entity|
bulk_import.entities.create!(entity)
end
end end
end end
end end
......
...@@ -9,12 +9,9 @@ module BulkImports ...@@ -9,12 +9,9 @@ module BulkImports
def extract(context) def extract(context)
encoded_parent_path = ERB::Util.url_encode(context.entity.source_full_path) encoded_parent_path = ERB::Util.url_encode(context.entity.source_full_path)
subgroups = []
http_client(context.entity.bulk_import.configuration) http_client(context.entity.bulk_import.configuration)
.each_page(:get, "groups/#{encoded_parent_path}/subgroups") do |page| .each_page(:get, "groups/#{encoded_parent_path}/subgroups")
subgroups << page .flat_map(&:itself)
end
subgroups
end end
private private
......
...@@ -7,8 +7,8 @@ module BulkImports ...@@ -7,8 +7,8 @@ module BulkImports
include Pipeline include Pipeline
extractor BulkImports::Groups::Extractors::SubgroupsExtractor extractor BulkImports::Groups::Extractors::SubgroupsExtractor
transformer BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer transformer BulkImports::Groups::Transformers::SubgroupToEntityTransformer
loader BulkImports::Common::Loaders::EntitiesLoader loader BulkImports::Common::Loaders::EntityLoader
end end
end end
end end
......
# frozen_string_literal: true
module BulkImports
module Groups
module Transformers
class SubgroupToEntityTransformer
def initialize(*args); end
def transform(context, entry)
{
source_type: :group_entity,
source_full_path: entry['full_path'],
destination_name: entry['name'],
destination_namespace: context.entity.group.full_path,
parent_id: context.entity.id
}
end
end
end
end
end
# frozen_string_literal: true
module BulkImports
module Groups
module Transformers
class SubgroupsToEntitiesTransformer
def initialize(*args); end
def transform(context, data)
data.map do |entry|
{
source_type: :group_entity,
source_full_path: entry['full_path'],
destination_name: entry['name'],
destination_namespace: context.entity.group.full_path,
parent_id: context.entity.id
}
end
end
end
end
end
end
...@@ -2,20 +2,20 @@ ...@@ -2,20 +2,20 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe BulkImports::Common::Loaders::EntitiesLoader do RSpec.describe BulkImports::Common::Loaders::EntityLoader do
describe '#load' do describe '#load' do
it "creates entities for the given data" do it "creates entities for the given data" do
group = create(:group, path: "imported-group") group = create(:group, path: "imported-group")
parent_entity = create(:bulk_import_entity, group: group, bulk_import: create(:bulk_import)) parent_entity = create(:bulk_import_entity, group: group, bulk_import: create(:bulk_import))
context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity) context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity)
data = [{ data = {
source_type: :group_entity, source_type: :group_entity,
source_full_path: "parent/subgroup", source_full_path: "parent/subgroup",
destination_name: "subgroup", destination_name: "subgroup",
destination_namespace: parent_entity.group.full_path, destination_namespace: parent_entity.group.full_path,
parent_id: parent_entity.id parent_id: parent_entity.id
}] }
expect { subject.load(context, data) }.to change(BulkImports::Entity, :count).by(1) expect { subject.load(context, data) }.to change(BulkImports::Entity, :count).by(1)
......
...@@ -35,7 +35,7 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do ...@@ -35,7 +35,7 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do
before do before do
allow_next_instance_of(BulkImports::Groups::Extractors::SubgroupsExtractor) do |extractor| allow_next_instance_of(BulkImports::Groups::Extractors::SubgroupsExtractor) do |extractor|
allow(extractor).to receive(:extract).and_return([subgroup_data]) allow(extractor).to receive(:extract).and_return(subgroup_data)
end end
parent.add_owner(user) parent.add_owner(user)
...@@ -67,14 +67,14 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do ...@@ -67,14 +67,14 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do
it 'has transformers' do it 'has transformers' do
expect(described_class.transformers).to contain_exactly( expect(described_class.transformers).to contain_exactly(
klass: BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer, klass: BulkImports::Groups::Transformers::SubgroupToEntityTransformer,
options: nil options: nil
) )
end end
it 'has loaders' do it 'has loaders' do
expect(described_class.loaders).to contain_exactly( expect(described_class.loaders).to contain_exactly(
klass: BulkImports::Common::Loaders::EntitiesLoader, klass: BulkImports::Common::Loaders::EntityLoader,
options: nil options: nil
) )
end end
......
...@@ -2,20 +2,18 @@ ...@@ -2,20 +2,18 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer do RSpec.describe BulkImports::Groups::Transformers::SubgroupToEntityTransformer do
describe "#transform" do describe "#transform" do
it "transforms subgroups data in entity params" do it "transforms subgroups data in entity params" do
parent = create(:group) parent = create(:group)
parent_entity = instance_double(BulkImports::Entity, group: parent, id: 1) parent_entity = instance_double(BulkImports::Entity, group: parent, id: 1)
context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity) context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity)
subgroup_data = [ subgroup_data = {
{
"name" => "subgroup", "name" => "subgroup",
"full_path" => "parent/subgroup" "full_path" => "parent/subgroup"
} }
]
expect(subject.transform(context, subgroup_data)).to contain_exactly( expect(subject.transform(context, subgroup_data)).to eq(
source_type: :group_entity, source_type: :group_entity,
source_full_path: "parent/subgroup", source_full_path: "parent/subgroup",
destination_name: "subgroup", destination_name: "subgroup",
......
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