1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# frozen_string_literal: true
require 'spec_helper'
describe Milestones::TransferService do
describe '#execute' do
subject(:service) { described_class.new(user, old_group, project) }
context 'when old_group is present' do
let(:user) { create(:admin) }
let(:new_group) { create(:group) }
let(:old_group) { create(:group) }
let(:project) { create(:project, namespace: old_group) }
let(:group_milestone) { create(:milestone, group: old_group)}
let(:group_milestone2) { create(:milestone, group: old_group)}
let(:project_milestone) { create(:milestone, project: project)}
let!(:issue_with_group_milestone) { create(:issue, project: project, milestone: group_milestone) }
let!(:issue_with_project_milestone) { create(:issue, project: project, milestone: project_milestone) }
let!(:mr_with_group_milestone) { create(:merge_request, source_project: project, source_branch: 'branch-1', milestone: group_milestone) }
let!(:mr_with_project_milestone) { create(:merge_request, source_project: project, source_branch: 'branch-2', milestone: project_milestone) }
before do
new_group.add_maintainer(user)
project.add_maintainer(user)
# simulate project transfer
project.update(group: new_group)
end
context 'without existing milestone at the new group level' do
it 'recreates the missing group milestones at project level' do
expect { service.execute }.to change(project.milestones, :count).by(1)
end
it 'applies new project milestone to issues with group milestone' do
service.execute
new_milestone = issue_with_group_milestone.reload.milestone
expect(new_milestone).not_to eq(group_milestone)
expect(new_milestone.title).to eq(group_milestone.title)
expect(new_milestone.project_milestone?).to be_truthy
end
it 'does not apply new project milestone to issues with project milestone' do
service.execute
expect(issue_with_project_milestone.reload.milestone).to eq(project_milestone)
end
it 'applies new project milestone to merge_requests with group milestone' do
service.execute
new_milestone = mr_with_group_milestone.reload.milestone
expect(new_milestone).not_to eq(group_milestone)
expect(new_milestone.title).to eq(group_milestone.title)
expect(new_milestone.project_milestone?).to be_truthy
end
it 'does not apply new project milestone to issuables with project milestone' do
service.execute
expect(mr_with_project_milestone.reload.milestone).to eq(project_milestone)
end
it 'does not recreate missing group milestones that are not applied to issues or merge requests' do
service.execute
new_milestone_title = project.reload.milestones.pluck(:title)
expect(new_milestone_title).to include(group_milestone.title)
expect(new_milestone_title).not_to include(group_milestone2.title)
end
context 'when find_or_create_milestone returns nil' do
before do
allow_next_instance_of(Milestones::FindOrCreateService) do |instance|
allow(instance).to receive(:execute).and_return(nil)
end
end
it 'removes issues group milestone' do
service.execute
expect(mr_with_group_milestone.reload.milestone).to be_nil
end
it 'removes merge requests group milestone' do
service.execute
expect(issue_with_group_milestone.reload.milestone).to be_nil
end
end
end
context 'with existing milestone at the new group level' do
let!(:existing_milestone) { create(:milestone, group: new_group, title: group_milestone.title) }
it 'does not create a new milestone' do
expect { service.execute }.not_to change(project.milestones, :count)
end
it 'applies existing milestone to issues with group milestone' do
service.execute
expect(issue_with_group_milestone.reload.milestone).to eq(existing_milestone)
end
it 'applies existing milestone to merge_requests with group milestone' do
service.execute
expect(mr_with_group_milestone.reload.milestone).to eq(existing_milestone)
end
end
end
end
context 'when old_group is not present' do
let(:user) { create(:admin) }
let(:old_group) { project.group }
let(:project) { create(:project, namespace: user.namespace) }
it 'returns nil' do
expect(described_class.new(user, old_group, project).execute).to be_nil
end
end
end
|