Commit 615b4955 authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'georgekoltsov/rename-exportable-to-portable' into 'master'

Rename `exportable` variables to `portable` within `BulkImports` namespace

See merge request gitlab-org/gitlab!61268
parents ff337ee3 9ee6a1c3
......@@ -15,7 +15,7 @@ module BulkImports
validates :group, presence: true, unless: :project
validates :relation, :status, presence: true
validate :exportable_relation?
validate :portable_relation?
state_machine :status, initial: :started do
state :started, value: 0
......@@ -36,34 +36,25 @@ module BulkImports
end
end
def self.config(exportable)
case exportable
when ::Project
Exports::ProjectConfig.new(exportable)
when ::Group
Exports::GroupConfig.new(exportable)
end
end
def exportable_relation?
return unless exportable
def portable_relation?
return unless portable
errors.add(:relation, 'Unsupported exportable relation') unless config.exportable_relations.include?(relation)
errors.add(:relation, 'Unsupported portable relation') unless config.portable_relations.include?(relation)
end
def exportable
strong_memoize(:exportable) do
def portable
strong_memoize(:portable) do
project || group
end
end
def relation_definition
config.exportable_tree[:include].find { |include| include[relation.to_sym] }
config.portable_tree[:include].find { |include| include[relation.to_sym] }
end
def config
strong_memoize(:config) do
self.class.config(exportable)
FileTransfer.config_for(portable)
end
end
end
......
# frozen_string_literal: true
module BulkImports
module FileTransfer
extend self
UnsupportedObjectType = Class.new(StandardError)
def config_for(portable)
case portable
when ::Project
FileTransfer::ProjectConfig.new(portable)
when ::Group
FileTransfer::GroupConfig.new(portable)
else
raise(UnsupportedObjectType, "Unsupported object type: #{portable.class}")
end
end
end
end
# frozen_string_literal: true
module BulkImports
module Exports
module FileTransfer
class BaseConfig
include Gitlab::Utils::StrongMemoize
def initialize(exportable)
@exportable = exportable
def initialize(portable)
@portable = portable
end
def exportable_tree
attributes_finder.find_root(exportable_class_sym)
def portable_tree
attributes_finder.find_root(portable_class_sym)
end
def export_path
......@@ -21,13 +21,13 @@ module BulkImports
end
end
def exportable_relations
import_export_config.dig(:tree, exportable_class_sym).keys.map(&:to_s)
def portable_relations
import_export_config.dig(:tree, portable_class_sym).keys.map(&:to_s)
end
private
attr_reader :exportable
attr_reader :portable
def attributes_finder
strong_memoize(:attributes_finder) do
......@@ -39,12 +39,12 @@ module BulkImports
::Gitlab::ImportExport::Config.new(config: import_export_yaml).to_h
end
def exportable_class
@exportable_class ||= exportable.class
def portable_class
@portable_class ||= portable.class
end
def exportable_class_sym
@exportable_class_sym ||= exportable_class.to_s.downcase.to_sym
def portable_class_sym
@portable_class_sym ||= portable_class.to_s.demodulize.underscore.to_sym
end
def import_export_yaml
......
# frozen_string_literal: true
module BulkImports
module Exports
module FileTransfer
class GroupConfig < BaseConfig
def base_export_path
exportable.full_path
portable.full_path
end
def import_export_yaml
......
# frozen_string_literal: true
module BulkImports
module Exports
module FileTransfer
class ProjectConfig < BaseConfig
def base_export_path
exportable.disk_path
portable.disk_path
end
def import_export_yaml
......
......@@ -2,14 +2,14 @@
module BulkImports
class ExportService
def initialize(exportable:, user:)
@exportable = exportable
def initialize(portable:, user:)
@portable = portable
@current_user = user
end
def execute
Export.config(exportable).exportable_relations.each do |relation|
RelationExportWorker.perform_async(current_user.id, exportable.id, exportable.class.name, relation)
FileTransfer.config_for(portable).portable_relations.each do |relation|
RelationExportWorker.perform_async(current_user.id, portable.id, portable.class.name, relation)
end
ServiceResponse.success
......@@ -22,6 +22,6 @@ module BulkImports
private
attr_reader :exportable, :current_user
attr_reader :portable, :current_user
end
end
......@@ -4,9 +4,9 @@ module BulkImports
class RelationExportService
include Gitlab::ImportExport::CommandLineUtil
def initialize(user, exportable, relation, jid)
def initialize(user, portable, relation, jid)
@user = user
@exportable = exportable
@portable = portable
@relation = relation
@jid = jid
end
......@@ -22,28 +22,28 @@ module BulkImports
private
attr_reader :user, :exportable, :relation, :jid
attr_reader :user, :portable, :relation, :jid
def find_or_create_export!
validate_user_permissions!
export = exportable.bulk_import_exports.safe_find_or_create_by!(relation: relation)
export = portable.bulk_import_exports.safe_find_or_create_by!(relation: relation)
export.update!(status_event: 'start', jid: jid)
yield export
export.update!(status_event: 'finish', error: nil)
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e, exportable_id: exportable.id, exportable_type: exportable.class.name)
Gitlab::ErrorTracking.track_exception(e, portable_id: portable.id, portable_type: portable.class.name)
export&.update(status_event: 'fail_op', error: e.class)
end
def validate_user_permissions!
ability = "admin_#{exportable.to_ability_name}"
ability = "admin_#{portable.to_ability_name}"
user.can?(ability, exportable) ||
raise(::Gitlab::ImportExport::Error.permission_error(user, exportable))
user.can?(ability, portable) ||
raise(::Gitlab::ImportExport::Error.permission_error(user, portable))
end
def remove_existing_export_file!(export)
......@@ -72,23 +72,23 @@ module BulkImports
upload.save!
end
def export_config
@export_config ||= Export.config(exportable)
def config
@config ||= FileTransfer.config_for(portable)
end
def export_path
@export_path ||= export_config.export_path
@export_path ||= config.export_path
end
def exportable_tree
@exportable_tree ||= export_config.exportable_tree
def portable_tree
@portable_tree ||= config.portable_tree
end
# rubocop: disable CodeReuse/Serializer
def serializer
@serializer ||= ::Gitlab::ImportExport::JSON::StreamingSerializer.new(
exportable,
exportable_tree,
portable,
portable_tree,
json_writer,
exportable_path: ''
)
......
......@@ -11,17 +11,17 @@ module BulkImports
tags :exclude_from_kubernetes
sidekiq_options status_expiration: StuckExportJobsWorker::EXPORT_JOBS_EXPIRATION
def perform(user_id, exportable_id, exportable_class, relation)
def perform(user_id, portable_id, portable_class, relation)
user = User.find(user_id)
exportable = exportable(exportable_id, exportable_class)
portable = portable(portable_id, portable_class)
RelationExportService.new(user, exportable, relation, jid).execute
RelationExportService.new(user, portable, relation, jid).execute
end
private
def exportable(exportable_id, exportable_class)
exportable_class.classify.constantize.find(exportable_id)
def portable(portable_id, portable_class)
portable_class.classify.constantize.find(portable_id)
end
end
end
......@@ -48,7 +48,7 @@ module API
detail 'This feature was introduced in GitLab 13.12'
end
post ':id/export_relations' do
response = ::BulkImports::ExportService.new(exportable: user_group, user: current_user).execute
response = ::BulkImports::ExportService.new(portable: user_group, user: current_user).execute
if response.success?
accepted!
......
......@@ -47,12 +47,12 @@ RSpec.describe BulkImports::Export, type: :model do
end
end
describe '#exportable' do
describe '#portable' do
context 'when associated with project' do
it 'returns project' do
export = create(:bulk_import_export, project: create(:project), group: nil)
expect(export.exportable).to be_instance_of(Project)
expect(export.portable).to be_instance_of(Project)
end
end
......@@ -60,7 +60,7 @@ RSpec.describe BulkImports::Export, type: :model do
it 'returns group' do
export = create(:bulk_import_export)
expect(export.exportable).to be_instance_of(Group)
expect(export.portable).to be_instance_of(Group)
end
end
end
......@@ -70,7 +70,7 @@ RSpec.describe BulkImports::Export, type: :model do
it 'returns project config' do
export = create(:bulk_import_export, project: create(:project), group: nil)
expect(export.config).to be_instance_of(BulkImports::Exports::ProjectConfig)
expect(export.config).to be_instance_of(BulkImports::FileTransfer::ProjectConfig)
end
end
......@@ -78,7 +78,7 @@ RSpec.describe BulkImports::Export, type: :model do
it 'returns group config' do
export = create(:bulk_import_export)
expect(export.config).to be_instance_of(BulkImports::Exports::GroupConfig)
expect(export.config).to be_instance_of(BulkImports::FileTransfer::GroupConfig)
end
end
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe BulkImports::Exports::GroupConfig do
RSpec.describe BulkImports::FileTransfer::GroupConfig do
let_it_be(:exportable) { create(:group) }
let_it_be(:hex) { '123' }
......@@ -18,7 +18,7 @@ RSpec.describe BulkImports::Exports::GroupConfig do
expect(finder).to receive(:find_root).with(:group).and_call_original
end
expect(subject.exportable_tree).not_to be_empty
expect(subject.portable_tree).not_to be_empty
end
end
......@@ -32,7 +32,7 @@ RSpec.describe BulkImports::Exports::GroupConfig do
describe '#exportable_relations' do
it 'returns a list of top level exportable relations' do
expect(subject.exportable_relations).to include('milestones', 'badges', 'boards', 'labels')
expect(subject.portable_relations).to include('milestones', 'badges', 'boards', 'labels')
end
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe BulkImports::Exports::ProjectConfig do
RSpec.describe BulkImports::FileTransfer::ProjectConfig do
let_it_be(:exportable) { create(:project) }
let_it_be(:hex) { '123' }
......@@ -18,7 +18,7 @@ RSpec.describe BulkImports::Exports::ProjectConfig do
expect(finder).to receive(:find_root).with(:project).and_call_original
end
expect(subject.exportable_tree).not_to be_empty
expect(subject.portable_tree).not_to be_empty
end
end
......@@ -32,7 +32,7 @@ RSpec.describe BulkImports::Exports::ProjectConfig do
describe '#exportable_relations' do
it 'returns a list of top level exportable relations' do
expect(subject.exportable_relations).to include('issues', 'labels', 'milestones', 'merge_requests')
expect(subject.portable_relations).to include('issues', 'labels', 'milestones', 'merge_requests')
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::FileTransfer do
describe '.config_for' do
context 'when portable is group' do
it 'returns group config' do
expect(described_class.config_for(build(:group))).to be_instance_of(BulkImports::FileTransfer::GroupConfig)
end
end
context 'when portable is project' do
it 'returns project config' do
expect(described_class.config_for(build(:project))).to be_instance_of(BulkImports::FileTransfer::ProjectConfig)
end
end
context 'when portable is unsupported' do
it 'raises an error' do
expect { described_class.config_for(nil) }.to raise_error(BulkImports::FileTransfer::UnsupportedObjectType)
end
end
end
end
......@@ -10,12 +10,12 @@ RSpec.describe BulkImports::ExportService do
group.add_owner(user)
end
subject { described_class.new(exportable: group, user: user) }
subject { described_class.new(portable: group, user: user) }
describe '#execute' do
it 'schedules RelationExportWorker for each top level relation' do
expect(subject).to receive(:execute).and_return(ServiceResponse.success).and_call_original
top_level_relations = BulkImports::Export.config(group).exportable_relations
top_level_relations = BulkImports::FileTransfer.config_for(group).portable_relations
top_level_relations.each do |relation|
expect(BulkImports::RelationExportWorker)
......@@ -28,7 +28,7 @@ RSpec.describe BulkImports::ExportService do
context 'when exception occurs' do
it 'does not schedule RelationExportWorker' do
service = described_class.new(exportable: nil, user: user)
service = described_class.new(portable: nil, user: user)
expect(service)
.to receive(:execute)
......
......@@ -77,7 +77,7 @@ RSpec.describe BulkImports::RelationExportService do
it 'tracks exception' do
expect(Gitlab::ErrorTracking)
.to receive(:track_exception)
.with(exception_class, exportable_id: group.id, exportable_type: group.class.name)
.with(exception_class, portable_id: group.id, portable_type: group.class.name)
.and_call_original
subject.execute
......
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