2019-11-11 21:06:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Groups
|
|
|
|
module ImportExport
|
|
|
|
class ExportService
|
|
|
|
def initialize(group:, user:, params: {})
|
2020-05-25 15:07:58 +00:00
|
|
|
@group = group
|
2019-11-11 21:06:20 +00:00
|
|
|
@current_user = user
|
2020-05-25 15:07:58 +00:00
|
|
|
@params = params
|
|
|
|
@shared = @params[:shared] || Gitlab::ImportExport::Shared.new(@group)
|
|
|
|
@logger = Gitlab::Export::Logger.build
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
2020-04-09 12:09:24 +00:00
|
|
|
def async_execute
|
2021-01-28 15:09:06 +00:00
|
|
|
GroupExportWorker.perform_async(current_user.id, group.id, params)
|
2020-04-09 12:09:24 +00:00
|
|
|
end
|
|
|
|
|
2019-11-11 21:06:20 +00:00
|
|
|
def execute
|
2020-03-05 18:08:19 +00:00
|
|
|
validate_user_permissions
|
2020-01-23 21:08:35 +00:00
|
|
|
|
2021-01-28 15:09:06 +00:00
|
|
|
remove_existing_export! if group.export_file_exists?
|
2020-04-09 12:09:24 +00:00
|
|
|
|
2019-11-11 21:06:20 +00:00
|
|
|
save!
|
2020-02-26 18:09:24 +00:00
|
|
|
ensure
|
2021-01-26 12:09:27 +00:00
|
|
|
remove_archive_tmp_dir
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-28 15:09:06 +00:00
|
|
|
attr_reader :group, :current_user, :params
|
2019-11-11 21:06:20 +00:00
|
|
|
attr_accessor :shared
|
|
|
|
|
2020-03-05 18:08:19 +00:00
|
|
|
def validate_user_permissions
|
2021-01-28 15:09:06 +00:00
|
|
|
unless current_user.can?(:admin_group, group)
|
|
|
|
shared.error(::Gitlab::ImportExport::Error.permission_error(current_user, group))
|
2020-03-05 18:08:19 +00:00
|
|
|
|
|
|
|
notify_error!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 12:09:24 +00:00
|
|
|
def remove_existing_export!
|
2021-01-28 15:09:06 +00:00
|
|
|
import_export_upload = group.import_export_upload
|
2020-04-09 12:09:24 +00:00
|
|
|
|
|
|
|
import_export_upload.remove_export_file!
|
|
|
|
import_export_upload.save
|
|
|
|
end
|
|
|
|
|
2019-11-11 21:06:20 +00:00
|
|
|
def save!
|
2021-01-28 15:09:06 +00:00
|
|
|
# We cannot include the file_saver with the other savers because
|
|
|
|
# it removes the tmp dir. This means that if we want to add new savers
|
|
|
|
# in EE the data won't be available.
|
|
|
|
if savers.all?(&:save) && file_saver.save
|
2019-11-11 21:06:20 +00:00
|
|
|
notify_success
|
|
|
|
else
|
2020-02-26 18:09:24 +00:00
|
|
|
notify_error!
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def savers
|
2021-01-28 15:09:06 +00:00
|
|
|
[version_saver, tree_exporter]
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tree_exporter
|
2020-04-23 21:09:31 +00:00
|
|
|
tree_exporter_class.new(
|
2021-01-28 15:09:06 +00:00
|
|
|
group: group,
|
|
|
|
current_user: current_user,
|
|
|
|
shared: shared,
|
|
|
|
params: params
|
2020-04-15 15:09:17 +00:00
|
|
|
)
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
2020-04-23 21:09:31 +00:00
|
|
|
def tree_exporter_class
|
2021-01-28 15:09:06 +00:00
|
|
|
if ndjson?
|
2020-04-23 21:09:31 +00:00
|
|
|
Gitlab::ImportExport::Group::TreeSaver
|
|
|
|
else
|
|
|
|
Gitlab::ImportExport::Group::LegacyTreeSaver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 15:09:06 +00:00
|
|
|
def ndjson?
|
|
|
|
::Feature.enabled?(:group_export_ndjson, group&.parent, default_enabled: :yaml)
|
|
|
|
end
|
|
|
|
|
2020-05-13 12:07:54 +00:00
|
|
|
def version_saver
|
|
|
|
Gitlab::ImportExport::VersionSaver.new(shared: shared)
|
|
|
|
end
|
|
|
|
|
2019-11-11 21:06:20 +00:00
|
|
|
def file_saver
|
2021-01-28 15:09:06 +00:00
|
|
|
Gitlab::ImportExport::Saver.new(exportable: group, shared: shared)
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
2021-01-26 12:09:27 +00:00
|
|
|
def remove_archive_tmp_dir
|
|
|
|
FileUtils.rm_rf(shared.archive_path) if shared&.archive_path
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 18:09:24 +00:00
|
|
|
def notify_error!
|
|
|
|
notify_error
|
2019-11-11 21:06:20 +00:00
|
|
|
|
2021-05-04 15:10:36 +00:00
|
|
|
raise Gitlab::ImportExport::Error, shared.errors.to_sentence
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_success
|
2020-05-25 15:07:58 +00:00
|
|
|
@logger.info(
|
|
|
|
message: 'Group Export succeeded',
|
2021-01-28 15:09:06 +00:00
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name
|
2019-11-11 21:06:20 +00:00
|
|
|
)
|
2020-05-14 03:07:52 +00:00
|
|
|
|
2021-01-28 15:09:06 +00:00
|
|
|
notification_service.group_was_exported(group, current_user)
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_error
|
2020-05-25 15:07:58 +00:00
|
|
|
@logger.error(
|
|
|
|
message: 'Group Export failed',
|
2021-01-28 15:09:06 +00:00
|
|
|
group_id: group.id,
|
|
|
|
group_name: group.name,
|
|
|
|
errors: shared.errors.join(', ')
|
2019-11-11 21:06:20 +00:00
|
|
|
)
|
2020-05-14 03:07:52 +00:00
|
|
|
|
2021-01-28 15:09:06 +00:00
|
|
|
notification_service.group_was_not_exported(group, current_user, shared.errors)
|
2020-05-14 03:07:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notification_service
|
|
|
|
@notification_service ||= NotificationService.new
|
2019-11-11 21:06:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-03 18:09:25 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Groups::ImportExport::ExportService.prepend_mod_with('Groups::ImportExport::ExportService')
|