Commit a851f891 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'georgekoltsov/add-logging-to-import-export' into 'master'

Log top-level relation creation when Importing Project/Group

See merge request gitlab-org/gitlab!27605
parents ab399c2b 206dca11
...@@ -71,6 +71,7 @@ module Gitlab ...@@ -71,6 +71,7 @@ module Gitlab
import_failure_service.with_retry(action: 'relation_object.save!', relation_key: relation_key, relation_index: relation_index) do import_failure_service.with_retry(action: 'relation_object.save!', relation_key: relation_key, relation_index: relation_index) do
relation_object.save! relation_object.save!
log_relation_creation(@importable, relation_key, relation_object)
end end
rescue => e rescue => e
import_failure_service.log_import_failure( import_failure_service.log_import_failure(
...@@ -218,6 +219,25 @@ module Gitlab ...@@ -218,6 +219,25 @@ module Gitlab
relation_reader.sort_ci_pipelines_by_id relation_reader.sort_ci_pipelines_by_id
end end
# Enable logging of each top-level relation creation when Importing
# into a Group if feature flag is enabled
def log_relation_creation(importable, relation_key, relation_object)
root_ancestor_group = importable.try(:root_ancestor)
return unless root_ancestor_group
return unless root_ancestor_group.instance_of?(::Group)
return unless Feature.enabled?(:log_import_export_relation_creation, root_ancestor_group)
@shared.logger.info(
importable_type: importable.class.to_s,
importable_id: importable.id,
relation_key: relation_key,
relation_id: relation_object.id,
author_id: relation_object.try(:author_id),
message: '[Project/Group Import] Created new object relation'
)
end
end end
end end
end end
...@@ -60,6 +60,38 @@ describe Gitlab::ImportExport::RelationTreeRestorer do ...@@ -60,6 +60,38 @@ describe Gitlab::ImportExport::RelationTreeRestorer do
end end
end end
shared_examples 'logging of relations creation' do
context 'when log_import_export_relation_creation feature flag is enabled' do
before do
stub_feature_flags(log_import_export_relation_creation: { enabled: true, thing: group })
end
it 'logs top-level relation creation' do
expect(relation_tree_restorer.shared.logger)
.to receive(:info)
.with(hash_including(message: '[Project/Group Import] Created new object relation'))
.at_least(:once)
subject
end
end
context 'when log_import_export_relation_creation feature flag is disabled' do
before do
stub_feature_flags(log_import_export_relation_creation: { enabled: false, thing: group })
end
it 'does not log top-level relation creation' do
expect(relation_tree_restorer.shared.logger)
.to receive(:info)
.with(hash_including(message: '[Project/Group Import] Created new object relation'))
.never
subject
end
end
end
context 'when restoring a project' do context 'when restoring a project' do
let(:path) { 'spec/fixtures/lib/gitlab/import_export/complex/project.json' } let(:path) { 'spec/fixtures/lib/gitlab/import_export/complex/project.json' }
let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') } let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') }
...@@ -71,6 +103,30 @@ describe Gitlab::ImportExport::RelationTreeRestorer do ...@@ -71,6 +103,30 @@ describe Gitlab::ImportExport::RelationTreeRestorer do
let(:relation_reader) { Gitlab::ImportExport::JSON::LegacyReader::File.new(path, reader.project_relation_names) } let(:relation_reader) { Gitlab::ImportExport::JSON::LegacyReader::File.new(path, reader.project_relation_names) }
it_behaves_like 'import project successfully' it_behaves_like 'import project successfully'
context 'logging of relations creation' do
let(:group) { create(:group) }
let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project', group: group) }
include_examples 'logging of relations creation'
end
end end
end end
context 'when restoring a group' do
let(:path) { 'spec/fixtures/lib/gitlab/import_export/group_exports/no_children/group.json' }
let(:group) { create(:group) }
let(:importable) { create(:group, parent: group) }
let(:object_builder) { Gitlab::ImportExport::Group::ObjectBuilder }
let(:relation_factory) { Gitlab::ImportExport::Group::RelationFactory }
let(:relation_reader) { Gitlab::ImportExport::JSON::LegacyReader::File.new(path, reader.group_relation_names) }
let(:reader) do
Gitlab::ImportExport::Reader.new(
shared: shared,
config: Gitlab::ImportExport::Config.new(config: Gitlab::ImportExport.group_config_file).to_h
)
end
include_examples 'logging of relations creation'
end
end end
...@@ -15,6 +15,10 @@ describe Groups::ImportExport::ImportService do ...@@ -15,6 +15,10 @@ describe Groups::ImportExport::ImportService do
before do before do
ImportExportUpload.create(group: group, import_file: import_file) ImportExportUpload.create(group: group, import_file: import_file)
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
allow(import_logger).to receive(:error)
allow(import_logger).to receive(:info)
end end
context 'when user has correct permissions' do context 'when user has correct permissions' do
...@@ -29,13 +33,11 @@ describe Groups::ImportExport::ImportService do ...@@ -29,13 +33,11 @@ describe Groups::ImportExport::ImportService do
end end
it 'logs the import success' do it 'logs the import success' do
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
expect(import_logger).to receive(:info).with( expect(import_logger).to receive(:info).with(
group_id: group.id, group_id: group.id,
group_name: group.name, group_name: group.name,
message: 'Group Import/Export: Import succeeded' message: 'Group Import/Export: Import succeeded'
) ).once
subject subject
end end
...@@ -45,8 +47,6 @@ describe Groups::ImportExport::ImportService do ...@@ -45,8 +47,6 @@ describe Groups::ImportExport::ImportService do
let(:user) { create(:user) } let(:user) { create(:user) }
it 'logs the error and raises an exception' do it 'logs the error and raises an exception' do
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
expect(import_logger).to receive(:error).with( expect(import_logger).to receive(:error).with(
group_id: group.id, group_id: group.id,
group_name: group.name, group_name: group.name,
...@@ -71,16 +71,12 @@ describe Groups::ImportExport::ImportService do ...@@ -71,16 +71,12 @@ describe Groups::ImportExport::ImportService do
context 'when there are errors with the import file' do context 'when there are errors with the import file' do
let(:import_file) { fixture_file_upload('spec/fixtures/symlink_export.tar.gz') } let(:import_file) { fixture_file_upload('spec/fixtures/symlink_export.tar.gz') }
before do
allow(Gitlab::Import::Logger).to receive(:build).and_return(import_logger)
end
it 'logs the error and raises an exception' do it 'logs the error and raises an exception' do
expect(import_logger).to receive(:error).with( expect(import_logger).to receive(:error).with(
group_id: group.id, group_id: group.id,
group_name: group.name, group_name: group.name,
message: a_string_including('Errors occurred') message: a_string_including('Errors occurred')
) ).once
expect { subject }.to raise_error(Gitlab::ImportExport::Error) expect { subject }.to raise_error(Gitlab::ImportExport::Error)
end end
......
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