Commit 8b103d17 authored by Dmytro Zaporozhets (DZ)'s avatar Dmytro Zaporozhets (DZ)

Merge branch 'handle_timeframe_arguments' into 'master'

Handle timeframe argument

See merge request gitlab-org/gitlab!63509
parents 12247a88 e40eb46d
...@@ -39,4 +39,12 @@ module TimeFrameArguments ...@@ -39,4 +39,12 @@ module TimeFrameArguments
raise Gitlab::Graphql::Errors::ArgumentError, error_message raise Gitlab::Graphql::Errors::ArgumentError, error_message
end end
end end
def transform_timeframe_parameters(args)
if args[:timeframe]
args[:timeframe].transform_keys { |k| :"#{k}_date" }
else
args.slice(:start_date, :end_date)
end
end
end end
...@@ -44,15 +44,7 @@ module Resolvers ...@@ -44,15 +44,7 @@ module Resolvers
title: args[:title], title: args[:title],
search_title: args[:search_title], search_title: args[:search_title],
containing_date: args[:containing_date] containing_date: args[:containing_date]
}.merge!(timeframe_parameters(args)).merge!(parent_id_parameters(args)) }.merge!(transform_timeframe_parameters(args)).merge!(parent_id_parameters(args))
end
def timeframe_parameters(args)
if args[:timeframe]
args[:timeframe].transform_keys { |k| :"#{k}_date" }
else
args.slice(:start_date, :end_date)
end
end end
def parent def parent
......
...@@ -11,10 +11,10 @@ module Resolvers ...@@ -11,10 +11,10 @@ module Resolvers
private private
def set_relative_param(args) def relative_param
args[:child_id] = parent.id if parent return {} unless parent
args { child_id: parent.id }
end end
end end
end end
...@@ -104,13 +104,13 @@ module Resolvers ...@@ -104,13 +104,13 @@ module Resolvers
transformed[:group_id] = group transformed[:group_id] = group
transformed[:iids] ||= [args[:iid]].compact transformed[:iids] ||= [args[:iid]].compact
set_relative_param(transformed) transformed.merge(transform_timeframe_parameters(args)).merge(relative_param)
end end
def set_relative_param(args) def relative_param
args[:parent_id] = parent.id if parent return {} unless parent
args { parent_id: parent.id }
end end
# `resolver_object` refers to the object we're currently querying on, and is usually a `Group` # `resolver_object` refers to the object we're currently querying on, and is usually a `Group`
......
...@@ -58,10 +58,8 @@ module Resolvers ...@@ -58,10 +58,8 @@ module Resolvers
iid: args[:iid], iid: args[:iid],
iteration_cadence_ids: args[:iteration_cadence_ids], iteration_cadence_ids: args[:iteration_cadence_ids],
state: args[:state] || 'all', state: args[:state] || 'all',
start_date: args.dig(:timeframe, :start) || args[:start_date],
end_date: args.dig(:timeframe, :end) || args[:end_date],
search_title: args[:title] search_title: args[:title]
} }.merge(transform_timeframe_parameters(args))
end end
def parent def parent
......
...@@ -72,6 +72,14 @@ RSpec.describe Resolvers::EpicsResolver do ...@@ -72,6 +72,14 @@ RSpec.describe Resolvers::EpicsResolver do
expect(epics).to match_array([epic1, epic2]) expect(epics).to match_array([epic1, epic2])
end end
end end
context 'when timeframe start and end are present' do
it 'returns epics within timeframe' do
epics = resolve_epics(timeframe: { start: '2019-08-13', end: '2019-08-21' })
expect(epics).to match_array([epic1, epic2])
end
end
end end
context 'with state' do context 'with state' do
......
...@@ -15,8 +15,6 @@ RSpec.describe Resolvers::IterationsResolver do ...@@ -15,8 +15,6 @@ RSpec.describe Resolvers::IterationsResolver do
iteration_cadence_ids: nil, iteration_cadence_ids: nil,
parent: nil, parent: nil,
state: nil, state: nil,
start_date: nil,
end_date: nil,
search_title: nil search_title: nil
} }
end end
......
...@@ -7,6 +7,7 @@ RSpec.describe 'Querying a Milestone' do ...@@ -7,6 +7,7 @@ RSpec.describe 'Querying a Milestone' do
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:group) { create(:group) }
let_it_be(:milestone) { create(:milestone, project: project, start_date: '2020-01-01', due_date: '2020-01-15') } let_it_be(:milestone) { create(:milestone, project: project, start_date: '2020-01-01', due_date: '2020-01-15') }
let(:query) do let(:query) do
...@@ -76,4 +77,82 @@ RSpec.describe 'Querying a Milestone' do ...@@ -76,4 +77,82 @@ RSpec.describe 'Querying a Milestone' do
end end
end end
end end
shared_examples 'milestones queried by timeframe' do
describe 'query for milestones by timeframe' do
context 'without start' do
it 'returns error' do
post_graphql(milestones_query(parent, "timeframe: { end: \"#{3.days.ago.to_date}\" }"), current_user: current_user)
expect(graphql_errors).to include(a_hash_including('message' => "Argument 'start' on InputObject 'Timeframe' is required. Expected type Date!"))
end
end
context 'without end date' do
it 'returns error' do
post_graphql(milestones_query(parent, "timeframe: { start: \"#{3.days.ago.to_date}\" }"), current_user: current_user)
expect(graphql_errors).to include(a_hash_including('message' => "Argument 'end' on InputObject 'Timeframe' is required. Expected type Date!"))
end
end
context 'with start and end date' do
it 'does not have errors' do
post_graphql(milestones_query(parent, "timeframe: { start: \"#{3.days.ago.to_date}\", end: \"#{3.days.from_now.to_date}\" }"), current_user: current_user)
expect(graphql_errors).to be_nil
end
end
end
end
context 'group milestones' do
it_behaves_like 'milestones queried by timeframe' do
let(:parent) { group }
end
end
context 'project milestones' do
it_behaves_like 'milestones queried by timeframe' do
let(:parent) { project }
end
end
def project_milestones_query(project, milestone_query)
<<~QUERY
query {
project(fullPath: "#{project.full_path}") {
id,
#{milestone_query}
}
}
QUERY
end
def group_milestones_query(group, milestone_query)
<<~QUERY
query {
group(fullPath: "#{group.full_path}") {
id,
#{milestone_query}
}
}
QUERY
end
def milestones_query(parent, field_queries)
query = <<~Q
milestones(#{field_queries}) {
nodes {
id
}
}
Q
if parent.is_a?(Group)
group_milestones_query(parent, query)
else
project_milestones_query(parent, query)
end
end
end end
...@@ -40,6 +40,13 @@ RSpec.describe 'Milestones through GroupQuery' do ...@@ -40,6 +40,13 @@ RSpec.describe 'Milestones through GroupQuery' do
expect_array_response(milestone_2.to_global_id.to_s, milestone_3.to_global_id.to_s) expect_array_response(milestone_2.to_global_id.to_s, milestone_3.to_global_id.to_s)
end end
it 'fetches milestones between timeframe start and end arguments' do
today = Date.today
fetch_milestones(user, { timeframe: { start: today.to_s, end: (today + 2.days).to_s } })
expect_array_response(milestone_2.to_global_id.to_s, milestone_3.to_global_id.to_s)
end
end end
context 'when filtering by state' do context 'when filtering by state' do
......
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