Commit 2f8fa628 authored by Arturo Herrero's avatar Arturo Herrero

Extract PropagateService mixin

PropagateIntegrationService and PropagateServiceTemplate have a lot of
duplicated code that we can extract into a mixin.
parent fef90b07
......@@ -2,17 +2,7 @@
module Admin
class PropagateIntegrationService
BATCH_SIZE = 100
delegate :data_fields_present?, to: :integration
def self.propagate(integration)
new(integration).propagate
end
def initialize(integration)
@integration = integration
end
include PropagateService
def propagate
update_inherited_integrations
......@@ -21,8 +11,6 @@ module Admin
private
attr_reader :integration
# rubocop: disable Cop/InBatches
# rubocop: disable CodeReuse/ActiveRecord
def update_inherited_integrations
......@@ -50,61 +38,9 @@ module Admin
end
# rubocop: enable CodeReuse/ActiveRecord
def create_integration_for_projects_without_integration
loop do
batch_ids = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch_ids) 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
Service.transaction do
results = bulk_insert(*service_list)
if data_fields_present?
data_list = DataList.new(results, data_fields_hash, integration.data_fields.class).to_array
bulk_insert(*data_list)
end
run_callbacks(batch_ids)
end
end
def bulk_insert(klass, columns, values_array)
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
klass.insert_all(items_to_insert, returning: [:id])
end
# rubocop: disable CodeReuse/ActiveRecord
def run_callbacks(batch_ids)
if integration.issue_tracker?
Project.where(id: batch_ids).update_all(has_external_issue_tracker: true)
end
if active_external_wiki?
Project.where(id: batch_ids).update_all(has_external_wiki: true)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def active_external_wiki?
integration.type == 'ExternalWikiService'
end
def service_hash
@service_hash ||= integration.to_service_hash
.tap { |json| json['inherit_from_id'] = integration.id }
end
def data_fields_hash
@data_fields_hash ||= integration.to_data_fields_hash
end
end
end
......@@ -2,17 +2,7 @@
module Admin
class PropagateServiceTemplate
BATCH_SIZE = 100
delegate :data_fields_present?, to: :integration
def self.propagate(integration)
new(integration).propagate
end
def initialize(integration)
@integration = integration
end
include PropagateService
def propagate
return unless integration.active?
......@@ -22,62 +12,8 @@ module Admin
private
attr_reader :integration
def create_integration_for_projects_without_integration
loop do
batch_ids = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch_ids) 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
Service.transaction do
results = bulk_insert(*service_list)
if data_fields_present?
data_list = DataList.new(results, data_fields_hash, integration.data_fields.class).to_array
bulk_insert(*data_list)
end
run_callbacks(batch_ids)
end
end
def bulk_insert(klass, columns, values_array)
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
klass.insert_all(items_to_insert, returning: [:id])
end
def service_hash
@service_hash ||= integration.to_service_hash
end
def data_fields_hash
@data_fields_hash ||= integration.to_data_fields_hash
end
# rubocop: disable CodeReuse/ActiveRecord
def run_callbacks(batch_ids)
if integration.issue_tracker?
Project.where(id: batch_ids).update_all(has_external_issue_tracker: true)
end
if active_external_wiki?
Project.where(id: batch_ids).update_all(has_external_wiki: true)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def active_external_wiki?
integration.type == 'ExternalWikiService'
end
end
end
# frozen_string_literal: true
module Admin
module PropagateService
extend ActiveSupport::Concern
BATCH_SIZE = 100
delegate :data_fields_present?, to: :integration
class_methods do
def propagate(integration)
new(integration).propagate
end
end
def initialize(integration)
@integration = integration
end
private
attr_reader :integration
def create_integration_for_projects_without_integration
loop do
batch_ids = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch_ids) 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
Service.transaction do
results = bulk_insert(*service_list)
if data_fields_present?
data_list = DataList.new(results, data_fields_hash, integration.data_fields.class).to_array
bulk_insert(*data_list)
end
run_callbacks(batch_ids)
end
end
def bulk_insert(klass, columns, values_array)
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
klass.insert_all(items_to_insert, returning: [:id])
end
# rubocop: disable CodeReuse/ActiveRecord
def run_callbacks(batch_ids)
if integration.issue_tracker?
Project.where(id: batch_ids).update_all(has_external_issue_tracker: true)
end
if integration.type == 'ExternalWikiService'
Project.where(id: batch_ids).update_all(has_external_wiki: true)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def data_fields_hash
@data_fields_hash ||= integration.to_data_fields_hash
end
end
end
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