gitlab-org--gitlab-foss/lib/gitlab/import_export/saver.rb
Mayra Cabrera 0ab89d8e36 Add a rubocop for Rails.logger
Suggests to use a JSON structured log instead

Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/54102
2019-07-10 19:26:47 +00:00

67 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module ImportExport
class Saver
include Gitlab::ImportExport::CommandLineUtil
def self.save(*args)
new(*args).save
end
def initialize(project:, shared:)
@project = project
@shared = shared
end
def save
if compress_and_save
remove_export_path
Rails.logger.info("Saved project export #{archive_file}") # rubocop:disable Gitlab/RailsLogger
save_upload
else
@shared.error(Gitlab::ImportExport::Error.new(error_message))
false
end
rescue => e
@shared.error(e)
false
ensure
remove_archive
remove_export_path
end
private
def compress_and_save
tar_czf(archive: archive_file, dir: @shared.export_path)
end
def remove_export_path
FileUtils.rm_rf(@shared.export_path)
end
def remove_archive
FileUtils.rm_rf(@shared.archive_path)
end
def archive_file
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
end
def save_upload
upload = ImportExportUpload.find_or_initialize_by(project: @project)
File.open(archive_file) { |file| upload.export_file = file }
upload.save!
end
def error_message
"Unable to save #{archive_file} into #{@shared.export_path}."
end
end
end
end