2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Projects
|
|
|
|
class PropagateServiceTemplate
|
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
delegate :data_fields_present?, to: :template
|
|
|
|
|
|
|
|
def self.propagate(template)
|
|
|
|
new(template).propagate
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(template)
|
|
|
|
@template = template
|
|
|
|
end
|
|
|
|
|
|
|
|
def propagate
|
2020-05-24 23:13:21 +05:30
|
|
|
return unless template.active?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
propagate_projects_with_template
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
attr_reader :template
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def propagate_projects_with_template
|
|
|
|
loop do
|
2020-06-23 00:09:42 +05:30
|
|
|
batch = Project.uncached { project_ids_without_integration }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
bulk_create_from_template(batch) unless batch.empty?
|
|
|
|
|
|
|
|
break if batch.size < BATCH_SIZE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_create_from_template(batch)
|
2020-06-23 00:09:42 +05:30
|
|
|
service_list = ServiceList.new(batch, service_hash).to_array
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
Project.transaction do
|
2020-06-23 00:09:42 +05:30
|
|
|
results = bulk_insert(*service_list)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
if data_fields_present?
|
2020-06-23 00:09:42 +05:30
|
|
|
data_list = DataList.new(results, data_fields_hash, template.data_fields.class).to_array
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
bulk_insert(*data_list)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
run_callbacks(batch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
# 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-08-17 22:00:37 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def service_hash
|
2020-06-23 00:09:42 +05:30
|
|
|
@service_hash ||= template.to_service_hash
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def data_fields_hash
|
|
|
|
@data_fields_hash ||= template.to_data_fields_hash
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
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-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def active_external_issue_tracker?
|
2020-05-24 23:13:21 +05:30
|
|
|
template.issue_tracker? && !template.default
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def active_external_wiki?
|
2020-05-24 23:13:21 +05:30
|
|
|
template.type == 'ExternalWikiService'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|