Commit 40d1ea1c authored by Arturo Herrero's avatar Arturo Herrero

New created groups inherits integrations

When creating a new group, it should inherit the integrations following
the precedence of the closest integration from the ancestors of the
group or the instance-level integration.
parent a13d588b
...@@ -215,7 +215,7 @@ class Service < ApplicationRecord ...@@ -215,7 +215,7 @@ class Service < ApplicationRecord
services_names.map { |service_name| "#{service_name}_service".camelize } services_names.map { |service_name| "#{service_name}_service".camelize }
end end
def self.build_from_integration(project_id, integration) def self.build_from_integration(id, integration, belongs_to_project = true)
service = integration.dup service = integration.dup
if integration.supports_data_fields? if integration.supports_data_fields?
...@@ -223,11 +223,17 @@ class Service < ApplicationRecord ...@@ -223,11 +223,17 @@ class Service < ApplicationRecord
data_fields.service = service data_fields.service = service
end end
if belongs_to_project
service.group = nil
service.project_id = id
else
service.project = nil
service.group_id = id
end
service.template = false service.template = false
service.instance = false service.instance = false
service.group = nil
service.inherit_from_id = integration.id if integration.instance? || integration.group service.inherit_from_id = integration.id if integration.instance? || integration.group
service.project_id = project_id
service.active = false if service.invalid? service.active = false if service.invalid?
service service
end end
......
...@@ -28,9 +28,12 @@ module Groups ...@@ -28,9 +28,12 @@ module Groups
@group.build_chat_team(name: response['name'], team_id: response['id']) @group.build_chat_team(name: response['name'], team_id: response['id'])
end end
Group.transaction do
if @group.save if @group.save
@group.add_owner(current_user) @group.add_owner(current_user)
add_settings_record @group.create_namespace_settings
create_services_from_active_default_integrations(@group) if Feature.enabled?(:group_level_integrations)
end
end end
@group @group
...@@ -84,8 +87,23 @@ module Groups ...@@ -84,8 +87,23 @@ module Groups
params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility
end end
def add_settings_record # rubocop: disable CodeReuse/ActiveRecord
@group.create_namespace_settings 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(group.id, records.first, false).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 end
end end
......
...@@ -360,7 +360,7 @@ RSpec.describe Service do ...@@ -360,7 +360,7 @@ RSpec.describe Service do
end end
shared_examples 'service creation from an integration' do shared_examples 'service creation from an integration' do
it 'creates a correct service' do it 'creates a correct service for a project integration' do
service = described_class.build_from_integration(project.id, integration) service = described_class.build_from_integration(project.id, integration)
expect(service).to be_active expect(service).to be_active
...@@ -373,6 +373,20 @@ RSpec.describe Service do ...@@ -373,6 +373,20 @@ RSpec.describe Service do
expect(service.project).to eq(project) expect(service.project).to eq(project)
expect(service.group).to eq(nil) expect(service.group).to eq(nil)
end end
it 'creates a correct service for a group integration' do
service = described_class.build_from_integration(group.id, integration, false)
expect(service).to be_active
expect(service.url).to eq(url)
expect(service.api_url).to eq(api_url)
expect(service.username).to eq(username)
expect(service.password).to eq(password)
expect(service.template).to eq(false)
expect(service.instance).to eq(false)
expect(service.project).to eq(nil)
expect(service.group).to eq(group)
end
end end
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404 # this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
......
...@@ -138,4 +138,51 @@ RSpec.describe Groups::CreateService, '#execute' do ...@@ -138,4 +138,51 @@ RSpec.describe Groups::CreateService, '#execute' do
expect(group.namespace_settings).to be_persisted expect(group.namespace_settings).to be_persisted
end end
end end
describe 'create service for the group' do
let(:service) { described_class.new(user, group_params) }
let(:created_group) { service.execute }
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
expect(created_group.services.count).to eq(1)
expect(created_group.services.first.api_url).to eq(instance_integration.api_url)
expect(created_group.services.first.inherit_from_id).to eq(instance_integration.id)
end
context 'with an active group-level integration' do
let(:service) { described_class.new(user, group_params.merge(parent_id: group.id)) }
let!(:group_integration) { create(:prometheus_service, group: group, project: nil, api_url: 'https://prometheus.group.com/') }
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
it 'creates a service from the group-level integration' do
expect(created_group.services.count).to eq(1)
expect(created_group.services.first.api_url).to eq(group_integration.api_url)
expect(created_group.services.first.inherit_from_id).to eq(group_integration.id)
end
context 'with an active subgroup' do
let(:service) { described_class.new(user, group_params.merge(parent_id: subgroup.id)) }
let!(:subgroup_integration) { create(:prometheus_service, group: subgroup, project: nil, api_url: 'https://prometheus.subgroup.com/') }
let(:subgroup) do
create(:group, parent: group).tap do |subgroup|
subgroup.add_owner(user)
end
end
it 'creates a service from the subgroup-level integration' do
expect(created_group.services.count).to eq(1)
expect(created_group.services.first.api_url).to eq(subgroup_integration.api_url)
expect(created_group.services.first.inherit_from_id).to eq(subgroup_integration.id)
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