Commit 7fd9e39d authored by Tiago Botelho's avatar Tiago Botelho

When a Service templates are invalid newly created projects will have them inactive

parent bf4a3af0
...@@ -273,6 +273,7 @@ class Service < ActiveRecord::Base ...@@ -273,6 +273,7 @@ class Service < ActiveRecord::Base
def self.build_from_template(project_id, template) def self.build_from_template(project_id, template)
service = template.dup service = template.dup
service.active = false unless service.valid?
service.template = false service.template = false
service.project_id = project_id service.project_id = project_id
service service
......
...@@ -133,8 +133,10 @@ module Projects ...@@ -133,8 +133,10 @@ module Projects
def fail(error:) def fail(error:)
message = "Unable to save project. Error: #{error}" message = "Unable to save project. Error: #{error}"
log_message = message.dup
Rails.logger.error(message) log_message << " Project ID: #{@project.id}" if @project&.id
Rails.logger.error(log_message)
if @project if @project
@project.errors.add(:base, message) @project.errors.add(:base, message)
......
...@@ -58,6 +58,21 @@ describe Service do ...@@ -58,6 +58,21 @@ describe Service do
end end
describe "Template" do describe "Template" do
describe '.build_from_template' do
context 'when template is invalid' do
it 'sets service template to inactive when template is invalid' do
project = create(:project)
template = JiraService.new(template: true, active: true)
template.save(validate: false)
service = described_class.build_from_template(project.id, template)
expect(service).to be_valid
expect(service.active).to be false
end
end
end
describe "for pushover service" do describe "for pushover service" do
let!(:service_template) do let!(:service_template) do
PushoverService.create( PushoverService.create(
......
...@@ -71,14 +71,14 @@ describe Projects::CreateService, '#execute' do ...@@ -71,14 +71,14 @@ describe Projects::CreateService, '#execute' do
expect(create_project(user, opts)).to eq(nil) expect(create_project(user, opts)).to eq(nil)
end end
it 'handles invalid service' do it 'sets invalid service as inactive' do
create(:service, type: 'JiraService', project: nil, template: true, active: true) create(:service, type: 'JiraService', project: nil, template: true, active: true)
project = create_project(user, opts) project = create_project(user, opts)
service = project.services.first
expect(project).not_to be_persisted expect(project).to be_persisted
expect(project.errors.full_messages_for(:base).first).to match(/Unable to save project. Error: Unable to save JiraService/) expect(service.active).to be false
expect(project.services.count).to eq 0
end end
end end
...@@ -242,14 +242,15 @@ describe Projects::CreateService, '#execute' do ...@@ -242,14 +242,15 @@ describe Projects::CreateService, '#execute' do
end end
context 'when a bad service template is created' do context 'when a bad service template is created' do
it 'reports an error in the imported project' do it 'sets service to be inactive' do
opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-ce' opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-ce'
create(:service, type: 'DroneCiService', project: nil, template: true, active: true) create(:service, type: 'DroneCiService', project: nil, template: true, active: true)
project = create_project(user, opts) project = create_project(user, opts)
service = project.services.first
expect(project.errors.full_messages_for(:base).first).to match(/Unable to save project. Error: Unable to save DroneCiService/) expect(project).to be_persisted
expect(project.services.count).to eq 0 expect(service.active).to be false
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