Commit 92316603 authored by Arturo Herrero's avatar Arturo Herrero

Remove duplication of project_ids_without_integration

Extract common code into the Integration concern, then we can use
Project.ids_without_integration.
parent a3002773
......@@ -15,5 +15,19 @@ module Integration
Project.where(id: custom_integration_project_ids)
end
def ids_without_integration(integration, limit)
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: integration.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(limit)
.pluck(:id)
end
end
end
......@@ -64,7 +64,7 @@ module Admin
def create_integration_for_projects_without_integration
loop do
batch = Project.uncached { project_ids_without_integration }
batch = Project.uncached { Project.ids_without_integration(integration, BATCH_SIZE) }
bulk_create_from_integration(batch) unless batch.empty?
......@@ -114,22 +114,6 @@ module Admin
integration.type == 'ExternalWikiService'
end
# rubocop: disable CodeReuse/ActiveRecord
def project_ids_without_integration
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: integration.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(BATCH_SIZE)
.pluck(:id)
end
# rubocop: enable CodeReuse/ActiveRecord
def service_hash
@service_hash ||= integration.to_service_hash
.tap { |json| json['inherit_from_id'] = integration.id }
......
......@@ -26,7 +26,7 @@ module Projects
def propagate_projects_with_template
loop do
batch = Project.uncached { project_ids_without_integration }
batch = Project.uncached { Project.ids_without_integration(template, BATCH_SIZE) }
bulk_create_from_template(batch) unless batch.empty?
......@@ -50,22 +50,6 @@ module Projects
end
end
# rubocop: disable CodeReuse/ActiveRecord
def project_ids_without_integration
services = Service
.select('1')
.where('services.project_id = projects.id')
.where(type: template.type)
Project
.where('NOT EXISTS (?)', services)
.where(pending_delete: false)
.where(archived: false)
.limit(BATCH_SIZE)
.pluck(:id)
end
# rubocop: enable CodeReuse/ActiveRecord
def bulk_insert(klass, columns, values_array)
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
......
......@@ -3,8 +3,9 @@
require 'spec_helper'
RSpec.describe Integration do
let(:project_1) { create(:project) }
let(:project_2) { create(:project) }
let!(:project_1) { create(:project) }
let!(:project_2) { create(:project) }
let!(:project_3) { create(:project) }
let(:instance_integration) { create(:jira_service, :instance) }
before do
......@@ -18,4 +19,10 @@ RSpec.describe Integration do
expect(Project.with_custom_integration_for(instance_integration)).to contain_exactly(project_2)
end
end
describe '#ids_without_integration' do
it 'returns projects ids without an integration' do
expect(Project.ids_without_integration(instance_integration, 100)).to contain_exactly(project_3.id)
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