diff --git a/app/services/protected_branches/base_service.rb b/app/services/protected_branches/base_service.rb index 0ab46bf236cca03178bd75f0707afc9001d88afa..62d5e04b49957adf9d0ed808c7817844132f5460 100644 --- a/app/services/protected_branches/base_service.rb +++ b/app/services/protected_branches/base_service.rb @@ -18,9 +18,20 @@ def after_execute(*) def refresh_cache CacheService.new(@project_or_group, @current_user, @params).refresh + refresh_cache_for_groups_projects rescue StandardError => e Gitlab::ErrorTracking.track_exception(e) end + + private + + def refresh_cache_for_groups_projects + return unless @project_or_group.is_a?(Group) + + @project_or_group.all_projects.find_each do |project| + CacheService.new(project, @current_user, @params).refresh + end + end end end diff --git a/spec/services/protected_branches/create_service_spec.rb b/spec/services/protected_branches/create_service_spec.rb index 625aa4fa3773b4c8a181145848dd671aacbe790e..abfb73c147ec5e9867606db5c8b2782de00d4a0f 100644 --- a/spec/services/protected_branches/create_service_spec.rb +++ b/spec/services/protected_branches/create_service_spec.rb @@ -16,6 +16,8 @@ describe '#execute' do let(:name) { 'master' } + let(:group_cache_service_double) { instance_double(ProtectedBranches::CacheService) } + let(:project_cache_service_double) { instance_double(ProtectedBranches::CacheService) } it 'creates a new protected branch' do expect { service.execute }.to change(ProtectedBranch, :count).by(1) @@ -24,8 +26,12 @@ end it 'refreshes the cache' do - expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service| - expect(cache_service).to receive(:refresh) + expect(ProtectedBranches::CacheService).to receive(:new).with(entity, user, params).and_return(group_cache_service_double) + expect(group_cache_service_double).to receive(:refresh) + + if entity.is_a?(Group) + expect(ProtectedBranches::CacheService).to receive(:new).with(project, user, params).and_return(project_cache_service_double) + expect(project_cache_service_double).to receive(:refresh) end service.execute @@ -58,14 +64,14 @@ context 'with entity project' do let_it_be_with_reload(:entity) { create(:project) } - let(:user) { entity.first_owner } it_behaves_like 'execute with entity' end context 'with entity group' do - let_it_be_with_reload(:entity) { create(:group) } + let_it_be_with_reload(:project) { create(:project, :in_group) } + let_it_be_with_reload(:entity) { project.group } let_it_be_with_reload(:user) { create(:user) } before do