2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
module Integrations
|
|
|
|
class PropagateService
|
|
|
|
BATCH_SIZE = 10_000
|
|
|
|
|
|
|
|
def initialize(integration)
|
|
|
|
@integration = integration
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def propagate
|
2021-06-08 01:23:25 +05:30
|
|
|
if integration.instance_level?
|
2021-01-29 00:20:46 +05:30
|
|
|
update_inherited_integrations
|
2021-02-22 17:27:13 +05:30
|
|
|
create_integration_for_groups_without_integration
|
2021-01-03 14:25:43 +05:30
|
|
|
create_integration_for_projects_without_integration
|
|
|
|
else
|
2021-01-29 00:20:46 +05:30
|
|
|
update_inherited_descendant_integrations
|
2021-01-03 14:25:43 +05:30
|
|
|
create_integration_for_groups_without_integration_belonging_to_group
|
|
|
|
create_integration_for_projects_without_integration_belonging_to_group
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def self.propagate(integration)
|
|
|
|
new(integration).propagate
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
private
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
attr_reader :integration
|
|
|
|
|
|
|
|
def create_integration_for_projects_without_integration
|
|
|
|
propagate_integrations(
|
|
|
|
Project.without_integration(integration),
|
|
|
|
PropagateIntegrationProjectWorker
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def update_inherited_integrations
|
2021-01-29 00:20:46 +05:30
|
|
|
propagate_integrations(
|
2021-06-08 01:23:25 +05:30
|
|
|
Integration.by_type(integration.type).inherit_from_id(integration.id),
|
2021-01-29 00:20:46 +05:30
|
|
|
PropagateIntegrationInheritWorker
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_inherited_descendant_integrations
|
|
|
|
propagate_integrations(
|
2021-06-08 01:23:25 +05:30
|
|
|
Integration.inherited_descendants_from_self_or_ancestors_from(integration),
|
2021-01-29 00:20:46 +05:30
|
|
|
PropagateIntegrationInheritDescendantWorker
|
|
|
|
)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def create_integration_for_groups_without_integration
|
2021-01-29 00:20:46 +05:30
|
|
|
propagate_integrations(
|
|
|
|
Group.without_integration(integration),
|
|
|
|
PropagateIntegrationGroupWorker
|
|
|
|
)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def create_integration_for_groups_without_integration_belonging_to_group
|
2021-01-29 00:20:46 +05:30
|
|
|
propagate_integrations(
|
|
|
|
integration.group.descendants.without_integration(integration),
|
|
|
|
PropagateIntegrationGroupWorker
|
|
|
|
)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def create_integration_for_projects_without_integration_belonging_to_group
|
2021-01-29 00:20:46 +05:30
|
|
|
propagate_integrations(
|
|
|
|
Project.without_integration(integration).in_namespace(integration.group.self_and_descendants),
|
|
|
|
PropagateIntegrationProjectWorker
|
|
|
|
)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
def propagate_integrations(relation, worker_class)
|
|
|
|
relation.each_batch(of: BATCH_SIZE) do |records|
|
|
|
|
min_id, max_id = records.pick("MIN(#{relation.table_name}.id), MAX(#{relation.table_name}.id)")
|
|
|
|
worker_class.perform_async(integration.id, min_id, max_id)
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|