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