Commit 8e742b18 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '218250-build-from-integration' into 'master'

Service model: Refactor 'build_from_template' to make it agnostic

See merge request gitlab-org/gitlab!32588
parents 1621fea4 262ff95f
......@@ -1273,7 +1273,7 @@ class Project < ApplicationRecord
template = find_service(services_templates, name)
if template
Service.build_from_template(id, template)
Service.build_from_integration(id, template)
else
public_send("build_#{name}_service") # rubocop:disable GitlabSecurity/PublicSend
end
......
......@@ -335,17 +335,18 @@ class Service < ApplicationRecord
services_names.map { |service_name| "#{service_name}_service".camelize }
end
def self.build_from_template(project_id, template)
service = template.dup
def self.build_from_integration(project_id, integration)
service = integration.dup
if template.supports_data_fields?
data_fields = template.data_fields.dup
if integration.supports_data_fields?
data_fields = integration.data_fields.dup
data_fields.service = service
end
service.template = false
service.instance = false
service.project_id = project_id
service.active = false if service.active? && service.invalid?
service.active = false if service.invalid?
service
end
......
......@@ -178,7 +178,7 @@ module Projects
# rubocop: disable CodeReuse/ActiveRecord
def create_services_from_active_templates(project)
Service.where(template: true, active: true).each do |template|
service = Service.build_from_template(project.id, template)
service = Service.build_from_integration(project.id, template)
service.save!
end
end
......
......@@ -2736,9 +2736,6 @@ Service
when repository is empty
test runs execute
Template
.build_from_template
when template is invalid
sets service template to inactive when template is invalid
for pushover service
is prefilled for projects pushover service
has all fields prefilled
......
......@@ -264,13 +264,13 @@ describe Service do
end
end
describe '.build_from_template' do
describe '.build_from_integration' do
context 'when template is invalid' do
it 'sets service template to inactive when template is invalid' do
template = build(:prometheus_service, template: true, active: true, properties: {})
template.save(validate: false)
service = described_class.build_from_template(project.id, template)
service = described_class.build_from_integration(project.id, template)
expect(service).to be_valid
expect(service.active).to be false
......@@ -293,7 +293,7 @@ describe Service do
shared_examples 'service creation from a template' do
it 'creates a correct service' do
service = described_class.build_from_template(project.id, template)
service = described_class.build_from_integration(project.id, template)
expect(service).to be_active
expect(service.title).to eq(title)
......@@ -302,6 +302,8 @@ describe Service do
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)
end
end
......
......@@ -62,8 +62,8 @@ describe Projects::PropagateServiceTemplate do
}
)
Service.build_from_template(project.id, service_template).save!
Service.build_from_template(project.id, other_service).save!
Service.build_from_integration(project.id, service_template).save!
Service.build_from_integration(project.id, other_service).save!
expect { described_class.propagate(service_template) }
.not_to change { Service.count }
......
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