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
end
end
def entity_type
source_type.gsub('_entity', '')
end
def pluralized_name
source_type.gsub('_entity', '').pluralize
entity_type.pluralize
end
def export_relations_url_path
......@@ -116,6 +120,14 @@ class BulkImports::Entity < ApplicationRecord
"#{export_relations_url_path}/download?relation=#{relation}"
end
def project?
source_type == 'project_entity'
end
def group?
source_type == 'group_entity'
end
private
def validate_parent_is_a_group
......
......@@ -11,7 +11,7 @@ RSpec.describe BulkImports::Groups::Stage do
[1, BulkImports::Groups::Pipelines::MembersPipeline],
[1, BulkImports::Common::Pipelines::LabelsPipeline],
[1, BulkImports::Common::Pipelines::MilestonesPipeline],
[1, BulkImports::Groups::Pipelines::BadgesPipeline],
[1, BulkImports::Common::Pipelines::BadgesPipeline],
[1, BulkImports::Groups::Pipelines::IterationsPipeline],
[1, BulkImports::Groups::Pipelines::ProjectEntitiesPipeline],
[2, BulkImports::Common::Pipelines::BoardsPipeline],
......
......@@ -9,6 +9,7 @@ RSpec.describe BulkImports::Projects::Stage do
[1, BulkImports::Projects::Pipelines::RepositoryPipeline],
[2, BulkImports::Common::Pipelines::LabelsPipeline],
[2, BulkImports::Common::Pipelines::MilestonesPipeline],
[2, BulkImports::Common::Pipelines::BadgesPipeline],
[3, BulkImports::Projects::Pipelines::IssuesPipeline],
[3, BulkImports::Projects::Pipelines::SnippetsPipeline],
[4, BulkImports::Common::Pipelines::BoardsPipeline],
......
# frozen_string_literal: true
module BulkImports
module Groups
module Common
module Pipelines
class BadgesPipeline
include Pipeline
extractor BulkImports::Common::Extractors::RestExtractor,
query: BulkImports::Groups::Rest::GetBadgesQuery
query: BulkImports::Common::Rest::GetBadgesQuery
transformer Common::Transformers::ProhibitedAttributesTransformer
def transform(_, data)
def transform(context, data)
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'],
......@@ -24,7 +26,17 @@ module BulkImports
def load(context, data)
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
......
# frozen_string_literal: true
module BulkImports
module Groups
module Common
module Rest
module GetBadgesQuery
extend self
def to_h(context)
resource = context.entity.pluralized_name
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: {
page: context.tracker.next_page
}
......
......@@ -32,7 +32,7 @@ module BulkImports
stage: 1
},
badges: {
pipeline: BulkImports::Groups::Pipelines::BadgesPipeline,
pipeline: BulkImports::Common::Pipelines::BadgesPipeline,
stage: 1
},
boards: {
......
......@@ -23,6 +23,10 @@ module BulkImports
pipeline: BulkImports::Common::Pipelines::MilestonesPipeline,
stage: 2
},
badges: {
pipeline: BulkImports::Common::Pipelines::BadgesPipeline,
stage: 2
},
issues: {
pipeline: BulkImports::Projects::Pipelines::IssuesPipeline,
stage: 3
......
......@@ -2,37 +2,29 @@
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(:group) { create(:group) }
let_it_be(:project) { create(:project) }
let_it_be(:entity) do
create(
:bulk_import_entity,
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) }
let(:entity) { create(:bulk_import_entity, group: group) }
let(:tracker) { create(:bulk_import_tracker, entity: entity) }
let(:context) { BulkImports::Pipeline::Context.new(tracker) }
subject { described_class.new(context) }
subject(:pipeline) { described_class.new(context) }
describe '#run' do
it 'imports a group badge' do
first_page = extracted_data(has_next_page: true)
last_page = extracted_data(name: 'badge2')
let(:first_page) { extracted_data(has_next_page: true) }
let(:last_page) { extracted_data(name: 'badge2') }
before do
allow_next_instance_of(BulkImports::Common::Extractors::RestExtractor) do |extractor|
allow(extractor)
.to receive(:extract)
.and_return(first_page, last_page)
allow(extractor).to receive(:extract).and_return(first_page, last_page)
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
......@@ -41,19 +33,20 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do
expect(badge.image_url).to eq(badge_data['image_url'])
end
describe '#load' do
it 'creates a badge' do
expect { subject.load(context, badge_data) }.to change(Badge, :count).by(1)
context 'when project entity' do
let(:first_page) { extracted_data(has_next_page: true) }
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|
expect(badge[key]).to eq(value)
end
end
badge = project.badges.last
it 'does nothing when the data is blank' do
expect { subject.load(context, nil) }.not_to change(Badge, :count)
expect(badge.name).to eq('badge2')
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
......@@ -72,45 +65,32 @@ RSpec.describe BulkImports::Groups::Pipelines::BadgesPipeline do
expect(subject.transform(context, nil)).to be_nil
end
end
end
describe 'pipeline parts' do
it { expect(described_class).to include_module(BulkImports::Pipeline) }
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
context 'when project entity & group badge' do
let(:entity) { create(:bulk_import_entity, :project_entity, project: project) }
it 'has transformers' do
expect(described_class.transformers)
.to contain_exactly(
{ klass: BulkImports::Common::Transformers::ProhibitedAttributesTransformer, options: nil }
)
it 'returns' do
expect(subject.transform(context, { 'name' => 'test', 'kind' => 'group' })).to be_nil
end
end
end
def badge_data(name = 'badge')
def badge_data(name = 'badge', kind = 'group')
{
'name' => name,
'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
def extracted_data(name: 'badge', has_next_page: false)
def extracted_data(name: 'badge', kind: 'group', has_next_page: false)
page_info = {
'has_next_page' => has_next_page,
'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
# 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
[1, BulkImports::Groups::Pipelines::MembersPipeline],
[1, BulkImports::Common::Pipelines::LabelsPipeline],
[1, BulkImports::Common::Pipelines::MilestonesPipeline],
[1, BulkImports::Groups::Pipelines::BadgesPipeline],
[1, BulkImports::Common::Pipelines::BadgesPipeline],
[2, BulkImports::Common::Pipelines::BoardsPipeline]
]
end
......
......@@ -11,6 +11,7 @@ RSpec.describe BulkImports::Projects::Stage do
[1, BulkImports::Projects::Pipelines::RepositoryPipeline],
[2, BulkImports::Common::Pipelines::LabelsPipeline],
[2, BulkImports::Common::Pipelines::MilestonesPipeline],
[2, BulkImports::Common::Pipelines::BadgesPipeline],
[3, BulkImports::Projects::Pipelines::IssuesPipeline],
[3, BulkImports::Projects::Pipelines::SnippetsPipeline],
[4, BulkImports::Common::Pipelines::BoardsPipeline],
......
......@@ -252,4 +252,34 @@ RSpec.describe BulkImports::Entity, type: :model do
.to eq("/groups/#{entity.encoded_source_full_path}/export_relations/download?relation=test")
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
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