Commit 527e59d0 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix ancestor group labels when moving projects

When moving within the same ancestor group, we should not recreate
these labels because these are still available to the moved project

Also, we should take ancestor group labels into account when
searching for labels that we need to transfer
parent e2b0b262
......@@ -49,7 +49,7 @@ module Labels
Label.joins(:issues)
.where(
issues: { project_id: project.id },
labels: { type: 'GroupLabel', group_id: old_group.id }
labels: { type: 'GroupLabel', group_id: old_group.self_and_ancestors }
)
end
# rubocop: enable CodeReuse/ActiveRecord
......@@ -59,14 +59,14 @@ module Labels
Label.joins(:merge_requests)
.where(
merge_requests: { target_project_id: project.id },
labels: { type: 'GroupLabel', group_id: old_group.id }
labels: { type: 'GroupLabel', group_id: old_group.self_and_ancestors }
)
end
# rubocop: enable CodeReuse/ActiveRecord
def find_or_create_label!(label)
params = label.attributes.slice('title', 'description', 'color')
new_label = FindOrCreateService.new(current_user, project, params).execute
new_label = FindOrCreateService.new(current_user, project, params.merge(include_ancestor_groups: true)).execute
new_label.id
end
......
---
title: Fix duplicate labels when moving projects within the same ancestor group
merge_request: 27261
author:
type: fixed
......@@ -15,7 +15,7 @@ describe Labels::TransferService do
subject(:service) { described_class.new(user, old_group, project) }
it 'recreates the missing group labels at project level and assigns them to the issuables' do
it 'recreates missing group labels at project level and assigns them to the issuables' do
old_group_label_1 = create(:group_label, group: old_group)
old_group_label_2 = create(:group_label, group: old_group)
......@@ -27,6 +27,18 @@ describe Labels::TransferService do
expect(labeled_merge_request.reload.labels).to contain_exactly(project.labels.find_by_title(old_group_label_2.title))
end
it 'recreates missing ancestor group labels at project level and assigns them to the issuables' do
old_group_ancestor_label_1 = create(:group_label, group: old_group_ancestor)
old_group_ancestor_label_2 = create(:group_label, group: old_group_ancestor)
labeled_issue = create(:labeled_issue, project: project, labels: [old_group_ancestor_label_1])
labeled_merge_request = create(:labeled_merge_request, source_project: project, labels: [old_group_ancestor_label_2])
expect { service.execute }.to change(project.labels, :count).by(2)
expect(labeled_issue.reload.labels).to contain_exactly(project.labels.find_by_title(old_group_ancestor_label_1.title))
expect(labeled_merge_request.reload.labels).to contain_exactly(project.labels.find_by_title(old_group_ancestor_label_2.title))
end
it 'recreates label priorities related to the missing group labels' do
old_group_label = create(:group_label, group: old_group)
create(:labeled_issue, project: project, labels: [old_group_label])
......@@ -71,5 +83,22 @@ describe Labels::TransferService do
expect(labeled_issue.reload.labels).not_to include(old_group_label)
expect(other_project_labeled_issue.reload.labels).to contain_exactly(old_group_label)
end
context 'when moving within the same ancestor group' do
let(:other_subgroup) { create(:group, parent: old_group_ancestor) }
let(:project) { create(:project, :repository, group: other_subgroup) }
it 'does not recreate ancestor group labels' do
old_group_ancestor_label_1 = create(:group_label, group: old_group_ancestor)
old_group_ancestor_label_2 = create(:group_label, group: old_group_ancestor)
labeled_issue = create(:labeled_issue, project: project, labels: [old_group_ancestor_label_1])
labeled_merge_request = create(:labeled_merge_request, source_project: project, labels: [old_group_ancestor_label_2])
expect { service.execute }.not_to change(project.labels, :count)
expect(labeled_issue.reload.labels).to contain_exactly(old_group_ancestor_label_1)
expect(labeled_merge_request.reload.labels).to contain_exactly(old_group_ancestor_label_2)
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