Commit 971aa13b authored by charlie ablett's avatar charlie ablett

Merge branch '218275-new-created-projects-inherit-instance-level' into 'master'

New created projects inherit instance integration

See merge request gitlab-org/gitlab!32775
parents a8141db1 7fbf16ac
...@@ -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
subject(:project) { create_project(user, opts) }
context 'when there is an active instance-level and an active template integration' do
before do before do
create(:prometheus_service, project: nil, template: true, active: true) create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/')
create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/')
end end
it 'creates a service from this template' do it 'creates a service from the instance-level integration' do
project = create_project(user, opts) expect(project.services.count).to eq(1)
expect(project.services.first.api_url).to eq('https://prometheus.instance.com/')
expect(project.services.count).to eq 1
expect(project.errors).to be_empty
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
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).to be_persisted
expect(service.active).to be false 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