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