diff --git a/app/workers/projects/import_export/relation_import_worker.rb b/app/workers/projects/import_export/relation_import_worker.rb index 1943d68cfa476bda5b3c01dd133755cf66301e4d..3132def0f9bc208b14e3adc88da2431867278986 100644 --- a/app/workers/projects/import_export/relation_import_worker.rb +++ b/app/workers/projects/import_export/relation_import_worker.rb @@ -96,7 +96,7 @@ def process_import skip_on_duplicate_iid: true ) - tree_restorer.restore_single_relation(tracker.relation) + tree_restorer.restore_single_relation(tracker.relation, extra_track_scope: { tracker_id: tracker.id }) end def relation_reader diff --git a/lib/gitlab/import_export/group/relation_tree_restorer.rb b/lib/gitlab/import_export/group/relation_tree_restorer.rb index 9d5f4343cce77e4e83a7c60633ffdcf55bfd5e3b..be1e679f2b59396ebaffb5c15a8a83f805952211 100644 --- a/lib/gitlab/import_export/group/relation_tree_restorer.rb +++ b/lib/gitlab/import_export/group/relation_tree_restorer.rb @@ -41,7 +41,7 @@ def restore end end - def restore_single_relation(relation_key) + def restore_single_relation(relation_key, extra_track_scope: {}) # NO-OP. This is currently only available for file-based project import. end @@ -89,9 +89,9 @@ def create_relations! end end - def process_relation!(relation_key, relation_definition) + def process_relation!(relation_key, relation_definition, extra_track_scope: {}) @relation_reader.consume_relation(@importable_path, relation_key).each do |data_hash, relation_index| - with_progress_tracking(**progress_tracking_options(relation_key, relation_index)) do + with_progress_tracking(**progress_tracking_options(relation_key, relation_index, extra_track_scope)) do process_relation_item!(relation_key, relation_definition, relation_index, data_hash) end end @@ -372,11 +372,12 @@ def persist_relation(attributes) @relation_factory.create(**attributes) end - def progress_tracking_options(relation_key, relation_index) + def progress_tracking_options(relation_key, relation_index, extra_track_scope) { scope: { "#{importable_class_sym}_id" => @importable.id, - 'relation_key' => relation_key + 'relation_key' => relation_key, + **extra_track_scope }, data: relation_index } diff --git a/lib/gitlab/import_export/project/relation_tree_restorer.rb b/lib/gitlab/import_export/project/relation_tree_restorer.rb index 430c029802852fa097df92c07268bdcbbe1c0799..ab984aa915d6a46fdef87263a51a0f2b06a74c35 100644 --- a/lib/gitlab/import_export/project/relation_tree_restorer.rb +++ b/lib/gitlab/import_export/project/relation_tree_restorer.rb @@ -7,9 +7,9 @@ class RelationTreeRestorer < ImportExport::Group::RelationTreeRestorer # Relations which cannot be saved at project level (and have a group assigned) GROUP_MODELS = [GroupLabel, Milestone, Epic].freeze - def restore_single_relation(relation_key) + def restore_single_relation(relation_key, extra_track_scope: {}) bulk_insert_without_cache_or_touch do - process_relation!(relation_key, relations[relation_key]) + process_relation!(relation_key, relations[relation_key], extra_track_scope: extra_track_scope) end end diff --git a/spec/lib/gitlab/import_export/project/relation_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project/relation_tree_restorer_spec.rb index 962fd789dfee23d1406d40c02a3c9fe0e130c666..0856e4542bd30c2136cfadc44529460adb42ad0f 100644 --- a/spec/lib/gitlab/import_export/project/relation_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project/relation_tree_restorer_spec.rb @@ -9,7 +9,7 @@ require 'spec_helper' -RSpec.describe Gitlab::ImportExport::Project::RelationTreeRestorer, feature_category: :importers do +RSpec.describe Gitlab::ImportExport::Project::RelationTreeRestorer, :clean_gitlab_redis_shared_state, feature_category: :importers do let_it_be(:importable, reload: true) do create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') end @@ -154,6 +154,55 @@ end end + describe 'progress tracking' do + let(:relation_key) { 'issues' } + let(:skip_on_duplicate_iid) { true } + + before do + allow(relation_reader) + .to receive(:consume_relation) + .with(importable_name, 'issues') + .and_return([[build(:issue, iid: 123, title: 'Issue', author_id: user.id), 0]]) + end + + it 'tracks processed entry' do + expect(relation_tree_restorer).to receive(:process_relation_item!).once + + restore_relations + restore_relations + + expect( + relation_tree_restorer.processed_entry?( + scope: { + 'project_id' => importable.id, + 'relation_key' => 'issues' + }, + data: 0 + ) + ).to be(true) + end + + context 'when extra tracking scope is provided' do + it 'tracks processed entry' do + expect(relation_tree_restorer).to receive(:process_relation_item!).once + + relation_tree_restorer.restore_single_relation(relation_key, extra_track_scope: { tracker_id: 1 }) + relation_tree_restorer.restore_single_relation(relation_key, extra_track_scope: { tracker_id: 1 }) + + expect( + relation_tree_restorer.processed_entry?( + scope: { + 'project_id' => importable.id, + 'relation_key' => 'issues', + 'tracker_id' => 1 + }, + data: 0 + ) + ).to be(true) + end + end + end + context 'when importing issues' do let(:relation_key) { 'issues' } diff --git a/spec/workers/projects/import_export/relation_import_worker_spec.rb b/spec/workers/projects/import_export/relation_import_worker_spec.rb index 0d443037489646896396a38e52f78e4e40e0ac94..3604bc14b8ddeae9796b02246396fc4dc66db9e1 100644 --- a/spec/workers/projects/import_export/relation_import_worker_spec.rb +++ b/spec/workers/projects/import_export/relation_import_worker_spec.rb @@ -22,7 +22,8 @@ context 'when the import succeeds' do it 'schedules the relation restoration' do expect_next_instance_of(Gitlab::ImportExport::Project::RelationTreeRestorer) do |restorer| - expect(restorer).to receive(:restore_single_relation).with(tracker.relation) + expect(restorer).to receive(:restore_single_relation).with(tracker.relation, + extra_track_scope: { tracker_id: tracker.id }) end perform