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 @@
module BulkImports
module Common
module Loaders
class EntitiesLoader
class EntityLoader
def initialize(*args); end
def load(context, entities)
bulk_import = context.entity.bulk_import
entities.each do |entity|
bulk_import.entities.create!(entity)
end
def load(context, entity)
context.entity.bulk_import.entities.create!(entity)
end
end
end
......
......@@ -9,12 +9,9 @@ module BulkImports
def extract(context)
encoded_parent_path = ERB::Util.url_encode(context.entity.source_full_path)
subgroups = []
http_client(context.entity.bulk_import.configuration)
.each_page(:get, "groups/#{encoded_parent_path}/subgroups") do |page|
subgroups << page
end
subgroups
.each_page(:get, "groups/#{encoded_parent_path}/subgroups")
.flat_map(&:itself)
end
private
......
......@@ -7,8 +7,8 @@ module BulkImports
include Pipeline
extractor BulkImports::Groups::Extractors::SubgroupsExtractor
transformer BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer
loader BulkImports::Common::Loaders::EntitiesLoader
transformer BulkImports::Groups::Transformers::SubgroupToEntityTransformer
loader BulkImports::Common::Loaders::EntityLoader
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 @@
require 'spec_helper'
RSpec.describe BulkImports::Common::Loaders::EntitiesLoader do
RSpec.describe BulkImports::Common::Loaders::EntityLoader do
describe '#load' do
it "creates entities for the given data" do
group = create(:group, path: "imported-group")
parent_entity = create(:bulk_import_entity, group: group, bulk_import: create(:bulk_import))
context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity)
data = [{
data = {
source_type: :group_entity,
source_full_path: "parent/subgroup",
destination_name: "subgroup",
destination_namespace: parent_entity.group.full_path,
parent_id: parent_entity.id
}]
}
expect { subject.load(context, data) }.to change(BulkImports::Entity, :count).by(1)
......
......@@ -35,7 +35,7 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do
before do
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
parent.add_owner(user)
......@@ -67,14 +67,14 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do
it 'has transformers' do
expect(described_class.transformers).to contain_exactly(
klass: BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer,
klass: BulkImports::Groups::Transformers::SubgroupToEntityTransformer,
options: nil
)
end
it 'has loaders' do
expect(described_class.loaders).to contain_exactly(
klass: BulkImports::Common::Loaders::EntitiesLoader,
klass: BulkImports::Common::Loaders::EntityLoader,
options: nil
)
end
......
......@@ -2,20 +2,18 @@
require 'spec_helper'
RSpec.describe BulkImports::Groups::Transformers::SubgroupsToEntitiesTransformer do
RSpec.describe BulkImports::Groups::Transformers::SubgroupToEntityTransformer do
describe "#transform" do
it "transforms subgroups data in entity params" do
parent = create(:group)
parent_entity = instance_double(BulkImports::Entity, group: parent, id: 1)
context = instance_double(BulkImports::Pipeline::Context, entity: parent_entity)
subgroup_data = [
{
"name" => "subgroup",
"full_path" => "parent/subgroup"
}
]
subgroup_data = {
"name" => "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_full_path: "parent/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