Commit 56afc1fc authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'georgekoltsov/project-migration-badges' into 'master'

Add badges to Project Migration

See merge request gitlab-org/gitlab!75029
parents b0bad997 6c44e1ac
...@@ -104,8 +104,12 @@ class BulkImports::Entity < ApplicationRecord ...@@ -104,8 +104,12 @@ class BulkImports::Entity < ApplicationRecord
end end
end end
def entity_type
source_type.gsub('_entity', '')
end
def pluralized_name def pluralized_name
source_type.gsub('_entity', '').pluralize entity_type.pluralize
end end
def export_relations_url_path def export_relations_url_path
...@@ -116,6 +120,14 @@ class BulkImports::Entity < ApplicationRecord ...@@ -116,6 +120,14 @@ class BulkImports::Entity < ApplicationRecord
"#{export_relations_url_path}/download?relation=#{relation}" "#{export_relations_url_path}/download?relation=#{relation}"
end end
def project?
source_type == 'project_entity'
end
def group?
source_type == 'group_entity'
end
private private
def validate_parent_is_a_group def validate_parent_is_a_group
......
...@@ -11,7 +11,7 @@ RSpec.describe BulkImports::Groups::Stage do ...@@ -11,7 +11,7 @@ RSpec.describe BulkImports::Groups::Stage do
[1, BulkImports::Groups::Pipelines::MembersPipeline], [1, BulkImports::Groups::Pipelines::MembersPipeline],
[1, BulkImports::Common::Pipelines::LabelsPipeline], [1, BulkImports::Common::Pipelines::LabelsPipeline],
[1, BulkImports::Common::Pipelines::MilestonesPipeline], [1, BulkImports::Common::Pipelines::MilestonesPipeline],
[1, BulkImports::Groups::Pipelines::BadgesPipeline], [1, BulkImports::Common::Pipelines::BadgesPipeline],
[1, BulkImports::Groups::Pipelines::IterationsPipeline], [1, BulkImports::Groups::Pipelines::IterationsPipeline],
[1, BulkImports::Groups::Pipelines::ProjectEntitiesPipeline], [1, BulkImports::Groups::Pipelines::ProjectEntitiesPipeline],
[2, BulkImports::Common::Pipelines::BoardsPipeline], [2, BulkImports::Common::Pipelines::BoardsPipeline],
......
...@@ -9,6 +9,7 @@ RSpec.describe BulkImports::Projects::Stage do ...@@ -9,6 +9,7 @@ RSpec.describe BulkImports::Projects::Stage do
[1, BulkImports::Projects::Pipelines::RepositoryPipeline], [1, BulkImports::Projects::Pipelines::RepositoryPipeline],
[2, BulkImports::Common::Pipelines::LabelsPipeline], [2, BulkImports::Common::Pipelines::LabelsPipeline],
[2, BulkImports::Common::Pipelines::MilestonesPipeline], [2, BulkImports::Common::Pipelines::MilestonesPipeline],
[2, BulkImports::Common::Pipelines::BadgesPipeline],
[3, BulkImports::Projects::Pipelines::IssuesPipeline], [3, BulkImports::Projects::Pipelines::IssuesPipeline],
[3, BulkImports::Projects::Pipelines::SnippetsPipeline], [3, BulkImports::Projects::Pipelines::SnippetsPipeline],
[4, BulkImports::Common::Pipelines::BoardsPipeline], [4, BulkImports::Common::Pipelines::BoardsPipeline],
......
# frozen_string_literal: true # frozen_string_literal: true
module BulkImports module BulkImports
module Groups module Common
module Pipelines module Pipelines
class BadgesPipeline class BadgesPipeline
include Pipeline include Pipeline
extractor BulkImports::Common::Extractors::RestExtractor, extractor BulkImports::Common::Extractors::RestExtractor,
query: BulkImports::Groups::Rest::GetBadgesQuery query: BulkImports::Common::Rest::GetBadgesQuery
transformer Common::Transformers::ProhibitedAttributesTransformer transformer Common::Transformers::ProhibitedAttributesTransformer
def transform(_, data) def transform(context, data)
return if data.blank? return if data.blank?
# Project badges API returns badges of both group and project kind. To avoid creation of duplicates for the group we skip group badges when it's a project.
return if context.entity.project? && group_badge?(data)
{ {
name: data['name'], name: data['name'],
...@@ -24,7 +26,17 @@ module BulkImports ...@@ -24,7 +26,17 @@ module BulkImports
def load(context, data) def load(context, data)
return if data.blank? return if data.blank?
context.group.badges.create!(data) if context.entity.project?
context.portable.project_badges.create!(data)
else
context.portable.badges.create!(data)
end
end
private
def group_badge?(data)
data['kind'] == 'group'
end end
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module BulkImports module BulkImports
module Groups module Common
module Rest module Rest
module GetBadgesQuery module GetBadgesQuery
extend self extend self
def to_h(context) def to_h(context)
resource = context.entity.pluralized_name
encoded_full_path = ERB::Util.url_encode(context.entity.source_full_path) encoded_full_path = ERB::Util.url_encode(context.entity.source_full_path)
{ {
resource: ['groups', encoded_full_path, 'badges'].join('/'), resource: [resource, encoded_full_path, 'badges'].join('/'),
query: { query: {
page: context.tracker.next_page page: context.tracker.next_page
} }
......
...@@ -32,7 +32,7 @@ module BulkImports ...@@ -32,7 +32,7 @@ module BulkImports
stage: 1 stage: 1
}, },
badges: { badges: {
pipeline: BulkImports::Groups::Pipelines::BadgesPipeline, pipeline: BulkImports::Common::Pipelines::BadgesPipeline,
stage: 1 stage: 1
}, },
boards: { boards: {
......
...@@ -23,6 +23,10 @@ module BulkImports ...@@ -23,6 +23,10 @@ module BulkImports
pipeline: BulkImports::Common::Pipelines::MilestonesPipeline, pipeline: BulkImports::Common::Pipelines::MilestonesPipeline,
stage: 2 stage: 2
}, },
badges: {
pipeline: BulkImports::Common::Pipelines::BadgesPipeline,
stage: 2
},
issues: { issues: {
pipeline: BulkImports::Projects::Pipelines::IssuesPipeline, pipeline: BulkImports::Projects::Pipelines::IssuesPipeline,
stage: 3 stage: 3
......
...@@ -2,37 +2,29 @@ ...@@ -2,37 +2,29 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do RSpec.describe BulkImports::Common::Pipelines::BadgesPipeline do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project) }
let_it_be(:entity) do let(:entity) { create(:bulk_import_entity, group: group) }
create( let(:tracker) { create(:bulk_import_tracker, entity: entity) }
:bulk_import_entity, let(:context) { BulkImports::Pipeline::Context.new(tracker) }
source_full_path: 'source/full/path',
destination_name: 'My Destination Group',
destination_namespace: group.full_path,
group: group
)
end
let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) }
let_it_be(:context) { BulkImports::Pipeline::Context.new(tracker) }
subject { described_class.new(context) } subject(:pipeline) { described_class.new(context) }
describe '#run' do describe '#run' do
it 'imports a group badge' do let(:first_page) { extracted_data(has_next_page: true) }
first_page = extracted_data(has_next_page: true) let(:last_page) { extracted_data(name: 'badge2') }
last_page = extracted_data(name: 'badge2')
before do
allow_next_instance_of(BulkImports::Common::Extractors::RestExtractor) do |extractor| allow_next_instance_of(BulkImports::Common::Extractors::RestExtractor) do |extractor|
allow(extractor) allow(extractor).to receive(:extract).and_return(first_page, last_page)
.to receive(:extract)
.and_return(first_page, last_page)
end end
end
expect { subject.run }.to change(Badge, :count).by(2) it 'imports a group badge' do
expect { pipeline.run }.to change(Badge, :count).by(2)
badge = group.badges.last badge = group.badges.last
...@@ -41,19 +33,20 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do ...@@ -41,19 +33,20 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do
expect(badge.image_url).to eq(badge_data['image_url']) expect(badge.image_url).to eq(badge_data['image_url'])
end end
describe '#load' do context 'when project entity' do
it 'creates a badge' do let(:first_page) { extracted_data(has_next_page: true) }
expect { subject.load(context, badge_data) }.to change(Badge, :count).by(1) let(:last_page) { extracted_data(name: 'badge2', kind: 'project') }
let(:entity) { create(:bulk_import_entity, :project_entity, project: project) }
badge = group.badges.first it 'imports a project badge & skips group badge' do
expect { pipeline.run }.to change(Badge, :count).by(1)
badge_data.each do |key, value| badge = project.badges.last
expect(badge[key]).to eq(value)
end
end
it 'does nothing when the data is blank' do expect(badge.name).to eq('badge2')
expect { subject.load(context, nil) }.not_to change(Badge, :count) expect(badge.link_url).to eq(badge_data['link_url'])
expect(badge.image_url).to eq(badge_data['image_url'])
expect(badge.type).to eq('ProjectBadge')
end end
end end
...@@ -72,45 +65,32 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do ...@@ -72,45 +65,32 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do
expect(subject.transform(context, nil)).to be_nil expect(subject.transform(context, nil)).to be_nil
end end
end end
end
describe 'pipeline parts' do context 'when project entity & group badge' do
it { expect(described_class).to include_module(BulkImports::Pipeline) } let(:entity) { create(:bulk_import_entity, :project_entity, project: project) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Runner) }
it 'has extractors' do
expect(described_class.get_extractor)
.to eq(
klass: BulkImports::Common::Extractors::RestExtractor,
options: {
query: BulkImports::Groups::Rest::GetBadgesQuery
}
)
end
it 'has transformers' do it 'returns' do
expect(described_class.transformers) expect(subject.transform(context, { 'name' => 'test', 'kind' => 'group' })).to be_nil
.to contain_exactly( end
{ klass: BulkImports::Common::Transformers::ProhibitedAttributesTransformer, options: nil }
)
end end
end end
def badge_data(name = 'badge') def badge_data(name = 'badge', kind = 'group')
{ {
'name' => name, 'name' => name,
'link_url' => 'https://gitlab.example.com', 'link_url' => 'https://gitlab.example.com',
'image_url' => 'https://gitlab.example.com/image.png' 'image_url' => 'https://gitlab.example.com/image.png',
'kind' => kind
} }
end end
def extracted_data(name: 'badge', has_next_page: false) def extracted_data(name: 'badge', kind: 'group', has_next_page: false)
page_info = { page_info = {
'has_next_page' => has_next_page, 'has_next_page' => has_next_page,
'next_page' => has_next_page ? '2' : nil 'next_page' => has_next_page ? '2' : nil
} }
BulkImports::Pipeline::ExtractedData.new(data: [badge_data(name)], page_info: page_info) BulkImports::Pipeline::ExtractedData.new(data: [badge_data(name, kind)], page_info: page_info)
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::Common::Rest::GetBadgesQuery do
describe '.to_h' do
shared_examples 'resource and page info query' do
let(:tracker) { create(:bulk_import_tracker, entity: entity) }
let(:context) { BulkImports::Pipeline::Context.new(tracker) }
let(:encoded_full_path) { ERB::Util.url_encode(entity.source_full_path) }
it 'returns correct query and page info' do
expected = {
resource: [entity.pluralized_name, encoded_full_path, 'badges'].join('/'),
query: {
page: context.tracker.next_page
}
}
expect(described_class.to_h(context)).to eq(expected)
end
end
context 'when entity is group' do
let(:entity) { create(:bulk_import_entity) }
include_examples 'resource and page info query'
end
context 'when entity is project' do
let(:entity) { create(:bulk_import_entity, :project_entity) }
include_examples 'resource and page info query'
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::Groups::Rest::GetBadgesQuery do
describe '.to_h' do
it 'returns query resource and page info' do
entity = create(:bulk_import_entity)
tracker = create(:bulk_import_tracker, entity: entity)
context = BulkImports::Pipeline::Context.new(tracker)
encoded_full_path = ERB::Util.url_encode(entity.source_full_path)
expected = {
resource: ['groups', encoded_full_path, 'badges'].join('/'),
query: {
page: context.tracker.next_page
}
}
expect(described_class.to_h(context)).to eq(expected)
end
end
end
...@@ -13,7 +13,7 @@ RSpec.describe BulkImports::Groups::Stage do ...@@ -13,7 +13,7 @@ RSpec.describe BulkImports::Groups::Stage do
[1, BulkImports::Groups::Pipelines::MembersPipeline], [1, BulkImports::Groups::Pipelines::MembersPipeline],
[1, BulkImports::Common::Pipelines::LabelsPipeline], [1, BulkImports::Common::Pipelines::LabelsPipeline],
[1, BulkImports::Common::Pipelines::MilestonesPipeline], [1, BulkImports::Common::Pipelines::MilestonesPipeline],
[1, BulkImports::Groups::Pipelines::BadgesPipeline], [1, BulkImports::Common::Pipelines::BadgesPipeline],
[2, BulkImports::Common::Pipelines::BoardsPipeline] [2, BulkImports::Common::Pipelines::BoardsPipeline]
] ]
end end
......
...@@ -11,6 +11,7 @@ RSpec.describe BulkImports::Projects::Stage do ...@@ -11,6 +11,7 @@ RSpec.describe BulkImports::Projects::Stage do
[1, BulkImports::Projects::Pipelines::RepositoryPipeline], [1, BulkImports::Projects::Pipelines::RepositoryPipeline],
[2, BulkImports::Common::Pipelines::LabelsPipeline], [2, BulkImports::Common::Pipelines::LabelsPipeline],
[2, BulkImports::Common::Pipelines::MilestonesPipeline], [2, BulkImports::Common::Pipelines::MilestonesPipeline],
[2, BulkImports::Common::Pipelines::BadgesPipeline],
[3, BulkImports::Projects::Pipelines::IssuesPipeline], [3, BulkImports::Projects::Pipelines::IssuesPipeline],
[3, BulkImports::Projects::Pipelines::SnippetsPipeline], [3, BulkImports::Projects::Pipelines::SnippetsPipeline],
[4, BulkImports::Common::Pipelines::BoardsPipeline], [4, BulkImports::Common::Pipelines::BoardsPipeline],
......
...@@ -252,4 +252,34 @@ RSpec.describe BulkImports::Entity, type: :model do ...@@ -252,4 +252,34 @@ RSpec.describe BulkImports::Entity, type: :model do
.to eq("/groups/#{entity.encoded_source_full_path}/export_relations/download?relation=test") .to eq("/groups/#{entity.encoded_source_full_path}/export_relations/download?relation=test")
end end
end end
describe '#entity_type' do
it 'returns entity type' do
group_entity = build(:bulk_import_entity)
project_entity = build(:bulk_import_entity, :project_entity)
expect(group_entity.entity_type).to eq('group')
expect(project_entity.entity_type).to eq('project')
end
end
describe '#project?' do
it 'returns true if project entity' do
group_entity = build(:bulk_import_entity)
project_entity = build(:bulk_import_entity, :project_entity)
expect(group_entity.project?).to eq(false)
expect(project_entity.project?).to eq(true)
end
end
describe '#group?' do
it 'returns true if group entity' do
group_entity = build(:bulk_import_entity)
project_entity = build(:bulk_import_entity, :project_entity)
expect(group_entity.group?).to eq(true)
expect(project_entity.group?).to eq(false)
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