Commit 856a511b authored by James Lopez's avatar James Lopez

refactor code based on feedback

parent f15466bd
...@@ -11,7 +11,7 @@ module Projects ...@@ -11,7 +11,7 @@ module Projects
end end
def propagate def propagate
return unless @template&.active? return unless @template.active?
Rails.logger.info("Propagating services for template #{@template.id}") Rails.logger.info("Propagating services for template #{@template.id}")
...@@ -32,11 +32,11 @@ module Projects ...@@ -32,11 +32,11 @@ module Projects
def bulk_create_from_template(batch) def bulk_create_from_template(batch)
service_list = batch.map do |project_id| service_list = batch.map do |project_id|
service_hash.merge('project_id' => project_id).values service_hash.values << project_id
end end
Project.transaction do Project.transaction do
bulk_insert_services(service_hash.keys + ['project_id'], service_list) bulk_insert_services(service_hash.keys << 'project_id', service_list)
run_callbacks(batch) run_callbacks(batch)
end end
end end
...@@ -75,9 +75,9 @@ module Projects ...@@ -75,9 +75,9 @@ module Projects
template_hash.each_with_object({}) do |(key, value), service_hash| template_hash.each_with_object({}) do |(key, value), service_hash|
value = value.is_a?(Hash) ? value.to_json : value value = value.is_a?(Hash) ? value.to_json : value
key = Gitlab::Database.postgresql? ? "\"#{key}\"" : "`#{key}`"
service_hash[key] = ActiveRecord::Base.sanitize(value) service_hash[ActiveRecord::Base.connection.quote_column_name(key)] =
ActiveRecord::Base.sanitize(value)
end end
end end
end end
...@@ -93,7 +93,7 @@ module Projects ...@@ -93,7 +93,7 @@ module Projects
end end
def active_external_issue_tracker? def active_external_issue_tracker?
@template.category == :issue_tracker && !@template.default @template.issue_tracker? && !@template.default
end end
def active_external_wiki? def active_external_wiki?
......
...@@ -3,8 +3,6 @@ class PropagateServiceTemplateWorker ...@@ -3,8 +3,6 @@ class PropagateServiceTemplateWorker
include Sidekiq::Worker include Sidekiq::Worker
include DedicatedSidekiqQueue include DedicatedSidekiqQueue
sidekiq_options retry: 3
LEASE_TIMEOUT = 4.hours.to_i LEASE_TIMEOUT = 4.hours.to_i
def perform(template_id) def perform(template_id)
......
...@@ -18,8 +18,11 @@ describe Projects::PropagateServiceTemplate, services: true do ...@@ -18,8 +18,11 @@ describe Projects::PropagateServiceTemplate, services: true do
let!(:project) { create(:empty_project) } let!(:project) { create(:empty_project) }
it 'creates services for projects' do it 'creates services for projects' do
expect { described_class.propagate(service_template) }. expect(project.pushover_service).to be_nil
to change { Service.count }.by(1)
described_class.propagate(service_template)
expect(project.reload.pushover_service).to be_present
end end
it 'creates services for a project that has another service' do it 'creates services for a project that has another service' do
...@@ -35,8 +38,11 @@ describe Projects::PropagateServiceTemplate, services: true do ...@@ -35,8 +38,11 @@ describe Projects::PropagateServiceTemplate, services: true do
} }
) )
expect { described_class.propagate(service_template) }. expect(project.pushover_service).to be_nil
to change { Service.count }.by(1)
described_class.propagate(service_template)
expect(project.reload.pushover_service).to be_present
end end
it 'does not create the service if it exists already' do it 'does not create the service if it exists already' do
...@@ -61,9 +67,7 @@ describe Projects::PropagateServiceTemplate, services: true do ...@@ -61,9 +67,7 @@ describe Projects::PropagateServiceTemplate, services: true do
it 'creates the service containing the template attributes' do it 'creates the service containing the template attributes' do
described_class.propagate(service_template) described_class.propagate(service_template)
service = Service.find_by!(type: service_template.type, template: false) expect(project.pushover_service.properties).to eq(service_template.properties)
expect(service.properties).to eq(service_template.properties)
end end
describe 'bulk update' do describe 'bulk update' do
......
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