Commit 58156648 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '263225-default-integration-not-inheriting' into 'master'

Fix query to find the closest group integration

See merge request gitlab-org/gitlab!45022
parents eb8551ae 6cfeab55
...@@ -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,119 @@ RSpec.describe Service do ...@@ -481,13 +481,119 @@ 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
describe '.create_from_active_default_integrations' do
context 'with an active service template' do
let_it_be(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
it 'creates a service from the template' do
described_class.create_from_active_default_integrations(project, :project_id, with_templates: true)
expect(project.reload.services.size).to eq(1)
expect(project.reload.services.first.api_url).to eq(template_integration.api_url)
expect(project.reload.services.first.inherit_from_id).to be_nil
end
context 'with an active instance-level integration' do
let!(:instance_integration) { create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/') }
it 'creates a service from the instance-level integration' do
described_class.create_from_active_default_integrations(project, :project_id, with_templates: true)
expect(project.reload.services.size).to eq(1)
expect(project.reload.services.first.api_url).to eq(instance_integration.api_url)
expect(project.reload.services.first.inherit_from_id).to eq(instance_integration.id)
end
context 'passing a group' do
it 'creates a service from the instance-level integration' do
described_class.create_from_active_default_integrations(group, :group_id)
expect(group.reload.services.size).to eq(1)
expect(group.reload.services.first.api_url).to eq(instance_integration.api_url)
expect(group.reload.services.first.inherit_from_id).to eq(instance_integration.id)
end
end
context 'with an active group-level integration' do
let!(:group_integration) { create(:prometheus_service, group: group, project: nil, api_url: 'https://prometheus.group.com/') }
it 'creates a service from the group-level integration' do
described_class.create_from_active_default_integrations(project, :project_id, with_templates: true)
expect(project.reload.services.size).to eq(1)
expect(project.reload.services.first.api_url).to eq(group_integration.api_url)
expect(project.reload.services.first.inherit_from_id).to eq(group_integration.id)
end
context 'passing a group' do
let!(:subgroup) { create(:group, parent: group) }
it 'creates a service from the group-level integration' do
described_class.create_from_active_default_integrations(subgroup, :group_id)
expect(subgroup.reload.services.size).to eq(1)
expect(subgroup.reload.services.first.api_url).to eq(group_integration.api_url)
expect(subgroup.reload.services.first.inherit_from_id).to eq(group_integration.id)
end
end
context 'with an active subgroup' do
let!(:subgroup_integration) { create(:prometheus_service, group: subgroup, project: nil, api_url: 'https://prometheus.subgroup.com/') }
let!(:subgroup) { create(:group, parent: group) }
let(:project) { create(:project, group: subgroup) }
it 'creates a service from the subgroup-level integration' do
described_class.create_from_active_default_integrations(project, :project_id, with_templates: true)
expect(project.reload.services.size).to eq(1)
expect(project.reload.services.first.api_url).to eq(subgroup_integration.api_url)
expect(project.reload.services.first.inherit_from_id).to eq(subgroup_integration.id)
end
context 'passing a group' do
let!(:sub_subgroup) { create(:group, parent: subgroup) }
it 'creates a service from the subgroup-level integration' do
described_class.create_from_active_default_integrations(sub_subgroup, :group_id)
expect(sub_subgroup.reload.services.size).to eq(1)
expect(sub_subgroup.reload.services.first.api_url).to eq(subgroup_integration.api_url)
expect(sub_subgroup.reload.services.first.inherit_from_id).to eq(subgroup_integration.id)
end
context 'having a service inheriting settings' do
let!(:subgroup_integration) { create(:prometheus_service, group: subgroup, project: nil, inherit_from_id: group_integration.id, api_url: 'https://prometheus.subgroup.com/') }
it 'creates a service from the group-level integration' do
described_class.create_from_active_default_integrations(sub_subgroup, :group_id)
expect(sub_subgroup.reload.services.size).to eq(1)
expect(sub_subgroup.reload.services.first.api_url).to eq(group_integration.api_url)
expect(sub_subgroup.reload.services.first.inherit_from_id).to eq(group_integration.id)
end
end
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