Commit c2b330dc authored by Arturo Herrero's avatar Arturo Herrero

Fix project callbacks when propagating integrations

After introducing the propagation of integrations using batching and
queues https://gitlab.com/gitlab-org/gitlab/-/merge_requests/42128, we
are not updating the project callbacks: has_external_issue_tracker and
has_external_wiki.

The reason for that is that the batch was uncached but after the merge
request batch is scoped to the query.

In this case, the order is important because we do two things:
- Create the integration for those projects without integration.
- Update has_external_issue_tracker and has_external_wiki in those
  projects.

If we first create the integrations when we then try to update the
callbacks, there is nothing to update since the batch scope is empty at
this time. We have to update first and then create the integrations.
parent a6a84a12
......@@ -11,6 +11,8 @@ class BulkCreateIntegrationService
service_list = ServiceList.new(batch, service_hash, association).to_array
Service.transaction do
run_callbacks(batch) if association == 'project'
results = bulk_insert(*service_list)
if integration.data_fields_present?
......@@ -18,8 +20,6 @@ class BulkCreateIntegrationService
bulk_insert(*data_list)
end
run_callbacks(batch) if association == 'project'
end
end
......@@ -33,17 +33,15 @@ class BulkCreateIntegrationService
klass.insert_all(items_to_insert, returning: [:id])
end
# rubocop: disable CodeReuse/ActiveRecord
def run_callbacks(batch)
if integration.issue_tracker?
Project.where(id: batch.select(:id)).update_all(has_external_issue_tracker: true)
batch.update_all(has_external_issue_tracker: true)
end
if integration.type == 'ExternalWikiService'
Project.where(id: batch.select(:id)).update_all(has_external_wiki: true)
batch.update_all(has_external_wiki: true)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def service_hash
if integration.template?
......
---
title: Fix project callbacks when propagating integrations
merge_request: 45781
author:
type: fixed
......@@ -72,7 +72,7 @@ RSpec.describe BulkCreateIntegrationService do
context 'with a project association' do
let!(:project) { create(:project) }
let(:created_integration) { project.jira_service }
let(:batch) { Project.all }
let(:batch) { Project.without_integration(integration) }
let(:association) { 'project' }
it_behaves_like 'creates integration from batch ids'
......
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