debian-mirror-gitlab/spec/requests/api/graphql/project/merge_request_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

511 lines
14 KiB
Ruby
Raw Permalink Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe 'getting merge request information nested in a project', feature_category: :code_review_workflow do
2018-11-08 19:23:39 +05:30
include GraphqlHelpers
2021-04-29 21:17:54 +05:30
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) }
2018-11-08 19:23:39 +05:30
let(:query) do
graphql_query_for(
2021-04-29 21:17:54 +05:30
:project,
{ full_path: project.full_path },
query_graphql_field(:merge_request, { iid: merge_request.iid.to_s }, mr_fields)
2018-11-08 19:23:39 +05:30
)
end
it_behaves_like 'a working graphql query' do
2021-04-29 21:17:54 +05:30
# we exclude Project.pipeline because it needs arguments
let(:mr_fields) { all_graphql_fields_for('MergeRequest', excluded: %w[jobs pipeline]) }
2018-11-08 19:23:39 +05:30
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
2021-04-29 21:17:54 +05:30
context 'when selecting author' do
let(:mr_fields) { 'author { username }' }
2020-06-23 00:09:42 +05:30
2021-04-29 21:17:54 +05:30
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
2020-06-23 00:09:42 +05:30
end
2021-04-29 21:17:54 +05:30
context 'when the merge_request has reviewers' do
2021-03-08 18:12:59 +05:30
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|
2022-07-16 23:28:13 +05:30
a_graphql_entity_for(r, :username)
2021-03-08 18:12:59 +05:30
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)
2022-05-07 20:08:51 +05:30
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)
2021-03-08 18:12:59 +05:30
end
end
2021-04-29 21:17:54 +05:30
describe 'diffStats' do
let(:mr_fields) do
<<~FIELDS
diffStats { #{all_graphql_fields_for('DiffStats')} }
diffStatsSummary { #{all_graphql_fields_for('DiffStatsSummary')} }
FIELDS
2020-07-28 23:09:34 +05:30
end
2021-04-29 21:17:54 +05:30
it 'includes diff stats' do
be_natural = an_instance_of(Integer).and(be >= 0)
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
post_graphql(query, current_user: current_user)
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
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
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
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
)
2020-07-28 23:09:34 +05:30
)
2021-04-29 21:17:54 +05:30
# 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
2020-07-28 23:09:34 +05:30
end
2021-04-29 21:17:54 +05:30
context 'when requesting a specific diff stat' do
let(:diff_stat) { merge_request.diff_stats.first }
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
let(:mr_fields) do
query_graphql_field(
:diff_stats,
{ path: diff_stat.path },
all_graphql_fields_for('DiffStats')
2020-07-28 23:09:34 +05:30
)
2021-04-29 21:17:54 +05:30
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
2020-07-28 23:09:34 +05:30
end
end
2020-06-23 00:09:42 +05:30
it 'includes correct mergedAt value when merged' do
time = 1.week.ago
merge_request.mark_as_merged
2021-04-29 21:17:54 +05:30
merge_request.metrics.update!(merged_at: time)
2020-06-23 00:09:42 +05:30
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
2021-04-29 21:17:54 +05:30
describe 'permissions on the merge request' do
let(:mr_fields) do
"userPermissions { #{all_graphql_fields_for('MergeRequestPermissions')} }"
end
2018-11-08 19:23:39 +05:30
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,
2020-11-24 15:15:51 +05:30
'updateMergeRequest' => false,
2023-04-23 21:23:45 +05:30
'canMerge' => false,
'canApprove' => false
2018-11-08 19:23:39 +05:30
}
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
2021-04-29 21:17:54 +05:30
let_it_be(:pipeline) do
2019-07-07 11:18:12 +05:30
create(
2018-11-08 19:23:39 +05:30
:ci_pipeline,
project: merge_request.source_project,
ref: merge_request.source_branch,
sha: merge_request.diff_head_sha
)
2021-04-29 21:17:54 +05:30
end
let(:mr_fields) do
<<~FIELDS
headPipeline { id }
pipelines { nodes { id } }
FIELDS
end
before do
2019-07-07 11:18:12 +05:30
merge_request.update_head_pipeline
2018-11-08 19:23:39 +05:30
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)
2021-04-29 21:17:54 +05:30
expect(merge_request_graphql_data['pipelines']['nodes']).to be_one
2018-11-08 19:23:39 +05:30
end
end
2020-04-22 19:07:51 +05:30
context 'when limiting the number of results' do
2021-04-29 21:17:54 +05:30
let(:merge_requests_graphql_data) { graphql_data_at(:project, :merge_requests, :edges) }
2020-04-22 19:07:51 +05:30
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
2021-04-29 21:17:54 +05:30
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')
2020-04-22 19:07:51 +05:30
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
2021-03-08 18:12:59 +05:30
# 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
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
shared_examples 'when requesting information about MR interactions' do
2021-04-29 21:17:54 +05:30
let_it_be(:user) { create(:user) }
let(:selected_fields) { all_graphql_fields_for('UserMergeRequestInteraction') }
let(:mr_fields) do
query_nodes(
2021-06-08 01:23:25 +05:30
field,
2021-04-29 21:17:54 +05:30
query_graphql_field(:merge_request_interaction, nil, selected_fields)
)
end
def interaction_data
2021-06-08 01:23:25 +05:30
graphql_data_at(:project, :merge_request, field, :nodes, :merge_request_interaction)
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
context 'when the user is not assigned' do
2021-04-29 21:17:54 +05:30
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)
2021-06-08 01:23:25 +05:30
assign_user(user)
2021-04-29 21:17:54 +05:30
end
it 'returns falsey values' do
post_graphql(query)
expect(interaction_data).to contain_exactly a_hash_including(
'canMerge' => false,
2021-06-08 01:23:25 +05:30
'canUpdate' => can_update,
2022-10-11 01:57:18 +05:30
'reviewState' => unreviewed,
2021-04-29 21:17:54 +05:30
'reviewed' => false,
'approved' => false
)
end
end
context 'when the user has interacted' do
before do
project.add_maintainer(user)
2021-06-08 01:23:25 +05:30
assign_user(user)
r = merge_request.merge_request_reviewers.find_or_create_by!(reviewer: user)
r.update!(state: 'reviewed')
2021-04-29 21:17:54 +05:30
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) }
2022-10-11 01:57:18 +05:30
let(:unreviewed) do
{ 'reviewState' => 'UNREVIEWED' }
2021-04-29 21:17:54 +05:30
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
2021-06-08 01:23:25 +05:30
assign_user(user)
merge_request.merge_request_reviewers
.find_or_create_by!(reviewer: user)
.update!(state: 'reviewed')
2021-04-29 21:17:54 +05:30
baseline = ActiveRecord::QueryRecorder.new do
post_graphql(query)
end
expect(interaction_data).to contain_exactly(include(reviewed))
other_users.each do |user|
2021-06-08 01:23:25 +05:30
assign_user(user)
2022-10-11 01:57:18 +05:30
merge_request.merge_request_reviewers.find_or_create_by!(reviewer: user)
2021-04-29 21:17:54 +05:30
end
expect { post_graphql(query) }.not_to exceed_query_limit(baseline)
expect(interaction_data).to contain_exactly(
2022-10-11 01:57:18 +05:30
include(unreviewed),
include(unreviewed),
include(unreviewed),
2021-04-29 21:17:54 +05:30
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
2021-06-08 01:23:25 +05:30
it_behaves_like 'when requesting information about MR interactions' do
let(:field) { :reviewers }
2022-10-11 01:57:18 +05:30
let(:unreviewed) { 'UNREVIEWED' }
2021-06-08 01:23:25 +05:30
let(:can_update) { false }
def assign_user(user)
2022-10-11 01:57:18 +05:30
merge_request.merge_request_reviewers.create!(reviewer: user)
2021-06-08 01:23:25 +05:30
end
end
it_behaves_like 'when requesting information about MR interactions' do
let(:field) { :assignees }
2022-10-11 01:57:18 +05:30
let(:unreviewed) { nil }
2021-06-08 01:23:25 +05:30
let(:can_update) { true } # assignees can update MRs
def assign_user(user)
merge_request.assignees << user
end
end
2023-06-20 00:43:36 +05:30
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
2018-11-08 19:23:39 +05:30
end