Commit 70121424 authored by Rémy Coutable's avatar Rémy Coutable

Add the `draft` argument to GraphQL MergeRequestsResolver

Changelog: added
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 356491bd
...@@ -140,14 +140,13 @@ class MergeRequestsFinder < IssuableFinder ...@@ -140,14 +140,13 @@ class MergeRequestsFinder < IssuableFinder
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def by_draft(items) def by_draft(items)
draft_param = params[:draft] || params[:wip] draft_param = Gitlab::Utils.to_boolean(params.fetch(:draft) { params.fetch(:wip, nil) })
return items if draft_param.nil?
if draft_param == 'yes' if draft_param
items.where(wip_match(items.arel_table)) items.where(wip_match(items.arel_table))
elsif draft_param == 'no'
items.where.not(wip_match(items.arel_table))
else else
items items.where.not(wip_match(items.arel_table))
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -51,6 +51,10 @@ module Resolvers ...@@ -51,6 +51,10 @@ module Resolvers
required: false, required: false,
description: 'Merge request state. If provided, all resolved merge requests will have this state.' description: 'Merge request state. If provided, all resolved merge requests will have this state.'
argument :draft, GraphQL::Types::Boolean,
required: false,
description: 'Limit result to draft merge requests.'
argument :labels, [GraphQL::Types::String], argument :labels, [GraphQL::Types::String],
required: false, required: false,
as: :label_name, as: :label_name,
......
This diff is collapsed.
...@@ -278,33 +278,38 @@ RSpec.describe MergeRequestsFinder do ...@@ -278,33 +278,38 @@ RSpec.describe MergeRequestsFinder do
end end
describe 'draft state' do describe 'draft state' do
let!(:wip_merge_request1) { create(:merge_request, :simple, author: user, source_project: project5, target_project: project5, title: 'WIP: thing') } shared_examples 'draft MRs filtering' do |draft_param_key, draft_param_value, title_prefix, draft_only|
let!(:wip_merge_request2) { create(:merge_request, :simple, author: user, source_project: project6, target_project: project6, title: 'wip thing') } it "filters by #{draft_param_key} => #{draft_param_value}" do
let!(:wip_merge_request3) { create(:merge_request, :simple, author: user, source_project: project1, target_project: project1, title: '[wip] thing') } merge_request1.reload.update!(title: "#{title_prefix} #{merge_request1.title}")
let!(:wip_merge_request4) { create(:merge_request, :simple, author: user, source_project: project1, target_project: project2, title: 'wip: thing') }
let!(:draft_merge_request1) { create(:merge_request, :simple, author: user, source_branch: 'draft1', source_project: project5, target_project: project5, title: 'Draft: thing') } params = { draft_param_key => draft_param_value }
let!(:draft_merge_request2) { create(:merge_request, :simple, author: user, source_branch: 'draft2', source_project: project6, target_project: project6, title: '[draft] thing') }
let!(:draft_merge_request3) { create(:merge_request, :simple, author: user, source_branch: 'draft3', source_project: project1, target_project: project1, title: '(draft) thing') }
let!(:draft_merge_request4) { create(:merge_request, :simple, author: user, source_branch: 'draft4', source_project: project1, target_project: project2, title: 'Draft - thing') }
[:wip, :draft].each do |draft_param_key|
it "filters by #{draft_param_key}" do
params = { draft_param_key => 'yes' }
merge_requests = described_class.new(user, params).execute merge_requests = described_class.new(user, params).execute
expect(merge_requests).to contain_exactly( if draft_only
merge_request4, merge_request5, wip_merge_request1, wip_merge_request2, wip_merge_request3, wip_merge_request4, expect(merge_requests).to contain_exactly(merge_request1, merge_request4, merge_request5)
draft_merge_request1, draft_merge_request2, draft_merge_request3, draft_merge_request4 else
) expect(merge_requests).to contain_exactly(merge_request2, merge_request3)
end
end end
end
it "filters by not #{draft_param_key}" do {
params = { draft_param_key => 'no' } wip: ["WIP:", "wip", "[wip]"],
draft: ["Draft:", "Draft -", "[Draft]", "(Draft)"]
merge_requests = described_class.new(user, params).execute }.each do |draft_param_key, title_prefixes|
title_prefixes.each do |title_prefix|
it_behaves_like 'draft MRs filtering', draft_param_key, 1, title_prefix, true
it_behaves_like 'draft MRs filtering', draft_param_key, '1', title_prefix, true
it_behaves_like 'draft MRs filtering', draft_param_key, true, title_prefix, true
it_behaves_like 'draft MRs filtering', draft_param_key, 'true', title_prefix, true
it_behaves_like 'draft MRs filtering', draft_param_key, 'yes', title_prefix, true
expect(merge_requests).to contain_exactly(merge_request1, merge_request2, merge_request3) it_behaves_like 'draft MRs filtering', draft_param_key, 0, title_prefix, false
it_behaves_like 'draft MRs filtering', draft_param_key, '0', title_prefix, false
it_behaves_like 'draft MRs filtering', draft_param_key, false, title_prefix, false
it_behaves_like 'draft MRs filtering', draft_param_key, 'false', title_prefix, false
it_behaves_like 'draft MRs filtering', draft_param_key, 'no', title_prefix, false
end end
it "returns all items if no valid #{draft_param_key} param exists" do it "returns all items if no valid #{draft_param_key} param exists" do
...@@ -313,43 +318,41 @@ RSpec.describe MergeRequestsFinder do ...@@ -313,43 +318,41 @@ RSpec.describe MergeRequestsFinder do
merge_requests = described_class.new(user, params).execute merge_requests = described_class.new(user, params).execute
expect(merge_requests).to contain_exactly( expect(merge_requests).to contain_exactly(
merge_request1, merge_request2, merge_request3, merge_request4, merge_request1, merge_request2, merge_request3, merge_request4, merge_request5
merge_request5, wip_merge_request1, wip_merge_request2, wip_merge_request3, wip_merge_request4,
draft_merge_request1, draft_merge_request2, draft_merge_request3, draft_merge_request4
) )
end end
end end
end
context 'filter by deployment' do context 'filter by deployment' do
let_it_be(:project_with_repo) { create(:project, :repository) } let_it_be(:project_with_repo) { create(:project, :repository) }
it 'returns the relevant merge requests' do it 'returns the relevant merge requests' do
deployment1 = create( deployment1 = create(
:deployment, :deployment,
project: project_with_repo, project: project_with_repo,
sha: project_with_repo.commit.id sha: project_with_repo.commit.id
) )
deployment2 = create( deployment2 = create(
:deployment, :deployment,
project: project_with_repo, project: project_with_repo,
sha: project_with_repo.commit.id sha: project_with_repo.commit.id
) )
deployment1.link_merge_requests(MergeRequest.where(id: [merge_request1.id, merge_request2.id])) deployment1.link_merge_requests(MergeRequest.where(id: [merge_request1.id, merge_request2.id]))
deployment2.link_merge_requests(MergeRequest.where(id: merge_request3.id)) deployment2.link_merge_requests(MergeRequest.where(id: merge_request3.id))
params = { deployment_id: deployment1.id } params = { deployment_id: deployment1.id }
merge_requests = described_class.new(user, params).execute merge_requests = described_class.new(user, params).execute
expect(merge_requests).to contain_exactly(merge_request1, merge_request2) expect(merge_requests).to contain_exactly(merge_request1, merge_request2)
end end
context 'when a deployment does not contain any merge requests' do context 'when a deployment does not contain any merge requests' do
it 'returns an empty result' do it 'returns an empty result' do
params = { deployment_id: create(:deployment, project: project_with_repo, sha: project_with_repo.commit.sha).id } params = { deployment_id: create(:deployment, project: project_with_repo, sha: project_with_repo.commit.sha).id }
merge_requests = described_class.new(user, params).execute merge_requests = described_class.new(user, params).execute
expect(merge_requests).to be_empty expect(merge_requests).to be_empty
end
end end
end end
end end
......
...@@ -172,6 +172,28 @@ RSpec.describe Resolvers::MergeRequestsResolver do ...@@ -172,6 +172,28 @@ RSpec.describe Resolvers::MergeRequestsResolver do
end end
end end
context 'with draft argument' do
before do
merge_request_4.update!(title: MergeRequest.wip_title(merge_request_4.title))
end
context 'with draft: true argument' do
it 'takes one argument' do
result = resolve_mr(project, draft: true)
expect(result).to contain_exactly(merge_request_4)
end
end
context 'with draft: false argument' do
it 'takes one argument' do
result = resolve_mr(project, draft: false)
expect(result).not_to contain_exactly(merge_request_1, merge_request_2, merge_request_3, merge_request_5, merge_request_6)
end
end
end
context 'with label argument' do context 'with label argument' do
let_it_be(:label) { merge_request_6.labels.first } let_it_be(:label) { merge_request_6.labels.first }
let_it_be(:with_label) { create(:labeled_merge_request, :closed, labels: [label], **common_attrs) } let_it_be(:with_label) { create(:labeled_merge_request, :closed, labels: [label], **common_attrs) }
......
...@@ -289,6 +289,7 @@ RSpec.describe GitlabSchema.types['Project'] do ...@@ -289,6 +289,7 @@ RSpec.describe GitlabSchema.types['Project'] do
:source_branches, :source_branches,
:target_branches, :target_branches,
:state, :state,
:draft,
:labels, :labels,
:before, :before,
:after, :after,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment