2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-26 16:10:54 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Labels::TransferService do
|
2021-11-09 19:11:48 -05:00
|
|
|
shared_examples 'transfer labels' do
|
|
|
|
describe '#execute' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
let_it_be(:old_group_ancestor) { create(:group) }
|
|
|
|
let_it_be(:old_group) { create(:group, parent: old_group_ancestor) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
let_it_be(:new_group) { create(:group) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
let_it_be(:project) { create(:project, :repository, group: new_group) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
subject(:service) { described_class.new(user, old_group, project) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
before do
|
|
|
|
old_group_ancestor.add_developer(user)
|
|
|
|
new_group.add_developer(user)
|
|
|
|
end
|
2020-11-11 07:09:06 -05:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
labeled_issue = create(:labeled_issue, project: project, labels: [old_group_label_1])
|
|
|
|
labeled_merge_request = create(:labeled_merge_request, source_project: project, labels: [old_group_label_2])
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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_label_1.title))
|
|
|
|
expect(labeled_merge_request.reload.labels).to contain_exactly(project.labels.find_by_title(old_group_label_2.title))
|
|
|
|
end
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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])
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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])
|
|
|
|
create(:label_priority, project: project, label: old_group_label, priority: 1)
|
2016-10-17 20:39:21 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
service.execute
|
2016-10-17 20:39:21 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
new_project_label = project.labels.find_by(title: old_group_label.title)
|
|
|
|
expect(new_project_label.id).not_to eq old_group_label.id
|
|
|
|
expect(new_project_label.priorities).not_to be_empty
|
|
|
|
end
|
2016-10-17 20:39:21 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
it 'does not recreate missing group labels that are not applied to issues or merge requests' do
|
|
|
|
old_group_label = create(:group_label, group: old_group)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
service.execute
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
expect(project.labels.where(title: old_group_label.title)).to be_empty
|
|
|
|
end
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
it 'does not recreate missing group labels that already exist in the project group' do
|
|
|
|
old_group_label = create(:group_label, group: old_group)
|
|
|
|
labeled_issue = create(:labeled_issue, project: project, labels: [old_group_label])
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
new_group_label = create(:group_label, group: new_group, title: old_group_label.title)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
service.execute
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
expect(project.labels.where(title: old_group_label.title)).to be_empty
|
|
|
|
expect(labeled_issue.reload.labels).to contain_exactly(new_group_label)
|
|
|
|
end
|
2018-04-19 04:17:12 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
it 'updates only label links in the given project' do
|
|
|
|
old_group_label = create(:group_label, group: old_group)
|
|
|
|
other_project = create(:project, group: old_group)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
labeled_issue = create(:labeled_issue, project: project, labels: [old_group_label])
|
|
|
|
other_project_labeled_issue = create(:labeled_issue, project: other_project, labels: [old_group_label])
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
service.execute
|
2018-04-19 04:17:12 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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) }
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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)
|
2020-03-17 20:09:16 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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])
|
2018-04-19 04:17:12 -04:00
|
|
|
|
2021-11-09 19:11:48 -05:00
|
|
|
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
|
2020-03-17 20:09:16 -04:00
|
|
|
end
|
2018-04-19 04:17:12 -04:00
|
|
|
end
|
2016-09-26 16:10:54 -04:00
|
|
|
end
|
2021-11-09 19:11:48 -05:00
|
|
|
|
2021-12-20 13:13:27 -05:00
|
|
|
it_behaves_like 'transfer labels'
|
2016-09-26 16:10:54 -04:00
|
|
|
end
|