diff --git a/app/models/repository.rb b/app/models/repository.rb index e80c223840259cf7687313fb6c0ec6782dd13c8f..a8e826c9cbfea355af759254db5436644f060f82 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -364,6 +364,11 @@ class Repository expire_tag_count_cache end + def before_import + expire_emptiness_caches + expire_exists_cache + end + # Runs code after a repository has been forked/imported. def after_import expire_emptiness_caches diff --git a/app/services/projects/import_service.rb b/app/services/projects/import_service.rb index 2015897dd198e6aceea4a4db4a4a9da263ebee68..ef15ef6a47310c2afc4db4b4ced7436d7a9617aa 100644 --- a/app/services/projects/import_service.rb +++ b/app/services/projects/import_service.rb @@ -46,6 +46,8 @@ module Projects def import_data return unless has_importer? + project.repository.before_import + unless importer.execute raise Error, 'The remote data could not be imported.' end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index c5d5a1c2492e70feccfb33b340e162adcb09ec56..f517f325c03a57eeb88131c721de681385d5ed5c 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -612,6 +612,20 @@ describe Repository, models: true do end end + describe '#before_import' do + it 'flushes the emptiness cachess' do + expect(repository).to receive(:expire_emptiness_caches) + + repository.before_import + end + + it 'flushes the exists cache' do + expect(repository).to receive(:expire_exists_cache) + + repository.before_import + end + end + describe '#after_import' do it 'flushes the emptiness cachess' do expect(repository).to receive(:expire_emptiness_caches) diff --git a/spec/services/projects/import_service_spec.rb b/spec/services/projects/import_service_spec.rb index 04f474c736cf62aa1d1b8f6a1ea206b88af6b527..32bf3acf4836d102fae390bc660503f50d519cea 100644 --- a/spec/services/projects/import_service_spec.rb +++ b/spec/services/projects/import_service_spec.rb @@ -72,6 +72,23 @@ describe Projects::ImportService, services: true do expect(result[:status]).to eq :success end + it 'flushes various caches' do + expect_any_instance_of(Gitlab::Shell).to receive(:import_repository). + with(project.path_with_namespace, project.import_url). + and_return(true) + + expect_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute). + and_return(true) + + expect_any_instance_of(Repository).to receive(:expire_emptiness_caches). + and_call_original + + expect_any_instance_of(Repository).to receive(:expire_exists_cache). + and_call_original + + subject.execute + end + it 'fails if importer fails' do expect_any_instance_of(Gitlab::Shell).to receive(:import_repository).with(project.path_with_namespace, project.import_url).and_return(true) expect_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).and_return(false)