Skip to content
Snippets Groups Projects
Commit 0c3f1214 authored by GitLab Bot's avatar GitLab Bot
Browse files

Add latest changes from gitlab-org/gitlab@master

parent ff67e3ed
No related branches found
No related tags found
No related merge requests found
Showing
with 353 additions and 27 deletions
Loading
Loading
@@ -9,16 +9,16 @@ module Gitlab
new(*args).save
end
 
def initialize(project:, shared:)
@project = project
@shared = shared
def initialize(exportable:, shared:)
@exportable = exportable
@shared = shared
end
 
def save
if compress_and_save
remove_export_path
 
Rails.logger.info("Saved project export #{archive_file}") # rubocop:disable Gitlab/RailsLogger
Rails.logger.info("Saved #{@exportable.class} export #{archive_file}") # rubocop:disable Gitlab/RailsLogger
 
save_upload
else
Loading
Loading
@@ -48,11 +48,11 @@ module Gitlab
end
 
def archive_file
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @exportable))
end
 
def save_upload
upload = ImportExportUpload.find_or_initialize_by(project: @project)
upload = initialize_upload
 
File.open(archive_file) { |file| upload.export_file = file }
 
Loading
Loading
@@ -62,6 +62,12 @@ module Gitlab
def error_message
"Unable to save #{archive_file} into #{@shared.export_path}."
end
def initialize_upload
exportable_kind = @exportable.class.name.downcase
ImportExportUpload.find_or_initialize_by(Hash[exportable_kind, @exportable])
end
end
end
end
Loading
Loading
@@ -23,21 +23,21 @@
module Gitlab
module ImportExport
class Shared
attr_reader :errors, :project
attr_reader :errors, :exportable, :logger
 
LOCKS_DIRECTORY = 'locks'
 
def initialize(project)
@project = project
@errors = []
@logger = Gitlab::Import::Logger.build
def initialize(exportable)
@exportable = exportable
@errors = []
@logger = Gitlab::Import::Logger.build
end
 
def active_export_count
Dir[File.join(base_path, '*')].count { |name| File.basename(name) != LOCKS_DIRECTORY && File.directory?(name) }
end
 
# The path where the project metadata and repository bundle is saved
# The path where the exportable metadata and repository bundle (in case of project) is saved
def export_path
@export_path ||= Gitlab::ImportExport.export_path(relative_path: relative_path)
end
Loading
Loading
@@ -84,11 +84,18 @@ module Gitlab
end
 
def relative_archive_path
@relative_archive_path ||= File.join(@project.disk_path, SecureRandom.hex)
@relative_archive_path ||= File.join(relative_base_path, SecureRandom.hex)
end
 
def relative_base_path
@project.disk_path
case exportable_type
when 'Project'
@exportable.disk_path
when 'Group'
@exportable.full_path
else
raise Gitlab::ImportExport::Error.new("Unsupported Exportable Type #{@exportable&.class}")
end
end
 
def log_error(details)
Loading
Loading
@@ -100,17 +107,24 @@ module Gitlab
end
 
def log_base_data
{
importer: 'Import/Export',
import_jid: @project&.import_state&.jid,
project_id: @project&.id,
project_path: @project&.full_path
log = {
importer: 'Import/Export',
exportable_id: @exportable&.id,
exportable_path: @exportable&.full_path
}
log[:import_jid] = @exportable&.import_state&.jid if exportable_type == 'Project'
log
end
 
def filtered_error_message(message)
Projects::ImportErrorFilter.filter_message(message)
end
def exportable_type
@exportable.class.name
end
end
end
end
Loading
Loading
@@ -234,6 +234,19 @@ describe TodosFinder do
end
end
 
describe '.todo_types' do
it 'returns the expected types' do
expected_result =
if Gitlab.ee?
%w[Epic Issue MergeRequest]
else
%w[Issue MergeRequest]
end
expect(described_class.todo_types).to contain_exactly(*expected_result)
end
end
describe '#any_for_target?' do
it 'returns true if there are any todos for the given target' do
todo = create(:todo, :pending)
Loading
Loading
File added
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::ImportExport::GroupTreeSaver do
describe 'saves the group tree into a json object' do
let(:shared) { Gitlab::ImportExport::Shared.new(group) }
let(:group_tree_saver) { described_class.new(group: group, current_user: user, shared: shared) }
let(:export_path) { "#{Dir.tmpdir}/group_tree_saver_spec" }
let(:user) { create(:user) }
let!(:group) { setup_group }
before do
group.add_maintainer(user)
allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
end
after do
FileUtils.rm_rf(export_path)
end
it 'saves group successfully' do
expect(group_tree_saver.save).to be true
end
context ':export_fast_serialize feature flag checks' do
before do
expect(Gitlab::ImportExport::Reader).to receive(:new).with(shared: shared, config: group_config).and_return(reader)
expect(reader).to receive(:group_tree).and_return(group_tree)
end
let(:reader) { instance_double('Gitlab::ImportExport::Reader') }
let(:group_config) { Gitlab::ImportExport::Config.new(config: Gitlab::ImportExport.group_config_file).to_h }
let(:group_tree) do
{
include: [{ milestones: { include: [] } }],
preload: { milestones: nil }
}
end
context 'when :export_fast_serialize feature is enabled' do
before do
stub_feature_flags(export_fast_serialize: true)
end
it 'uses FastHashSerializer' do
expect_any_instance_of(Gitlab::ImportExport::FastHashSerializer).to receive(:execute).and_call_original
group_tree_saver.save
end
end
context 'when :export_fast_serialize feature is disabled' do
before do
stub_feature_flags(export_fast_serialize: false)
end
it 'is serialized via built-in `as_json`' do
expect(group).to receive(:as_json).with(group_tree).and_call_original
group_tree_saver.save
end
end
end
# It is mostly duplicated in
# `spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb`
# except:
# context 'with description override' do
# context 'group members' do
# ^ These are specific for the groupTreeSaver
context 'JSON' do
let(:saved_group_json) do
group_tree_saver.save
group_json(group_tree_saver.full_path)
end
it 'saves the correct json' do
expect(saved_group_json).to include({ 'description' => 'description', 'visibility_level' => 20 })
end
it 'has milestones' do
expect(saved_group_json['milestones']).not_to be_empty
end
it 'has labels' do
expect(saved_group_json['labels']).not_to be_empty
end
it 'has boards' do
expect(saved_group_json['boards']).not_to be_empty
end
it 'has group members' do
expect(saved_group_json['members']).not_to be_empty
end
it 'has priorities associated to labels' do
expect(saved_group_json['labels'].first['priorities']).not_to be_empty
end
it 'has badges' do
expect(saved_group_json['badges']).not_to be_empty
end
context 'group members' do
let(:user2) { create(:user, email: 'group@member.com') }
let(:member_emails) do
saved_group_json['members'].map do |pm|
pm['user']['email']
end
end
before do
group.add_developer(user2)
end
it 'exports group members as group owner' do
group.add_owner(user)
expect(member_emails).to include('group@member.com')
end
context 'as admin' do
let(:user) { create(:admin) }
it 'exports group members as admin' do
expect(member_emails).to include('group@member.com')
end
it 'exports group members' do
member_types = saved_group_json['members'].map { |pm| pm['source_type'] }
expect(member_types).to all(eq('Namespace'))
end
end
end
context 'group attributes' do
it 'does not contain the runners token' do
expect(saved_group_json).not_to include("runners_token" => 'token')
end
end
end
end
def setup_group
group = create(:group, description: 'description')
create(:milestone, group: group)
create(:group_badge, group: group)
group_label = create(:group_label, group: group)
create(:label_priority, label: group_label, priority: 1)
create(:board, group: group)
create(:group_badge, group: group)
group
end
def group_json(filename)
JSON.parse(IO.read(filename))
end
end
Loading
Loading
@@ -6,17 +6,17 @@ describe Gitlab::ImportExport do
let(:project) { create(:project, :public, path: 'project-path', namespace: group) }
 
it 'contains the project path' do
expect(described_class.export_filename(project: project)).to include(project.path)
expect(described_class.export_filename(exportable: project)).to include(project.path)
end
 
it 'contains the namespace path' do
expect(described_class.export_filename(project: project)).to include(project.namespace.full_path.tr('/', '_'))
expect(described_class.export_filename(exportable: project)).to include(project.namespace.full_path.tr('/', '_'))
end
 
it 'does not go over a certain length' do
project.path = 'a' * 100
 
expect(described_class.export_filename(project: project).length).to be < 70
expect(described_class.export_filename(exportable: project).length).to be < 70
end
end
end
Loading
Loading
@@ -96,15 +96,20 @@ describe Gitlab::ImportExport::RelationRenameService do
let(:export_content_path) { project_tree_saver.full_path }
let(:export_content_hash) { ActiveSupport::JSON.decode(File.read(export_content_path)) }
let(:injected_hash) { renames.values.product([{}]).to_h }
let(:relation_tree_saver) { Gitlab::ImportExport::RelationTreeSaver.new }
 
let(:project_tree_saver) do
Gitlab::ImportExport::ProjectTreeSaver.new(
project: project, current_user: user, shared: shared)
end
 
before do
allow(project_tree_saver).to receive(:tree_saver).and_return(relation_tree_saver)
end
it 'adds old relationships to the exported file' do
# we inject relations with new names that should be rewritten
expect(project_tree_saver).to receive(:serialize_project_tree).and_wrap_original do |method, *args|
expect(relation_tree_saver).to receive(:serialize).and_wrap_original do |method, *args|
method.call(*args).merge(injected_hash)
end
 
Loading
Loading
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::ImportExport::RelationTreeSaver do
let(:exportable) { create(:group) }
let(:relation_tree_saver) { described_class.new }
let(:tree) { {} }
describe '#serialize' do
context 'when :export_fast_serialize feature is enabled' do
let(:serializer) { instance_double(Gitlab::ImportExport::FastHashSerializer) }
before do
stub_feature_flags(export_fast_serialize: true)
end
it 'uses FastHashSerializer' do
expect(Gitlab::ImportExport::FastHashSerializer)
.to receive(:new)
.with(exportable, tree)
.and_return(serializer)
expect(serializer).to receive(:execute)
relation_tree_saver.serialize(exportable, tree)
end
end
context 'when :export_fast_serialize feature is disabled' do
before do
stub_feature_flags(export_fast_serialize: false)
end
it 'is serialized via built-in `as_json`' do
expect(exportable).to receive(:as_json).with(tree)
relation_tree_saver.serialize(exportable, tree)
end
end
end
end
Loading
Loading
@@ -5,7 +5,7 @@ describe Gitlab::ImportExport::Saver do
let!(:project) { create(:project, :public, name: 'project') }
let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
let(:shared) { project.import_export_shared }
subject { described_class.new(project: project, shared: shared) }
subject { described_class.new(exportable: project, shared: shared) }
 
before do
allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
Loading
Loading
Loading
Loading
@@ -7,7 +7,7 @@ describe Gitlab::ImportExport::Shared do
 
context 'with a repository on disk' do
let(:project) { create(:project, :repository) }
let(:base_path) { %(/tmp/project_exports/#{project.disk_path}/) }
let(:base_path) { %(/tmp/gitlab_exports/#{project.disk_path}/) }
 
describe '#archive_path' do
it 'uses a random hash to avoid conflicts' do
Loading
Loading
# frozen_string_literal: true
require 'spec_helper'
describe Groups::ImportExport::ExportService do
describe '#execute' do
let!(:user) { create(:user) }
let(:group) { create(:group) }
let(:shared) { Gitlab::ImportExport::Shared.new(group) }
let(:export_path) { shared.export_path }
let(:service) { described_class.new(group: group, user: user, params: { shared: shared }) }
after do
FileUtils.rm_rf(export_path)
end
it 'saves the models' do
expect(Gitlab::ImportExport::GroupTreeSaver).to receive(:new).and_call_original
service.execute
end
context 'when saver succeeds' do
it 'saves the group in the file system' do
service.execute
expect(group.import_export_upload.export_file.file).not_to be_nil
expect(File.directory?(export_path)).to eq(false)
expect(File.exist?(shared.archive_path)).to eq(false)
end
end
context 'when saving services fail' do
before do
allow(service).to receive_message_chain(:tree_exporter, :save).and_return(false)
end
it 'removes the remaining exported data' do
allow_any_instance_of(Gitlab::ImportExport::Saver).to receive(:compress_and_save).and_return(false)
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
expect(group.import_export_upload).to be_nil
expect(File.directory?(export_path)).to eq(false)
expect(File.exist?(shared.archive_path)).to eq(false)
end
it 'notifies logger' do
expect_any_instance_of(Gitlab::Import::Logger).to receive(:error)
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error)
end
end
end
end
Loading
Loading
@@ -6,7 +6,7 @@ describe ImportExportCleanUpService do
describe '#execute' do
let(:service) { described_class.new }
 
let(:tmp_import_export_folder) { 'tmp/project_exports' }
let(:tmp_import_export_folder) { 'tmp/gitlab_exports' }
 
context 'when the import/export directory does not exist' do
it 'does not remove any archives' do
Loading
Loading
Loading
Loading
@@ -66,7 +66,7 @@ describe Projects::ImportExport::ExportService do
end
 
it 'saves the project in the file system' do
expect(Gitlab::ImportExport::Saver).to receive(:save).with(project: project, shared: shared)
expect(Gitlab::ImportExport::Saver).to receive(:save).with(exportable: project, shared: shared)
 
service.execute
end
Loading
Loading
# frozen_string_literal: true
require 'spec_helper'
describe GroupExportWorker do
let!(:user) { create(:user) }
let!(:group) { create(:group) }
subject { described_class.new }
describe '#perform' do
context 'when it succeeds' do
it 'calls the ExportService' do
expect_any_instance_of(::Groups::ImportExport::ExportService).to receive(:execute)
subject.perform(user.id, group.id, {})
end
end
context 'when it fails' do
it 'raises an exception when params are invalid' do
expect_any_instance_of(::Groups::ImportExport::ExportService).not_to receive(:execute)
expect { subject.perform(1234, group.id, {}) }.to raise_exception(ActiveRecord::RecordNotFound)
expect { subject.perform(user.id, 1234, {}) }.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment