Commit 0431ff43 authored by Jarka Košanová's avatar Jarka Košanová

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

Build integration from instance inherits id

See merge request gitlab-org/gitlab!33974
parents 12dc7b94 e81a0337
...@@ -355,6 +355,7 @@ class Service < ApplicationRecord ...@@ -355,6 +355,7 @@ class Service < ApplicationRecord
service.template = false service.template = false
service.instance = false service.instance = false
service.inherit_from_id = integration.id if integration.instance?
service.project_id = project_id service.project_id = project_id
service.active = false if service.invalid? service.active = false if service.invalid?
service service
......
...@@ -279,19 +279,31 @@ describe Service do ...@@ -279,19 +279,31 @@ describe Service do
end end
describe '.build_from_integration' do describe '.build_from_integration' do
context 'when template is invalid' do context 'when integration is invalid' do
it 'sets service template to inactive when template is invalid' do let(:integration) do
template = build(:prometheus_service, template: true, active: true, properties: {}) build(:prometheus_service, :template, active: true, properties: {})
template.save(validate: false) .tap { |integration| integration.save(validate: false) }
end
service = described_class.build_from_integration(project.id, template) it 'sets service to inactive' do
service = described_class.build_from_integration(project.id, integration)
expect(service).to be_valid expect(service).to be_valid
expect(service.active).to be false expect(service.active).to be false
end end
end end
describe 'build issue tracker from a template' do context 'when integration is an instance' do
let(:integration) { create(:jira_service, :instance) }
it 'sets inherit_from_id 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(:title) { 'custom title' }
let(:description) { 'custom description' } let(:description) { 'custom description' }
let(:url) { 'http://jira.example.com' } let(:url) { 'http://jira.example.com' }
...@@ -305,9 +317,9 @@ describe Service do ...@@ -305,9 +317,9 @@ describe Service do
} }
end end
shared_examples 'service creation from a template' do shared_examples 'service creation from an integration' do
it 'creates a correct service' 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).to be_active
expect(service.title).to eq(title) expect(service.title).to eq(title)
...@@ -324,30 +336,30 @@ describe Service do ...@@ -324,30 +336,30 @@ describe Service do
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404 # this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
context 'when data are stored in properties' do context 'when data are stored in properties' do
let(:properties) { data_params.merge(title: title, description: description) } 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')) create(:jira_service, :without_properties_callback, template: true, properties: properties.merge(additional: 'something'))
end end
it_behaves_like 'service creation from a template' it_behaves_like 'service creation from an integration'
end end
context 'when data are stored in separated fields' do 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)) create(:jira_service, :template, data_params.merge(properties: {}, title: title, description: description))
end end
it_behaves_like 'service creation from a template' it_behaves_like 'service creation from an integration'
end end
context 'when data are stored in both properties and separated fields' do context 'when data are stored in both properties and separated fields' do
let(:properties) { data_params.merge(title: title, description: description) } 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_service, :without_properties_callback, active: true, template: true, properties: properties).tap do |service|
create(:jira_tracker_data, data_params.merge(service: service)) create(:jira_tracker_data, data_params.merge(service: service))
end end
end end
it_behaves_like 'service creation from a template' it_behaves_like 'service creation from an integration'
end end
end end
end end
......
...@@ -453,24 +453,23 @@ describe Projects::CreateService, '#execute' do ...@@ -453,24 +453,23 @@ describe Projects::CreateService, '#execute' do
subject(:project) { create_project(user, opts) } subject(:project) { create_project(user, opts) }
context 'when there is an active instance-level and an active template integration' do context 'when there is an active instance-level and an active template integration' do
before do let!(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/') let!(:instance_integration) { create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/') }
create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/')
end
it 'creates a service from the instance-level integration' do it 'creates a service from the instance-level integration' do
expect(project.services.count).to eq(1) 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
end end
context 'when there is an active service template' do context 'when there is an active service template' do
before do let!(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
create(:prometheus_service, :template, active: true)
end
it 'creates a service from the template' do it 'creates a service from the template' do
expect(project.services.count).to eq(1) 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
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