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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'getting merge request information nested in a project', feature_category: :code_review_workflow do
include GraphqlHelpers
let_it_be(:project) { create(:project, :repository, :public) }
let_it_be(:current_user) { create(:user) }
let_it_be_with_reload(:merge_request) { create(:merge_request, source_project: project) }
let(:merge_request_graphql_data) { graphql_data_at(:project, :merge_request) }
let(:mr_fields) { all_graphql_fields_for('MergeRequest', max_depth: 1) }
let(:query) do
graphql_query_for(
:project,
{ full_path: project.full_path },
query_graphql_field(:merge_request, { iid: merge_request.iid.to_s }, mr_fields)
)
end
it_behaves_like 'a working graphql query' do
# we exclude Project.pipeline because it needs arguments,
# codequalityReportsComparer because it is behind a feature flag
# and runners because the user is not an admin and therefore has no access
let(:excluded) { %w[jobs pipeline runners codequalityReportsComparer] }
let(:mr_fields) { all_graphql_fields_for('MergeRequest', excluded: excluded) }
before do
post_graphql(query, current_user: current_user)
end
end
it 'contains merge request information' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data).not_to be_nil
end
# This is a field coming from the `MergeRequestPresenter`
it 'includes a web_url' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data['webUrl']).to be_present
end
context 'when selecting author' do
let(:mr_fields) { 'author { username }' }
it 'includes author' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data['author']['username']).to eq(merge_request.author.username)
end
end
context 'when the merge_request has reviewers' do
let(:mr_fields) do
<<~SELECT
reviewers { nodes { id username } }
participants { nodes { id username } }
SELECT
end
before do
merge_request.reviewers << create_list(:user, 2)
end
it 'includes reviewers' do
expected = merge_request.reviewers.map do |r|
a_graphql_entity_for(r, :username)
end
post_graphql(query, current_user: current_user)
expect(graphql_data_at(:project, :merge_request, :reviewers, :nodes)).to match_array(expected)
expect(graphql_data_at(:project, :merge_request, :participants, :nodes)).to include(*expected)
end
end
context 'when the merge_request has committers' do
let(:mr_fields) do
<<~SELECT
committers { nodes { id username } }
SELECT
end
it 'includes committers' do
expected = merge_request.committers.map do |r|
a_hash_including('id' => global_id_of(r), 'username' => r.username)
end
post_graphql(query, current_user: current_user)
expect(graphql_data_at(:project, :merge_request, :committers, :nodes)).to match_array(expected)
end
end
describe 'diffStats' do
let(:mr_fields) do
<<~FIELDS
diffStats { #{all_graphql_fields_for('DiffStats')} }
diffStatsSummary { #{all_graphql_fields_for('DiffStatsSummary')} }
FIELDS
end
it 'includes diff stats' do
be_natural = an_instance_of(Integer).and(be >= 0)
post_graphql(query, current_user: current_user)
sums = merge_request_graphql_data['diffStats'].reduce([0, 0, 0]) do |(a, d, c), node|
a_, d_ = node.values_at('additions', 'deletions')
[a + a_, d + d_, c + a_ + d_]
end
expect(merge_request_graphql_data).to include(
'diffStats' => all(a_hash_including('path' => String, 'additions' => be_natural, 'deletions' => be_natural)),
'diffStatsSummary' => a_hash_including(
'fileCount' => merge_request.diff_stats.count,
'additions' => be_natural,
'deletions' => be_natural,
'changes' => be_natural
)
)
# diff_stats is consistent with summary
expect(merge_request_graphql_data['diffStatsSummary']
.values_at('additions', 'deletions', 'changes')).to eq(sums)
# diff_stats_summary is internally consistent
expect(merge_request_graphql_data['diffStatsSummary']
.values_at('additions', 'deletions').sum)
.to eq(merge_request_graphql_data.dig('diffStatsSummary', 'changes'))
.and be_positive
end
context 'when requesting a specific diff stat' do
let(:diff_stat) { merge_request.diff_stats.first }
let(:mr_fields) do
query_graphql_field(
:diff_stats,
{ path: diff_stat.path },
all_graphql_fields_for('DiffStats')
)
end
it 'includes only the requested stats' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data).to include(
'diffStats' => contain_exactly(
a_hash_including(
'path' => diff_stat.path,
'additions' => diff_stat.additions,
'deletions' => diff_stat.deletions
)
)
)
end
end
end
it 'includes correct mergedAt value when merged' do
time = 1.week.ago
merge_request.mark_as_merged
merge_request.metrics.update!(merged_at: time)
post_graphql(query, current_user: current_user)
retrieved = merge_request_graphql_data['mergedAt']
expect(Time.zone.parse(retrieved)).to be_within(1.second).of(time)
end
it 'includes nil mergedAt value when not merged' do
post_graphql(query, current_user: current_user)
retrieved = merge_request_graphql_data['mergedAt']
expect(retrieved).to be_nil
end
describe 'permissions on the merge request' do
let(:mr_fields) do
"userPermissions { #{all_graphql_fields_for('MergeRequestPermissions')} }"
end
it 'includes the permissions for the current user on a public project' do
expected_permissions = {
'readMergeRequest' => true,
'adminMergeRequest' => false,
'createNote' => true,
'pushToSourceBranch' => false,
'removeSourceBranch' => false,
'cherryPickOnCurrentMergeRequest' => false,
'revertOnCurrentMergeRequest' => false,
'updateMergeRequest' => false,
'canMerge' => false,
'canApprove' => false
}
post_graphql(query, current_user: current_user)
permission_data = merge_request_graphql_data['userPermissions']
expect(permission_data).to be_present
expect(permission_data).to eq(expected_permissions)
end
end
context 'when the user does not have access to the merge request' do
it 'returns nil' do
project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)
post_graphql(query)
expect(merge_request_graphql_data).to be_nil
end
end
context 'when there are pipelines' do
let_it_be(:pipeline) do
create(
:ci_pipeline,
project: merge_request.source_project,
ref: merge_request.source_branch,
sha: merge_request.diff_head_sha
)
end
let(:mr_fields) do
<<~FIELDS
headPipeline { id }
pipelines { nodes { id } }
FIELDS
end
before do
merge_request.update_head_pipeline
end
it 'has a head pipeline' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data['headPipeline']).to be_present
end
it 'has pipeline connections' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data['pipelines']['nodes']).to be_one
end
end
context 'when limiting the number of results' do
let(:merge_requests_graphql_data) { graphql_data_at(:project, :merge_requests, :edges) }
let(:fields) do
<<~QUERY
edges {
node {
iid,
title
}
}
QUERY
end
let(:query) do
graphql_query_for(
'project',
{ 'fullPath' => project.full_path },
"mergeRequests(first: 2) { #{fields} }"
)
end
it 'returns the correct number of results' do
create(:merge_request, source_project: project, source_branch: 'branch-1')
create(:merge_request, source_project: project, source_branch: 'branch-2')
create(:merge_request, source_project: project, source_branch: 'branch-3')
post_graphql(query, current_user: current_user)
expect(merge_requests_graphql_data.size).to eq 2
end
end
context 'when merge request is cannot_be_merged_rechecking' do
before do
merge_request.update!(merge_status: 'cannot_be_merged_rechecking')
end
it 'returns checking' do
post_graphql(query, current_user: current_user)
expect(merge_request_graphql_data['mergeStatus']).to eq('checking')
end
end
# see: https://gitlab.com/gitlab-org/gitlab/-/issues/297358
context 'when the notes have been preloaded (by participants)' do
let(:query) do
<<~GQL
query($path: ID!) {
project(fullPath: $path) {
mrs: mergeRequests(first: 1) {
nodes {
participants { nodes { id } }
notes(first: 1) {
pageInfo { endCursor hasPreviousPage hasNextPage }
nodes { id }
}
}
}
}
}
GQL
end
before do
create_list(:note_on_merge_request, 3, project: project, noteable: merge_request)
end
it 'does not error' do
post_graphql(
query,
current_user: current_user,
variables: { path: project.full_path }
)
expect(graphql_data_at(:project, :mrs, :nodes, :notes, :pageInfo)).to contain_exactly a_hash_including(
'endCursor' => String,
'hasNextPage' => true,
'hasPreviousPage' => false
)
end
end
shared_examples 'when requesting information about MR interactions' do
let_it_be(:user) { create(:user) }
let(:selected_fields) { all_graphql_fields_for('UserMergeRequestInteraction') }
let(:mr_fields) do
query_nodes(
field,
query_graphql_field(:merge_request_interaction, nil, selected_fields)
)
end
def interaction_data
graphql_data_at(:project, :merge_request, field, :nodes, :merge_request_interaction)
end
context 'when the user is not assigned' do
it 'returns null data' do
post_graphql(query)
expect(interaction_data).to be_empty
end
end
context 'when the user is a reviewer, but has not reviewed' do
before do
project.add_guest(user)
assign_user(user)
end
it 'returns falsey values' do
post_graphql(query)
expect(interaction_data).to contain_exactly a_hash_including(
'canMerge' => false,
'canUpdate' => can_update,
'reviewState' => unreviewed,
'reviewed' => false,
'approved' => false
)
end
end
context 'when the user has interacted' do
before do
project.add_maintainer(user)
assign_user(user)
r = merge_request.merge_request_reviewers.find_or_create_by!(reviewer: user)
r.update!(state: 'reviewed')
merge_request.approved_by_users << user
end
it 'returns appropriate data' do
post_graphql(query)
enum = ::Types::MergeRequestReviewStateEnum.values['REVIEWED']
expect(interaction_data).to contain_exactly a_hash_including(
'canMerge' => true,
'canUpdate' => true,
'reviewState' => enum.graphql_name,
'reviewed' => true,
'approved' => true
)
end
end
describe 'scalability' do
let_it_be(:other_users) { create_list(:user, 3) }
let(:unreviewed) do
{ 'reviewState' => 'UNREVIEWED' }
end
let(:reviewed) do
{ 'reviewState' => 'REVIEWED' }
end
shared_examples 'scalable query for interaction fields' do
before do
([user] + other_users).each { project.add_guest(_1) }
end
it 'does not suffer from N+1' do
assign_user(user)
merge_request.merge_request_reviewers
.find_or_create_by!(reviewer: user)
.update!(state: 'reviewed')
baseline = ActiveRecord::QueryRecorder.new do
post_graphql(query)
end
expect(interaction_data).to contain_exactly(include(reviewed))
other_users.each do |user|
assign_user(user)
merge_request.merge_request_reviewers.find_or_create_by!(reviewer: user)
end
expect { post_graphql(query) }.not_to exceed_query_limit(baseline)
expect(interaction_data).to contain_exactly(
include(unreviewed),
include(unreviewed),
include(unreviewed),
include(reviewed)
)
end
end
context 'when selecting only known scalable fields' do
let(:not_scalable) { %w[canUpdate canMerge] }
let(:selected_fields) do
all_graphql_fields_for('UserMergeRequestInteraction', excluded: not_scalable)
end
it_behaves_like 'scalable query for interaction fields'
end
context 'when selecting all fields' do
before do
pending "See: https://gitlab.com/gitlab-org/gitlab/-/issues/322549"
end
let(:selected_fields) { all_graphql_fields_for('UserMergeRequestInteraction') }
it_behaves_like 'scalable query for interaction fields'
end
end
end
it_behaves_like 'when requesting information about MR interactions' do
let(:field) { :reviewers }
let(:unreviewed) { 'UNREVIEWED' }
let(:can_update) { false }
def assign_user(user)
merge_request.merge_request_reviewers.create!(reviewer: user)
end
end
it_behaves_like 'when requesting information about MR interactions' do
let(:field) { :assignees }
let(:unreviewed) { nil }
let(:can_update) { true } # assignees can update MRs
def assign_user(user)
merge_request.assignees << user
end
end
context 'when selecting `awardEmoji`' do
let_it_be(:award_emoji) { create(:award_emoji, awardable: merge_request, user: current_user) }
let(:mr_fields) do
<<~QUERY
awardEmoji {
nodes {
user {
username
}
name
}
}
QUERY
end
it 'includes award emojis' do
post_graphql(query, current_user: current_user)
response = merge_request_graphql_data['awardEmoji']['nodes']
expect(response.length).to eq(1)
expect(response.first['user']['username']).to eq(current_user.username)
expect(response.first['name']).to eq(award_emoji.name)
end
end
end
|