2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-04 06:13:33 -04:00
|
|
|
module Projects
|
2020-02-11 13:08:58 -05:00
|
|
|
class PropagateServiceTemplate
|
2017-05-04 06:13:33 -04:00
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2020-04-22 11:09:27 -04:00
|
|
|
delegate :data_fields_present?, to: :template
|
|
|
|
|
|
|
|
def self.propagate(template)
|
|
|
|
new(template).propagate
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
2020-02-11 13:08:58 -05:00
|
|
|
def initialize(template)
|
|
|
|
@template = template
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
2017-05-04 12:11:28 -04:00
|
|
|
def propagate
|
2020-04-22 11:09:27 -04:00
|
|
|
return unless template.active?
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2020-02-11 13:08:58 -05:00
|
|
|
propagate_projects_with_template
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-22 11:09:27 -04:00
|
|
|
attr_reader :template
|
|
|
|
|
2020-02-11 13:08:58 -05:00
|
|
|
def propagate_projects_with_template
|
2017-05-04 06:13:33 -04:00
|
|
|
loop do
|
2020-05-26 23:08:26 -04:00
|
|
|
batch = Project.uncached { project_ids_without_integration }
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2020-02-11 13:08:58 -05:00
|
|
|
bulk_create_from_template(batch) unless batch.empty?
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2017-05-04 12:11:28 -04:00
|
|
|
break if batch.size < BATCH_SIZE
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-11 13:08:58 -05:00
|
|
|
def bulk_create_from_template(batch)
|
2020-05-20 14:08:00 -04:00
|
|
|
service_list = ServiceList.new(batch, service_hash).to_array
|
2017-05-05 03:40:44 -04:00
|
|
|
|
2017-05-05 10:16:02 -04:00
|
|
|
Project.transaction do
|
2020-05-20 14:08:00 -04:00
|
|
|
results = bulk_insert(*service_list)
|
2020-04-22 11:09:27 -04:00
|
|
|
|
|
|
|
if data_fields_present?
|
2020-05-20 14:08:00 -04:00
|
|
|
data_list = DataList.new(results, data_fields_hash, template.data_fields.class).to_array
|
2020-04-22 11:09:27 -04:00
|
|
|
|
2020-05-20 14:08:00 -04:00
|
|
|
bulk_insert(*data_list)
|
2020-04-22 11:09:27 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 10:16:02 -04:00
|
|
|
run_callbacks(batch)
|
|
|
|
end
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
2020-05-26 23:08:26 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def project_ids_without_integration
|
|
|
|
services = Service
|
|
|
|
.select('1')
|
|
|
|
.where('services.project_id = projects.id')
|
|
|
|
.where(type: template.type)
|
|
|
|
|
|
|
|
Project
|
|
|
|
.where('NOT EXISTS (?)', services)
|
|
|
|
.where(pending_delete: false)
|
|
|
|
.where(archived: false)
|
|
|
|
.limit(BATCH_SIZE)
|
|
|
|
.pluck(:id)
|
2017-05-05 03:40:44 -04:00
|
|
|
end
|
2020-05-26 23:08:26 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-05 03:40:44 -04:00
|
|
|
|
2020-04-22 11:09:27 -04:00
|
|
|
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])
|
2017-05-05 12:01:33 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 03:40:44 -04:00
|
|
|
def service_hash
|
2020-05-20 14:08:00 -04:00
|
|
|
@service_hash ||= template.to_service_hash
|
2020-04-22 11:09:27 -04:00
|
|
|
end
|
2017-05-05 08:43:22 -04:00
|
|
|
|
2020-05-20 14:08:00 -04:00
|
|
|
def data_fields_hash
|
|
|
|
@data_fields_hash ||= template.to_data_fields_hash
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
2017-05-05 10:16:02 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-05 10:16:02 -04:00
|
|
|
def run_callbacks(batch)
|
|
|
|
if active_external_issue_tracker?
|
|
|
|
Project.where(id: batch).update_all(has_external_issue_tracker: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
if active_external_wiki?
|
|
|
|
Project.where(id: batch).update_all(has_external_wiki: true)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-05 10:16:02 -04:00
|
|
|
|
|
|
|
def active_external_issue_tracker?
|
2020-04-22 11:09:27 -04:00
|
|
|
template.issue_tracker? && !template.default
|
2017-05-05 10:16:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_external_wiki?
|
2020-04-22 11:09:27 -04:00
|
|
|
template.type == 'ExternalWikiService'
|
2017-05-05 10:16:02 -04:00
|
|
|
end
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
end
|