Commit a7e53cd2 authored by Kassio Borges's avatar Kassio Borges

BulkImports: Import Epic's labels

Import the LabelLink between the Labels and Epics.
parent e242480c
---
title: "BulkImports: Import Epic's labels"
merge_request: 52626
author:
type: changed
...@@ -42,6 +42,11 @@ module EE ...@@ -42,6 +42,11 @@ module EE
iid iid
} }
} }
labels {
nodes {
title
}
}
} }
} }
} }
......
...@@ -12,7 +12,7 @@ module EE ...@@ -12,7 +12,7 @@ module EE
end end
def load(context, data) def load(context, data)
raise NotAllowedError unless context.current_user.can?(:create_epic, context.group) raise NotAllowedError unless authorized?(context)
# Use `Epic` directly when creating new epics # Use `Epic` directly when creating new epics
# instead of `Epics::CreateService` since several # instead of `Epics::CreateService` since several
...@@ -20,6 +20,12 @@ module EE ...@@ -20,6 +20,12 @@ module EE
# group_id, parent, children need to be custom set # group_id, parent, children need to be custom set
::Epic.create!(data) ::Epic.create!(data)
end end
private
def authorized?(context)
Ability.allowed?(context.current_user, :admin_epic, context.group)
end
end end
end end
end end
......
...@@ -13,12 +13,13 @@ module EE ...@@ -13,12 +13,13 @@ module EE
.then { |data| add_author_id(context, data) } .then { |data| add_author_id(context, data) }
.then { |data| add_parent(context, data) } .then { |data| add_parent(context, data) }
.then { |data| add_children(context, data) } .then { |data| add_children(context, data) }
.then { |data| add_labels(context, data) }
end end
private private
def add_group_id(context, data) def add_group_id(context, data)
data.merge('group_id' => context.entity.namespace_id) data.merge('group_id' => context.group.id)
end end
def add_author_id(context, data) def add_author_id(context, data)
...@@ -27,7 +28,7 @@ module EE ...@@ -27,7 +28,7 @@ module EE
def add_parent(context, data) def add_parent(context, data)
data.merge( data.merge(
'parent' => context.entity.group.epics.find_by_iid(data.dig('parent', 'iid')) 'parent' => context.group.epics.find_by_iid(data.dig('parent', 'iid'))
) )
end end
...@@ -35,7 +36,15 @@ module EE ...@@ -35,7 +36,15 @@ module EE
nodes = Array.wrap(data.dig('children', 'nodes')) nodes = Array.wrap(data.dig('children', 'nodes'))
children_iids = nodes.filter_map { |child| child['iid'] } children_iids = nodes.filter_map { |child| child['iid'] }
data.merge('children' => context.entity.group.epics.where(iid: children_iids)) # rubocop: disable CodeReuse/ActiveRecord data.merge('children' => context.group.epics.where(iid: children_iids)) # rubocop: disable CodeReuse/ActiveRecord
end
def add_labels(context, data)
data['labels'] = data.dig('labels', 'nodes').filter_map do |node|
context.group.labels.find_by_title(node['title'])
end
data
end end
end end
end end
......
...@@ -4,32 +4,46 @@ require 'spec_helper' ...@@ -4,32 +4,46 @@ require 'spec_helper'
RSpec.describe EE::BulkImports::Groups::Loaders::EpicsLoader do RSpec.describe EE::BulkImports::Groups::Loaders::EpicsLoader do
describe '#load' do describe '#load' do
let(:user) { create(:user) } it 'creates the epic' do
let(:group) { create(:group) } stub_licensed_features(epics: true)
let(:bulk_import) { create(:bulk_import, user: user) }
let(:entity) { create(:bulk_import_entity, bulk_import: bulk_import, group: group) } user = create(:user)
let(:context) { BulkImports::Pipeline::Context.new(entity) } group = create(:group)
group.add_owner(user)
parent_epic = create(:epic, group: group)
child_epic = create(:epic, group: group)
label = create(:group_label, group: group)
bulk_import = create(:bulk_import, user: user)
entity = create(:bulk_import_entity, bulk_import: bulk_import, group: group)
context = BulkImports::Pipeline::Context.new(entity)
let(:data) do data = {
{ 'title' => 'epic',
'title' => 'epic1',
'state' => 'opened', 'state' => 'opened',
'confidential' => false, 'confidential' => false,
'iid' => 1, 'iid' => 99,
'author_id' => user.id, 'author_id' => user.id,
'group_id' => group.id 'group_id' => group.id,
'parent' => parent_epic,
'children' => [child_epic],
'labels' => [
label
]
} }
end
before do
stub_licensed_features(epics: true)
group.add_owner(user)
end
it 'creates the epic' do
expect { subject.load(context, data) }.to change(::Epic, :count).by(1) expect { subject.load(context, data) }.to change(::Epic, :count).by(1)
expect(group.epics.count).to eq(1) epic = group.epics.last
expect(epic.group).to eq(group)
expect(epic.author).to eq(user)
expect(epic.title).to eq('epic')
expect(epic.state).to eq('opened')
expect(epic.confidential).to eq(false)
expect(epic.iid).to eq(99)
expect(epic.parent).to eq(parent_epic)
expect(epic.children).to contain_exactly(child_epic)
expect(epic.labels).to contain_exactly(label)
end end
end end
end end
...@@ -3,18 +3,18 @@ ...@@ -3,18 +3,18 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::BulkImports::Groups::Pipelines::EpicsPipeline do RSpec.describe EE::BulkImports::Groups::Pipelines::EpicsPipeline do
let(:cursor) { 'cursor' }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) } let(:group) { create(:group) }
let(:cursor) { 'cursor' }
let(:bulk_import) { create(:bulk_import, user: user) } let(:bulk_import) { create(:bulk_import, user: user) }
let(:entity) do let(:entity) do
create( create(
:bulk_import_entity, :bulk_import_entity,
group: group,
bulk_import: bulk_import,
source_full_path: 'source/full/path', source_full_path: 'source/full/path',
destination_name: 'My Destination Group', destination_name: 'My Destination Group',
destination_namespace: group.full_path, destination_namespace: group.full_path
group: group,
bulk_import: bulk_import
) )
end end
...@@ -104,7 +104,10 @@ RSpec.describe EE::BulkImports::Groups::Pipelines::EpicsPipeline do ...@@ -104,7 +104,10 @@ RSpec.describe EE::BulkImports::Groups::Pipelines::EpicsPipeline do
{ {
'title' => 'epic1', 'title' => 'epic1',
'state' => 'closed', 'state' => 'closed',
'confidential' => true 'confidential' => true,
'labels' => {
'nodes' => []
}
} }
] ]
......
...@@ -3,69 +3,94 @@ ...@@ -3,69 +3,94 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::BulkImports::Groups::Transformers::EpicAttributesTransformer do RSpec.describe EE::BulkImports::Groups::Transformers::EpicAttributesTransformer do
describe '#transform' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group, name: 'My Source Group') } let(:group) { create(:group) }
let(:entity) do let(:bulk_import) { create(:bulk_import, user: user) }
instance_double( let(:entity) { create(:bulk_import_entity, bulk_import: bulk_import, group: group) }
BulkImports::Entity, let(:context) { ::BulkImports::Pipeline::Context.new(entity) }
group: group,
namespace_id: group.id
)
end
let(:context) do describe '#transform' do
instance_double( it 'transforms the epic attributes' do
BulkImports::Pipeline::Context, transformed = subject.transform(context, epic_data)
current_user: user,
entity: entity
)
end
let(:data) do expect(transformed).to eq({
{ 'title' => 'title',
'iid' => '7', 'description' => 'description',
'title' => 'Epic Title',
'description' => 'Epic Description',
'state' => 'opened', 'state' => 'opened',
'parent' => { 'create_at' => '2021-01-27T03:42:30Z',
'iid' => parent_iid 'closed_at' => nil,
}, 'start_date' => nil,
'children' => { 'start_date_fixed' => nil,
'nodes' => [ 'start_date_is_fixed' => false,
{ 'due_date_fixed' => nil,
'iid' => child_iid 'due_date_is_fixed' => false,
} 'relative_position' => 1073716855,
] 'confidential' => false,
} 'group_id' => group.id,
} 'author_id' => user.id,
'parent' => nil,
'children' => [],
'labels' => []
})
end end
context 'when parent and child iids are nil' do context 'labels' do
let(:parent_iid) { nil } it 'maps the existing labels' do
let(:child_iid) { nil } label = create(:group_label, group: group)
data = epic_data(labels_titles: [label.title, 'NON EXISTING TITLE'])
it 'sets group_id, author_id from context' do
transformed_data = subject.transform(context, data) transformed_data = subject.transform(context, data)
expect(transformed_data['group_id']).to eq(group.id) expect(transformed_data['labels'].to_a).to contain_exactly(label)
expect(transformed_data['author_id']).to eq(user.id)
expect(transformed_data['parent']).to be_nil
end end
end end
context 'when parent and child iids are present' do context 'parent and children epics' do
let(:parent) { create(:epic, group: group) } it 'sets parent and child epics when they exist' do
let(:child) { create(:epic, group: group) } parent = create(:epic, group: group)
let(:parent_iid) { parent.iid } child = create(:epic, group: group)
let(:child_iid) { child.iid } data = epic_data(parent_iid: parent.iid, children_iids: [child.iid])
it 'sets parent and child epics' do
transformed_data = subject.transform(context, data) transformed_data = subject.transform(context, data)
expect(transformed_data['parent']).to eq(parent) expect(transformed_data['parent']).to eq(parent)
expect(transformed_data['children']).to contain_exactly(child) expect(transformed_data['children']).to contain_exactly(child)
end end
it 'removes the parent and children id when they dont exist' do
data = epic_data(parent_iid: 9998, children_iids: [9999])
transformed_data = subject.transform(context, data)
expect(transformed_data['parent']).to be_nil
expect(transformed_data['children']).to be_empty
end
end
def epic_data(parent_iid: nil, children_iids: [], labels_titles: [])
{
'title' => 'title',
'description' => 'description',
'state' => 'opened',
'create_at' => '2021-01-27T03:42:30Z',
'closed_at' => nil,
'start_date' => nil,
'start_date_fixed' => nil,
'start_date_is_fixed' => false,
'due_date_fixed' => nil,
'due_date_is_fixed' => false,
'relative_position' => 1073716855,
'confidential' => false,
'parent' => {
'iid' => parent_iid
},
'children' => {
'nodes' => children_iids.map { |iid| { 'iid' => iid } }
},
'labels' => {
'nodes' => labels_titles.map { |title| { 'title' => title } }
}
}
end end
end end
end end
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