Commit 6617ecce authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '244320-242012-new-created-groups-inherit-integrations' into 'master'

New created groups inherits integrations

See merge request gitlab-org/gitlab!43162
parents f480022c e397829d
......@@ -2513,10 +2513,10 @@ class Project < ApplicationRecord
def build_from_instance_or_template(name)
instance = find_service(services_instances, name)
return Service.build_from_integration(id, instance) if instance
return Service.build_from_integration(instance, project_id: id) if instance
template = find_service(services_templates, name)
return Service.build_from_integration(id, template) if template
return Service.build_from_integration(template, project_id: id) if template
end
def services_templates
......
......@@ -215,7 +215,7 @@ class Service < ApplicationRecord
services_names.map { |service_name| "#{service_name}_service".camelize }
end
def self.build_from_integration(project_id, integration)
def self.build_from_integration(integration, project_id: nil, group_id: nil)
service = integration.dup
if integration.supports_data_fields?
......@@ -225,9 +225,9 @@ class Service < ApplicationRecord
service.template = false
service.instance = false
service.group = nil
service.inherit_from_id = integration.id if integration.instance? || integration.group
service.project_id = project_id
service.group_id = group_id
service.inherit_from_id = integration.id if integration.instance? || integration.group
service.active = false if service.invalid?
service
end
......@@ -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
......
......@@ -28,9 +28,12 @@ module Groups
@group.build_chat_team(name: response['name'], team_id: response['id'])
end
if @group.save
@group.add_owner(current_user)
add_settings_record
Group.transaction do
if @group.save
@group.add_owner(current_user)
@group.create_namespace_settings
Service.create_from_active_default_integrations(@group, :group_id) if Feature.enabled?(:group_level_integrations)
end
end
@group
......@@ -83,10 +86,6 @@ module Groups
params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility
end
def add_settings_record
@group.create_namespace_settings
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(project.id, records.first).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
......
......@@ -320,7 +320,7 @@ RSpec.describe Service do
end
it 'sets service to inactive' do
service = described_class.build_from_integration(project.id, integration)
service = described_class.build_from_integration(integration, project_id: project.id)
expect(service).to be_valid
expect(service.active).to be false
......@@ -331,7 +331,7 @@ RSpec.describe Service do
let(:integration) { create(:jira_service, :instance) }
it 'sets inherit_from_id from integration' do
service = described_class.build_from_integration(project.id, integration)
service = described_class.build_from_integration(integration, project_id: project.id)
expect(service.inherit_from_id).to eq(integration.id)
end
......@@ -341,7 +341,7 @@ RSpec.describe Service do
let(:integration) { create(:jira_service, group: group, project: nil) }
it 'sets inherit_from_id from integration' do
service = described_class.build_from_integration(project.id, integration)
service = described_class.build_from_integration(integration, project_id: project.id)
expect(service.inherit_from_id).to eq(integration.id)
end
......@@ -360,8 +360,8 @@ RSpec.describe Service do
end
shared_examples 'service creation from an integration' do
it 'creates a correct service' do
service = described_class.build_from_integration(project.id, integration)
it 'creates a correct service for a project integration' do
service = described_class.build_from_integration(integration, project_id: project.id)
expect(service).to be_active
expect(service.url).to eq(url)
......@@ -373,6 +373,20 @@ RSpec.describe Service do
expect(service.project).to eq(project)
expect(service.group).to eq(nil)
end
it 'creates a correct service for a group integration' do
service = described_class.build_from_integration(integration, group_id: group.id)
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
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
......
......@@ -61,8 +61,8 @@ RSpec.describe Admin::PropagateServiceTemplate do
}
)
Service.build_from_integration(project.id, service_template).save!
Service.build_from_integration(project.id, other_service).save!
Service.build_from_integration(service_template, project_id: project.id).save!
Service.build_from_integration(other_service, project_id: project.id).save!
expect { described_class.propagate(service_template) }
.not_to change { Service.count }
......
......@@ -138,4 +138,51 @@ RSpec.describe Groups::CreateService, '#execute' do
expect(group.namespace_settings).to be_persisted
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
......@@ -541,7 +541,7 @@ RSpec.describe Projects::CreateService, '#execute' do
}
end
it 'creates a service from the group-level integration' do
it 'creates a service from the subgroup-level integration' do
expect(project.services.count).to eq(1)
expect(project.services.first.api_url).to eq(subgroup_integration.api_url)
expect(project.services.first.inherit_from_id).to eq(subgroup_integration.id)
......
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