Commit c094250c authored by Luke Duncalfe's avatar Luke Duncalfe

Merge branch 'ajk-create-from-active-default-integrations-refactor' into 'master'

Refactor code for clarity

See merge request gitlab-org/gitlab!84518
parents 7dcb1383 d7e52188
......@@ -353,16 +353,17 @@ class Integration < ApplicationRecord
end
private_class_method :instance_level_integration
def self.create_from_active_default_integrations(scope, association)
group_ids = sorted_ancestors(scope).select(:id)
# Returns the number of successfully saved integrations
# Duplicate integrations are excluded from this count by their validations.
def self.create_from_active_default_integrations(owner, association)
group_ids = sorted_ancestors(owner).select(:id)
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
order = Arel.sql("type_new ASC, array_position(#{array}::bigint[], #{table_name}.group_id), instance DESC")
from_union([
active.where(instance: true),
active.where(group_id: group_ids, inherit_from_id: nil)
]).order(Arel.sql("type_new ASC, array_position(#{array}::bigint[], #{table_name}.group_id), instance DESC")).group_by(&:type).each do |type, records|
build_from_integration(records.first, association => scope.id).save
end
from_union([active.where(instance: true), active.where(group_id: group_ids, inherit_from_id: nil)])
.order(order)
.group_by(&:type)
.count { |type, parents| build_from_integration(parents.first, association => owner.id).save }
end
def self.inherited_descendants_from_self_or_ancestors_from(integration)
......
......@@ -473,6 +473,18 @@ RSpec.describe Integration do
expect(project.reload.integrations.first.inherit_from_id).to eq(group_integration.id)
end
context 'there are multiple inheritable integrations, and a duplicate' do
let!(:group_integration_2) { create(:jenkins_integration, :group, group: group) }
let!(:group_integration_3) { create(:datadog_integration, :instance) }
let!(:duplicate) { create(:jenkins_integration, project: project) }
it 'returns the number of successfully created integrations' do
expect(described_class.create_from_active_default_integrations(project, :project_id)).to eq 2
expect(project.reload.integrations.size).to eq(3)
end
end
context 'passing a group' do
let!(:subgroup) { create(:group, parent: group) }
......
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