2020-09-29 11:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkCreateIntegrationService
|
|
|
|
def initialize(integration, batch, association)
|
|
|
|
@integration = integration
|
|
|
|
@batch = batch
|
|
|
|
@association = association
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
service_list = ServiceList.new(batch, service_hash, association).to_array
|
|
|
|
|
|
|
|
Service.transaction do
|
2020-10-22 20:08:30 -04:00
|
|
|
run_callbacks(batch) if association == 'project'
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
results = bulk_insert(*service_list)
|
|
|
|
|
|
|
|
if integration.data_fields_present?
|
|
|
|
data_list = DataList.new(results, data_fields_hash, integration.data_fields.class).to_array
|
|
|
|
|
|
|
|
bulk_insert(*data_list)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :integration, :batch, :association
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-30 14:08:56 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-09-29 11:10:08 -04:00
|
|
|
def run_callbacks(batch)
|
2020-10-29 17:08:54 -04:00
|
|
|
if integration.external_issue_tracker?
|
2020-10-30 14:08:56 -04:00
|
|
|
Project.where(id: batch.select(:id)).update_all(has_external_issue_tracker: true)
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 17:08:54 -04:00
|
|
|
if integration.external_wiki?
|
2020-10-30 14:08:56 -04:00
|
|
|
Project.where(id: batch.select(:id)).update_all(has_external_wiki: true)
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-30 14:08:56 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-09-29 11:10:08 -04:00
|
|
|
|
|
|
|
def service_hash
|
|
|
|
if integration.template?
|
|
|
|
integration.to_service_hash
|
|
|
|
else
|
2020-10-23 14:08:31 -04:00
|
|
|
integration.to_service_hash.tap { |json| json['inherit_from_id'] = integration.inherit_from_id || integration.id }
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def data_fields_hash
|
|
|
|
integration.to_data_fields_hash
|
|
|
|
end
|
|
|
|
end
|