Commit 8a1b6b78 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'jprovazn-label-links' into 'master'

Fix label links update on project transfer

Closes #44158

See merge request gitlab-org/gitlab-ce!18320
parents 0517e1d8 470b3cb5
...@@ -64,9 +64,14 @@ module Labels ...@@ -64,9 +64,14 @@ module Labels
end end
def update_label_links(labels, old_label_id:, new_label_id:) def update_label_links(labels, old_label_id:, new_label_id:)
LabelLink.joins(:label) # use 'labels' relation to get label_link ids only of issues/MRs
.merge(labels) # in the project being transferred.
.where(label_id: old_label_id) # IDs are fetched in a separate query because MySQL doesn't
# allow referring of 'label_links' table in UPDATE query:
# https://gitlab.com/gitlab-org/gitlab-ce/-/jobs/62435068
link_ids = labels.pluck('label_links.id')
LabelLink.where(id: link_ids, label_id: old_label_id)
.update_all(label_id: new_label_id) .update_all(label_id: new_label_id)
end end
......
---
title: Fix label links update on project transfer
merge_request:
author:
type: fixed
...@@ -8,6 +8,7 @@ describe Labels::TransferService do ...@@ -8,6 +8,7 @@ describe Labels::TransferService do
let(:group_3) { create(:group) } let(:group_3) { create(:group) }
let(:project_1) { create(:project, namespace: group_2) } let(:project_1) { create(:project, namespace: group_2) }
let(:project_2) { create(:project, namespace: group_3) } let(:project_2) { create(:project, namespace: group_3) }
let(:project_3) { create(:project, namespace: group_1) }
let(:group_label_1) { create(:group_label, group: group_1, name: 'Group Label 1') } let(:group_label_1) { create(:group_label, group: group_1, name: 'Group Label 1') }
let(:group_label_2) { create(:group_label, group: group_1, name: 'Group Label 2') } let(:group_label_2) { create(:group_label, group: group_1, name: 'Group Label 2') }
...@@ -23,6 +24,7 @@ describe Labels::TransferService do ...@@ -23,6 +24,7 @@ describe Labels::TransferService do
create(:labeled_issue, project: project_1, labels: [group_label_4]) create(:labeled_issue, project: project_1, labels: [group_label_4])
create(:labeled_issue, project: project_1, labels: [project_label_1]) create(:labeled_issue, project: project_1, labels: [project_label_1])
create(:labeled_issue, project: project_2, labels: [group_label_5]) create(:labeled_issue, project: project_2, labels: [group_label_5])
create(:labeled_issue, project: project_3, labels: [group_label_1])
create(:labeled_merge_request, source_project: project_1, labels: [group_label_1, group_label_2]) create(:labeled_merge_request, source_project: project_1, labels: [group_label_1, group_label_2])
create(:labeled_merge_request, source_project: project_2, labels: [group_label_5]) create(:labeled_merge_request, source_project: project_2, labels: [group_label_5])
end end
...@@ -52,5 +54,13 @@ describe Labels::TransferService do ...@@ -52,5 +54,13 @@ describe Labels::TransferService do
expect(project_1.labels.where(title: group_label_4.title)).to be_empty expect(project_1.labels.where(title: group_label_4.title)).to be_empty
end end
it 'updates only label links in the given project' do
service.execute
targets = LabelLink.where(label_id: group_label_1.id).map(&:target)
expect(targets).to eq(project_3.issues)
end
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