Commit a38a5a14 authored by Arturo Herrero's avatar Arturo Herrero

Fix query to find the closest group integration

After propagating group-level integrations, all the inheriting
integrations have the same settings and inherit_from_id set with the id
from which they are inheriting the values.

When retrieving the default integration in order to restore the
administration settings (those coming from the group or the admin
instance), we need to look for the closest group integration which does
not inherit from its parent. Basically, the closest with
inherit_from_id = nil.
parent a8d50a38
...@@ -245,7 +245,7 @@ class Service < ApplicationRecord ...@@ -245,7 +245,7 @@ class Service < ApplicationRecord
group_ids = scope.ancestors.select(:id) group_ids = scope.ancestors.select(:id)
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]' array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
where(type: type, group_id: group_ids) where(type: type, group_id: group_ids, inherit_from_id: nil)
.order(Arel.sql("array_position(#{array}::bigint[], services.group_id)")) .order(Arel.sql("array_position(#{array}::bigint[], services.group_id)"))
.first .first
end end
...@@ -263,7 +263,7 @@ class Service < ApplicationRecord ...@@ -263,7 +263,7 @@ class Service < ApplicationRecord
from_union([ from_union([
with_templates ? active.where(template: true) : none, with_templates ? active.where(template: true) : none,
active.where(instance: true), active.where(instance: true),
active.where(group_id: group_ids) active.where(group_id: group_ids, inherit_from_id: nil)
]).order(Arel.sql("type ASC, array_position(#{array}::bigint[], services.group_id), instance DESC")).group_by(&:type).each do |type, records| ]).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! build_from_integration(records.first, association => scope.id).save!
end end
......
...@@ -481,13 +481,21 @@ RSpec.describe Service do ...@@ -481,13 +481,21 @@ RSpec.describe Service do
expect(described_class.default_integration('JiraService', subgroup)).to eq(group_service) expect(described_class.default_integration('JiraService', subgroup)).to eq(group_service)
end end
context 'having a service' do context 'having a service with custom settings' do
let!(:subgroup_service) { create(:jira_service, group_id: subgroup.id, project_id: nil) } let!(:subgroup_service) { create(:jira_service, group_id: subgroup.id, project_id: nil) }
it 'returns the closest group service for a project' do it 'returns the closest group service for a project' do
expect(described_class.default_integration('JiraService', project)).to eq(subgroup_service) expect(described_class.default_integration('JiraService', project)).to eq(subgroup_service)
end end
end end
context 'having a service inheriting settings' do
let!(:subgroup_service) { create(:jira_service, group_id: subgroup.id, project_id: nil, inherit_from_id: group_service.id) }
it 'returns the closest group service which does not inherit from its parent for a project' do
expect(described_class.default_integration('JiraService', project)).to eq(group_service)
end
end
end end
end end
end 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