Commit 6e81b31c authored by Arturo Herrero's avatar Arturo Herrero

Build integration from instance inherits id

Build an integration from an instance-level integration inherits its id.
This is needed in order to propagate the values when the instance-level
settings change.
parent 3b4e82b0
......@@ -355,6 +355,7 @@ class Service < ApplicationRecord
service.template = false
service.instance = false
service.inherit_from_id = integration.id if integration.instance?
service.project_id = project_id
service.active = false if service.invalid?
service
......
......@@ -279,8 +279,8 @@ describe Service do
end
describe '.build_from_integration' do
context 'when template is invalid' do
it 'sets service template to inactive when template is invalid' do
context 'when integration is invalid' do
it 'sets service to inactive when integration is invalid' do
template = build(:prometheus_service, template: true, active: true, properties: {})
template.save(validate: false)
......@@ -291,7 +291,17 @@ describe Service do
end
end
describe 'build issue tracker from a template' do
context 'when integration is an instance' do
subject(:integration) { create(:jira_service, :instance) }
it 'sets service inherit from integration' do
service = described_class.build_from_integration(project.id, integration)
expect(service.inherit_from_id).to eq(integration.id)
end
end
describe 'build issue tracker from an integration' do
let(:title) { 'custom title' }
let(:description) { 'custom description' }
let(:url) { 'http://jira.example.com' }
......@@ -305,9 +315,9 @@ describe Service do
}
end
shared_examples 'service creation from a template' do
shared_examples 'service creation from an integration' do
it 'creates a correct service' do
service = described_class.build_from_integration(project.id, template)
service = described_class.build_from_integration(project.id, integration)
expect(service).to be_active
expect(service.title).to eq(title)
......@@ -324,30 +334,30 @@ describe Service do
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
context 'when data are stored in properties' do
let(:properties) { data_params.merge(title: title, description: description) }
let!(:template) do
let!(:integration) do
create(:jira_service, :without_properties_callback, template: true, properties: properties.merge(additional: 'something'))
end
it_behaves_like 'service creation from a template'
it_behaves_like 'service creation from an integration'
end
context 'when data are stored in separated fields' do
let(:template) do
let(:integration) do
create(:jira_service, :template, data_params.merge(properties: {}, title: title, description: description))
end
it_behaves_like 'service creation from a template'
it_behaves_like 'service creation from an integration'
end
context 'when data are stored in both properties and separated fields' do
let(:properties) { data_params.merge(title: title, description: description) }
let(:template) do
let(:integration) do
create(:jira_service, :without_properties_callback, active: true, template: true, properties: properties).tap do |service|
create(:jira_tracker_data, data_params.merge(service: service))
end
end
it_behaves_like 'service creation from a template'
it_behaves_like 'service creation from an integration'
end
end
end
......
......@@ -453,24 +453,23 @@ describe Projects::CreateService, '#execute' do
subject(: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
let!(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
let!(:instance_integration) { create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/') }
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/')
expect(project.services.first.api_url).to eq(instance_integration.api_url)
expect(project.services.first.inherit_from_id).to eq(instance_integration.id)
end
end
context 'when there is an active service template' do
before do
create(:prometheus_service, :template, active: true)
end
let!(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
it 'creates a service from the template' do
expect(project.services.count).to eq(1)
expect(project.services.first.api_url).to eq(template_integration.api_url)
expect(project.services.first.inherit_from_id).to be_nil
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