Commit fd0364b1 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '209831-propagate-group-level-integrations-for-instance-level' into 'master'

Propagate instance-level integration to groups

See merge request gitlab-org/gitlab!40717
parents 219327ac 4a6f6217
# frozen_string_literal: true # frozen_string_literal: true
class DataList class DataList
def initialize(batch, data_fields_hash, klass) def initialize(batch_ids, data_fields_hash, klass)
@batch = batch @batch_ids = batch_ids
@data_fields_hash = data_fields_hash @data_fields_hash = data_fields_hash
@klass = klass @klass = klass
end end
...@@ -13,13 +13,15 @@ class DataList ...@@ -13,13 +13,15 @@ class DataList
private private
attr_reader :batch, :data_fields_hash, :klass attr_reader :batch_ids, :data_fields_hash, :klass
def columns def columns
data_fields_hash.keys << 'service_id' data_fields_hash.keys << 'service_id'
end end
def values def values
batch.map { |row| data_fields_hash.values << row['id'] } batch_ids.map do |row|
data_fields_hash.values << row['id']
end
end end
end end
...@@ -310,7 +310,7 @@ class Service < ApplicationRecord ...@@ -310,7 +310,7 @@ class Service < ApplicationRecord
end end
def to_service_hash def to_service_hash
as_json(methods: :type, except: %w[id template instance project_id]) as_json(methods: :type, except: %w[id template instance project_id group_id])
end end
def to_data_fields_hash def to_data_fields_hash
......
# frozen_string_literal: true # frozen_string_literal: true
class ServiceList class ServiceList
def initialize(batch_ids, service_hash) def initialize(batch_ids, service_hash, association)
@batch_ids = batch_ids @batch_ids = batch_ids
@service_hash = service_hash @service_hash = service_hash
@association = association
end end
def to_array def to_array
...@@ -12,15 +13,15 @@ class ServiceList ...@@ -12,15 +13,15 @@ class ServiceList
private private
attr_reader :batch_ids, :service_hash attr_reader :batch_ids, :service_hash, :association
def columns def columns
(service_hash.keys << 'project_id') (service_hash.keys << "#{association}_id")
end end
def values def values
batch_ids.map do |project_id| batch_ids.map do |id|
(service_hash.values << project_id) (service_hash.values << id)
end end
end end
end end
...@@ -6,6 +6,8 @@ module Admin ...@@ -6,6 +6,8 @@ module Admin
def propagate def propagate
update_inherited_integrations update_inherited_integrations
create_integration_for_groups_without_integration if Feature.enabled?(:group_level_integrations)
create_integration_for_projects_without_integration create_integration_for_projects_without_integration
end end
...@@ -38,9 +40,33 @@ module Admin ...@@ -38,9 +40,33 @@ module Admin
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def create_integration_for_groups_without_integration
loop do
batch = Group.uncached { group_ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch, 'group') unless batch.empty?
break if batch.size < BATCH_SIZE
end
end
def service_hash def service_hash
@service_hash ||= integration.to_service_hash @service_hash ||= integration.to_service_hash
.tap { |json| json['inherit_from_id'] = integration.id } .tap { |json| json['inherit_from_id'] = integration.id }
end end
# rubocop:disable CodeReuse/ActiveRecord
def group_ids_without_integration(integration, limit)
services = Service
.select('1')
.where('services.group_id = namespaces.id')
.where(type: integration.type)
Group
.where('NOT EXISTS (?)', services)
.limit(limit)
.pluck(:id)
end
# rubocop:enable CodeReuse/ActiveRecord
end end
end end
...@@ -26,14 +26,14 @@ module Admin ...@@ -26,14 +26,14 @@ module Admin
loop do loop do
batch_ids = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) } batch_ids = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch_ids) unless batch_ids.empty? bulk_create_from_integration(batch_ids, 'project') unless batch_ids.empty?
break if batch_ids.size < BATCH_SIZE break if batch_ids.size < BATCH_SIZE
end end
end end
def bulk_create_from_integration(batch_ids) def bulk_create_from_integration(batch_ids, association)
service_list = ServiceList.new(batch_ids, service_hash).to_array service_list = ServiceList.new(batch_ids, service_hash, association).to_array
Service.transaction do Service.transaction do
results = bulk_insert(*service_list) results = bulk_insert(*service_list)
...@@ -44,7 +44,7 @@ module Admin ...@@ -44,7 +44,7 @@ module Admin
bulk_insert(*data_list) bulk_insert(*data_list)
end end
run_callbacks(batch_ids) run_callbacks(batch_ids) if association == 'project'
end end
end end
......
...@@ -10,8 +10,9 @@ RSpec.describe Admin::PropagateIntegrationService do ...@@ -10,8 +10,9 @@ RSpec.describe Admin::PropagateIntegrationService do
stub_jira_service_test stub_jira_service_test
end end
let(:excluded_attributes) { %w[id project_id inherit_from_id instance created_at updated_at default] } let(:excluded_attributes) { %w[id project_id group_id inherit_from_id instance created_at updated_at default] }
let!(:project) { create(:project) } let!(:project) { create(:project) }
let!(:group) { create(:group) }
let!(:instance_integration) do let!(:instance_integration) do
JiraService.create!( JiraService.create!(
instance: true, instance: true,
...@@ -109,6 +110,10 @@ RSpec.describe Admin::PropagateIntegrationService do ...@@ -109,6 +110,10 @@ RSpec.describe Admin::PropagateIntegrationService do
it_behaves_like 'inherits settings from integration' do it_behaves_like 'inherits settings from integration' do
let(:integration) { project.jira_service } let(:integration) { project.jira_service }
end end
it_behaves_like 'inherits settings from integration' do
let(:integration) { Service.find_by(group_id: group.id) }
end
end end
it 'updates project#has_external_issue_tracker for issue tracker services' do it 'updates project#has_external_issue_tracker for issue tracker services' do
......
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