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
class DataList
def initialize(batch, data_fields_hash, klass)
@batch = batch
def initialize(batch_ids, data_fields_hash, klass)
@batch_ids = batch_ids
@data_fields_hash = data_fields_hash
@klass = klass
end
......@@ -13,13 +13,15 @@ class DataList
private
attr_reader :batch, :data_fields_hash, :klass
attr_reader :batch_ids, :data_fields_hash, :klass
def columns
data_fields_hash.keys << 'service_id'
end
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
......@@ -310,7 +310,7 @@ class Service < ApplicationRecord
end
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
def to_data_fields_hash
......
# frozen_string_literal: true
class ServiceList
def initialize(batch_ids, service_hash)
def initialize(batch_ids, service_hash, association)
@batch_ids = batch_ids
@service_hash = service_hash
@association = association
end
def to_array
......@@ -12,15 +13,15 @@ class ServiceList
private
attr_reader :batch_ids, :service_hash
attr_reader :batch_ids, :service_hash, :association
def columns
(service_hash.keys << 'project_id')
(service_hash.keys << "#{association}_id")
end
def values
batch_ids.map do |project_id|
(service_hash.values << project_id)
batch_ids.map do |id|
(service_hash.values << id)
end
end
end
......@@ -6,6 +6,8 @@ module Admin
def propagate
update_inherited_integrations
create_integration_for_groups_without_integration if Feature.enabled?(:group_level_integrations)
create_integration_for_projects_without_integration
end
......@@ -38,9 +40,33 @@ module Admin
end
# 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
@service_hash ||= integration.to_service_hash
.tap { |json| json['inherit_from_id'] = integration.id }
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
......@@ -26,14 +26,14 @@ module Admin
loop do
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
end
end
def bulk_create_from_integration(batch_ids)
service_list = ServiceList.new(batch_ids, service_hash).to_array
def bulk_create_from_integration(batch_ids, association)
service_list = ServiceList.new(batch_ids, service_hash, association).to_array
Service.transaction do
results = bulk_insert(*service_list)
......@@ -44,7 +44,7 @@ module Admin
bulk_insert(*data_list)
end
run_callbacks(batch_ids)
run_callbacks(batch_ids) if association == 'project'
end
end
......
......@@ -10,8 +10,9 @@ RSpec.describe Admin::PropagateIntegrationService do
stub_jira_service_test
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!(:group) { create(:group) }
let!(:instance_integration) do
JiraService.create!(
instance: true,
......@@ -109,6 +110,10 @@ RSpec.describe Admin::PropagateIntegrationService do
it_behaves_like 'inherits settings from integration' do
let(:integration) { project.jira_service }
end
it_behaves_like 'inherits settings from integration' do
let(:integration) { Service.find_by(group_id: group.id) }
end
end
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