summaryrefslogtreecommitdiff
path: root/spec/finders/merge_requests_finder_spec.rb
blob: 107da08a0a905fcf3d103d197bd134acf3c79ecd (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'spec_helper'

describe MergeRequestsFinder do
  include ProjectForksHelper

  # We need to explicitly permit Gitaly N+1s because of the specs that use
  # :request_store. Gitaly N+1 detection is only enabled when :request_store is,
  # but we don't care about potential N+1s when we're just creating several
  # projects in the setup phase.
  def create_project_without_n_plus_1(*args)
    Gitlab::GitalyClient.allow_n_plus_1_calls do
      create(:project, :public, *args)
    end
  end

  let(:user)  { create :user }
  let(:user2) { create :user }

  let(:group) { create(:group) }
  let(:subgroup) { create(:group, parent: group) }
  let(:project1) { create_project_without_n_plus_1(group: group) }
  let(:project2) do
    Gitlab::GitalyClient.allow_n_plus_1_calls do
      fork_project(project1, user)
    end
  end
  let(:project3) do
    Gitlab::GitalyClient.allow_n_plus_1_calls do
      p = fork_project(project1, user)
      p.update!(archived: true)
      p
    end
  end
  let(:project4) { create_project_without_n_plus_1(group: subgroup) }
  let(:project5) { create_project_without_n_plus_1(group: subgroup) }
  let(:project6) { create_project_without_n_plus_1(group: subgroup) }

  let!(:merge_request1) { create(:merge_request, :simple, author: user, source_project: project2, target_project: project1) }
  let!(:merge_request2) { create(:merge_request, :conflict, author: user, source_project: project2, target_project: project1, state: 'closed') }
  let!(:merge_request3) { create(:merge_request, :simple, author: user, source_project: project2, target_project: project2, state: 'locked', title: 'thing WIP thing') }
  let!(:merge_request4) { create(:merge_request, :simple, author: user, source_project: project3, target_project: project3, title: 'WIP thing') }
  let!(:merge_request5) { create(:merge_request, :simple, author: user, source_project: project4, target_project: project4, title: '[WIP]') }
  let!(:merge_request6) { create(:merge_request, :simple, author: user, source_project: project5, target_project: project5, title: 'WIP: thing') }
  let!(:merge_request7) { create(:merge_request, :simple, author: user, source_project: project6, target_project: project6, title: 'wip thing') }
  let!(:merge_request8) { create(:merge_request, :simple, author: user, source_project: project1, target_project: project1, title: '[wip] thing') }
  let!(:merge_request9) { create(:merge_request, :simple, author: user, source_project: project1, target_project: project2, title: 'wip: thing') }

  before do
    project1.add_maintainer(user)
    project2.add_developer(user)
    project3.add_developer(user)
    project2.add_developer(user2)
    project4.add_developer(user)
    project5.add_developer(user)
    project6.add_developer(user)
  end

  describe "#execute" do
    it 'filters by scope' do
      params = { scope: 'authored', state: 'opened' }
      merge_requests = described_class.new(user, params).execute
      expect(merge_requests.size).to eq(7)
    end

    it 'filters by project' do
      params = { project_id: project1.id, scope: 'authored', state: 'opened' }
      merge_requests = described_class.new(user, params).execute
      expect(merge_requests.size).to eq(2)
    end

    context 'filtering by group' do
      it 'includes all merge requests when user has access' do
        params = { group_id: group.id }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests.size).to eq(3)
      end

      it 'excludes merge requests from projects the user does not have access to' do
        private_project = create_project_without_n_plus_1(:private, group: group)
        private_mr = create(:merge_request, :simple, author: user, source_project: private_project, target_project: private_project)
        params = { group_id: group.id }

        private_project.add_guest(user)
        merge_requests = described_class.new(user, params).execute

        expect(merge_requests.size).to eq(3)
        expect(merge_requests).not_to include(private_mr)
      end

      it 'filters by group including subgroups', :nested_groups do
        params = { group_id: group.id, include_subgroups: true }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests.size).to eq(6)
      end
    end

    it 'filters by non_archived' do
      params = { non_archived: true }
      merge_requests = described_class.new(user, params).execute
      expect(merge_requests.size).to eq(8)
    end

    it 'filters by iid' do
      params = { project_id: project1.id, iids: merge_request1.iid }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request1)
    end

    it 'filters by source branch' do
      params = { source_branch: merge_request2.source_branch }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request2)
    end

    it 'filters by target branch' do
      params = { target_branch: merge_request2.target_branch }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request2)
    end

    it 'filters by state' do
      params = { state: 'locked' }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request3)
    end

    it 'filters by wip' do
      params = { wip: 'yes' }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request4, merge_request5, merge_request6, merge_request7, merge_request8, merge_request9)
    end

    it 'filters by not wip' do
      params = { wip: 'no' }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request1, merge_request2, merge_request3)
    end

    it 'returns all items if no valid wip param exists' do
      params = { wip: '' }

      merge_requests = described_class.new(user, params).execute

      expect(merge_requests).to contain_exactly(merge_request1, merge_request2, merge_request3, merge_request4, merge_request5, merge_request6, merge_request7, merge_request8, merge_request9)
    end

    it 'adds wip to scalar params' do
      scalar_params = described_class.scalar_params

      expect(scalar_params).to include(:wip, :assignee_id)
    end

    context 'filtering by group milestone' do
      let!(:group) { create(:group, :public) }
      let(:group_milestone) { create(:milestone, group: group) }
      let!(:group_member) { create(:group_member, group: group, user: user) }
      let(:params) { { milestone_title: group_milestone.title } }

      before do
        project2.update(namespace: group)
        merge_request2.update(milestone: group_milestone)
        merge_request3.update(milestone: group_milestone)
      end

      it 'returns issues assigned to that group milestone' do
        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(merge_request2, merge_request3)
      end
    end

    context 'filtering by created_at/updated_at' do
      let(:new_project) { create(:project, forked_from_project: project1) }

      let!(:new_merge_request) do
        create(:merge_request,
               :simple,
               author: user,
               created_at: 1.week.from_now,
               updated_at: 1.week.from_now,
               source_project: new_project,
               target_project: new_project)
      end

      let!(:old_merge_request) do
        create(:merge_request,
               :simple,
               author: user,
               source_branch: 'feature_1',
               created_at: 1.week.ago,
               updated_at: 1.week.ago,
               source_project: new_project,
               target_project: new_project)
      end

      before do
        new_project.add_maintainer(user)
      end

      it 'filters by created_after' do
        params = { project_id: new_project.id, created_after: new_merge_request.created_at }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(new_merge_request)
      end

      it 'filters by created_before' do
        params = { project_id: new_project.id, created_before: old_merge_request.created_at }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(old_merge_request)
      end

      it 'filters by created_after and created_before' do
        params = {
          project_id: new_project.id,
          created_after: old_merge_request.created_at,
          created_before: new_merge_request.created_at
        }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(old_merge_request, new_merge_request)
      end

      it 'filters by updated_after' do
        params = { project_id: new_project.id, updated_after: new_merge_request.updated_at }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(new_merge_request)
      end

      it 'filters by updated_before' do
        params = { project_id: new_project.id, updated_before: old_merge_request.updated_at }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(old_merge_request)
      end

      it 'filters by updated_after and updated_before' do
        params = {
          project_id: new_project.id,
          updated_after: old_merge_request.updated_at,
          updated_before: new_merge_request.updated_at
        }

        merge_requests = described_class.new(user, params).execute

        expect(merge_requests).to contain_exactly(old_merge_request, new_merge_request)
      end
    end
  end

  describe '#row_count', :request_store do
    it 'returns the number of rows for the default state' do
      finder = described_class.new(user)

      expect(finder.row_count).to eq(7)
    end

    it 'returns the number of rows for a given state' do
      finder = described_class.new(user, state: 'closed')

      expect(finder.row_count).to eq(1)
    end
  end
end