Commit e397829d authored by Arturo Herrero's avatar Arturo Herrero

Extract duplication on creation services

This removes the duplication creating services for new projects and
groups.
parent b2d09c24
......@@ -255,6 +255,19 @@ class Service < ApplicationRecord
end
private_class_method :instance_level_integration
def self.create_from_active_default_integrations(scope, association, with_templates: false)
group_ids = scope.ancestors.select(:id)
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
from_union([
with_templates ? active.where(template: true) : none,
active.where(instance: true),
active.where(group_id: group_ids)
]).order(Arel.sql("type ASC, array_position(#{array}::bigint[], services.group_id), instance DESC")).group_by(&:type).each do |type, records|
build_from_integration(records.first, association => scope.id).save!
end
end
def activated?
active
end
......
......@@ -32,7 +32,7 @@ module Groups
if @group.save
@group.add_owner(current_user)
@group.create_namespace_settings
create_services_from_active_default_integrations(@group) if Feature.enabled?(:group_level_integrations)
Service.create_from_active_default_integrations(@group, :group_id) if Feature.enabled?(:group_level_integrations)
end
end
......@@ -86,25 +86,6 @@ module Groups
params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility
end
# rubocop: disable CodeReuse/ActiveRecord
def create_services_from_active_default_integrations(group)
group_ids = group.ancestors.select(:id)
Service.from_union([
Service.active.where(instance: true),
Service.active.where(group_id: group_ids)
]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records|
Service.build_from_integration(records.first, group_id: group.id).save!
end
end
# rubocop: enable CodeReuse/ActiveRecord
def by_type_group_ids_and_instance(group_ids)
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
Arel.sql("type ASC, array_position(#{array}::bigint[], services.group_id), instance DESC")
end
end
end
......
......@@ -162,7 +162,7 @@ module Projects
if @project.save
unless @project.gitlab_project_import?
create_services_from_active_default_integrations_or_templates(@project)
Service.create_from_active_default_integrations(@project, :project_id, with_templates: true)
@project.create_labels
end
......@@ -228,26 +228,6 @@ module Projects
private
# rubocop: disable CodeReuse/ActiveRecord
def create_services_from_active_default_integrations_or_templates(project)
group_ids = project.ancestors.select(:id)
Service.from_union([
Service.active.where(instance: true),
Service.active.where(template: true),
Service.active.where(group_id: group_ids)
]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records|
Service.build_from_integration(records.first, project_id: project.id).save!
end
end
# rubocop: enable CodeReuse/ActiveRecord
def by_type_group_ids_and_instance(group_ids)
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
Arel.sql("type ASC, array_position(#{array}::bigint[], services.group_id), instance DESC")
end
def project_namespace
@project_namespace ||= Namespace.find_by_id(@params[:namespace_id]) || current_user.namespace
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