Commit 7fbf16ac authored by Arturo Herrero's avatar Arturo Herrero Committed by charlie ablett

New created projects inherit instance integration

Creating a new project should inherit the integration settings from the
instance-level integration before trying from the template.
parent c2a32118
...@@ -150,7 +150,7 @@ module Projects ...@@ -150,7 +150,7 @@ module Projects
if @project.save if @project.save
unless @project.gitlab_project_import? unless @project.gitlab_project_import?
create_services_from_active_templates(@project) create_services_from_active_instances_or_templates(@project)
@project.create_labels @project.create_labels
end end
...@@ -175,15 +175,6 @@ module Projects ...@@ -175,15 +175,6 @@ module Projects
@project @project
end end
# rubocop: disable CodeReuse/ActiveRecord
def create_services_from_active_templates(project)
Service.where(template: true, active: true).each do |template|
service = Service.build_from_integration(project.id, template)
service.save!
end
end
# rubocop: enable CodeReuse/ActiveRecord
def create_prometheus_service def create_prometheus_service
service = @project.find_or_initialize_service(::PrometheusService.to_param) service = @project.find_or_initialize_service(::PrometheusService.to_param)
...@@ -225,6 +216,15 @@ module Projects ...@@ -225,6 +216,15 @@ module Projects
private private
# rubocop: disable CodeReuse/ActiveRecord
def create_services_from_active_instances_or_templates(project)
Service.active.where(instance: true).or(Service.active.where(template: true)).group_by(&:type).each do |type, records|
service = records.find(&:instance?) || records.find(&:template?)
Service.build_from_integration(project.id, service).save!
end
end
# rubocop: enable CodeReuse/ActiveRecord
def project_namespace def project_namespace
@project_namespace ||= Namespace.find_by_id(@params[:namespace_id]) || current_user.namespace @project_namespace ||= Namespace.find_by_id(@params[:namespace_id]) || current_user.namespace
end end
......
...@@ -339,29 +339,40 @@ describe Projects::CreateService, '#execute' do ...@@ -339,29 +339,40 @@ describe Projects::CreateService, '#execute' do
end end
end end
context 'when there is an active service template' do describe 'create service for the project' do
before do subject(:project) { create_project(user, opts) }
create(:prometheus_service, project: nil, template: true, active: true)
end
it 'creates a service from this template' do context 'when there is an active instance-level and an active template integration' do
project = create_project(user, opts) before do
create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/')
create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/')
end
expect(project.services.count).to eq 1 it 'creates a service from the instance-level integration' do
expect(project.errors).to be_empty expect(project.services.count).to eq(1)
expect(project.services.first.api_url).to eq('https://prometheus.instance.com/')
end
end end
end
context 'when a bad service template is created' do context 'when there is an active service template' do
it 'sets service to be inactive' do before do
opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-foss' create(:prometheus_service, :template, active: true)
create(:service, type: 'DroneCiService', project: nil, template: true, active: true) end
project = create_project(user, opts) it 'creates a service from the template' do
service = project.services.first expect(project.services.count).to eq(1)
end
end
expect(project).to be_persisted context 'when there is an invalid integration' do
expect(service.active).to be false before do
create(:service, :template, type: 'DroneCiService', active: true)
end
it 'creates an inactive service' do
expect(project).to be_persisted
expect(project.services.first.active).to be false
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