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