Commit 574bc44b authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '333354-expose-sort-option-when-fetching-milestones-in-graphql' into 'master'

Allow sorting for listing milestones via GraphQL and expose expired attr [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!64572
parents 9eb0d01c 25d2f68c
......@@ -18,6 +18,8 @@ class MilestonesFinder
attr_reader :params
EXPIRED_LAST_SORTS = %i[expired_last_due_date_asc expired_last_due_date_desc].freeze
def initialize(params = {})
@params = params
end
......@@ -70,7 +72,16 @@ class MilestonesFinder
end
def order(items)
sort_by = params[:sort].presence || 'due_date_asc'
items.sort_by_attribute(sort_by)
sort_by = params[:sort].presence || :due_date_asc
if sort_by_expired_last?(sort_by)
items.sort_with_expired_last(sort_by)
else
items.sort_by_attribute(sort_by)
end
end
def sort_by_expired_last?(sort_by)
EXPIRED_LAST_SORTS.include?(sort_by)
end
end
......@@ -25,14 +25,27 @@ module Resolvers
required: false,
description: 'A date that the milestone contains.'
argument :sort, Types::MilestoneSortEnum,
description: 'Sort milestones by this criteria.',
required: false,
default_value: :due_date_asc
type Types::MilestoneType.connection_type, null: true
NON_STABLE_CURSOR_SORTS = %i[expired_last_due_date_asc expired_last_due_date_desc].freeze
def resolve(**args)
validate_timeframe_params!(args)
authorize!
MilestonesFinder.new(milestones_finder_params(args)).execute
milestones = MilestonesFinder.new(milestones_finder_params(args)).execute
if non_stable_cursor_sort?(args[:sort])
offset_pagination(milestones)
else
milestones
end
end
private
......@@ -43,6 +56,7 @@ module Resolvers
state: args[:state] || 'all',
title: args[:title],
search_title: args[:search_title],
sort: args[:sort],
containing_date: args[:containing_date]
}.merge!(transform_timeframe_parameters(args)).merge!(parent_id_parameters(args))
end
......@@ -64,5 +78,9 @@ module Resolvers
def parse_gids(gids)
gids&.map { |gid| GitlabSchema.parse_gid(gid, expected_type: Milestone).model_id }
end
def non_stable_cursor_sort?(sort)
NON_STABLE_CURSOR_SORTS.include?(sort)
end
end
end
# frozen_string_literal: true
module Types
class MilestoneSortEnum < SortEnum
graphql_name 'MilestoneSort'
description 'Values for sorting milestones'
value 'DUE_DATE_ASC', 'Milestone due date by ascending order.', value: :due_date_asc
value 'DUE_DATE_DESC', 'Milestone due date by descending order.', value: :due_date_desc
value 'EXPIRED_LAST_DUE_DATE_ASC', 'Group milestones in this order: non-expired milestones with due dates, non-expired milestones without due dates and expired milestones then sort by due date in ascending order.', value: :expired_last_due_date_asc
value 'EXPIRED_LAST_DUE_DATE_DESC', 'Group milestones in this order: non-expired milestones with due dates, non-expired milestones without due dates and expired milestones then sort by due date in descending order.', value: :expired_last_due_date_desc
end
end
......@@ -26,6 +26,9 @@ module Types
field :state, Types::MilestoneStateEnum, null: false,
description: 'State of the milestone.'
field :expired, GraphQL::BOOLEAN_TYPE, null: false,
description: 'Expired state of the milestone (a milestone is expired when the due date is past the current date). Defaults to `false` when due date has not been set.'
field :web_path, GraphQL::STRING_TYPE, null: false, method: :milestone_path,
description: 'Web path of the milestone.'
......
......@@ -101,6 +101,10 @@ module Milestoneish
due_date && due_date.past?
end
def expired
expired? || false
end
def total_time_spent
@total_time_spent ||= issues.joins(:timelogs).sum(:time_spent) + merge_requests.joins(:timelogs).sum(:time_spent)
end
......
......@@ -120,6 +120,19 @@ class Milestone < ApplicationRecord
sorted.with_order_id_desc
end
def self.sort_with_expired_last(method)
# NOTE: this is a custom ordering of milestones
# to prioritize displaying non-expired milestones and milestones without due dates
sorted = reorder(Arel.sql("(CASE WHEN due_date IS NULL THEN 1 WHEN due_date >= CURRENT_DATE THEN 0 ELSE 2 END) ASC"))
sorted = if method.to_s == 'expired_last_due_date_desc'
sorted.order(due_date: :desc)
else
sorted.order(due_date: :asc)
end
sorted.with_order_id_desc
end
def self.states_count(projects, groups = nil)
return STATE_COUNT_HASH unless projects || groups
......
......@@ -9568,6 +9568,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
| <a id="groupmilestonesincludeancestors"></a>`includeAncestors` | [`Boolean`](#boolean) | Include milestones from all parent groups. |
| <a id="groupmilestonesincludedescendants"></a>`includeDescendants` | [`Boolean`](#boolean) | Include milestones from all subgroups and subprojects. |
| <a id="groupmilestonessearchtitle"></a>`searchTitle` | [`String`](#string) | A search string for the title. |
| <a id="groupmilestonessort"></a>`sort` | [`MilestoneSort`](#milestonesort) | Sort milestones by this criteria. |
| <a id="groupmilestonesstartdate"></a>`startDate` **{warning-solid}** | [`Time`](#time) | **Deprecated** in 13.5. Use timeframe.start. |
| <a id="groupmilestonesstate"></a>`state` | [`MilestoneStateEnum`](#milestonestateenum) | Filter milestones by state. |
| <a id="groupmilestonestimeframe"></a>`timeframe` | [`Timeframe`](#timeframe) | List items overlapping the given timeframe. |
......@@ -10812,6 +10813,7 @@ Represents a milestone.
| <a id="milestonecreatedat"></a>`createdAt` | [`Time!`](#time) | Timestamp of milestone creation. |
| <a id="milestonedescription"></a>`description` | [`String`](#string) | Description of the milestone. |
| <a id="milestoneduedate"></a>`dueDate` | [`Time`](#time) | Timestamp of the milestone due date. |
| <a id="milestoneexpired"></a>`expired` | [`Boolean!`](#boolean) | Expired state of the milestone (a milestone is expired when the due date is past the current date). Defaults to `false` when due date has not been set. |
| <a id="milestonegroupmilestone"></a>`groupMilestone` | [`Boolean!`](#boolean) | Indicates if milestone is at group level. |
| <a id="milestoneid"></a>`id` | [`ID!`](#id) | ID of the milestone. |
| <a id="milestoneiid"></a>`iid` | [`ID!`](#id) | Internal ID of the milestone. |
......@@ -11892,6 +11894,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
| <a id="projectmilestonesids"></a>`ids` | [`[ID!]`](#id) | Array of global milestone IDs, e.g., `"gid://gitlab/Milestone/1"`. |
| <a id="projectmilestonesincludeancestors"></a>`includeAncestors` | [`Boolean`](#boolean) | Also return milestones in the project's parent group and its ancestors. |
| <a id="projectmilestonessearchtitle"></a>`searchTitle` | [`String`](#string) | A search string for the title. |
| <a id="projectmilestonessort"></a>`sort` | [`MilestoneSort`](#milestonesort) | Sort milestones by this criteria. |
| <a id="projectmilestonesstartdate"></a>`startDate` **{warning-solid}** | [`Time`](#time) | **Deprecated** in 13.5. Use timeframe.start. |
| <a id="projectmilestonesstate"></a>`state` | [`MilestoneStateEnum`](#milestonestateenum) | Filter milestones by state. |
| <a id="projectmilestonestimeframe"></a>`timeframe` | [`Timeframe`](#timeframe) | List items overlapping the given timeframe. |
......@@ -14735,6 +14738,25 @@ Representation of whether a GitLab merge request can be merged.
| <a id="mergestrategyenummerge_train"></a>`MERGE_TRAIN` | Use the merge_train merge strategy. |
| <a id="mergestrategyenummerge_when_pipeline_succeeds"></a>`MERGE_WHEN_PIPELINE_SUCCEEDS` | Use the merge_when_pipeline_succeeds merge strategy. |
### `MilestoneSort`
Values for sorting milestones.
| Value | Description |
| ----- | ----------- |
| <a id="milestonesortcreated_asc"></a>`CREATED_ASC` | Created at ascending order. |
| <a id="milestonesortcreated_desc"></a>`CREATED_DESC` | Created at descending order. |
| <a id="milestonesortdue_date_asc"></a>`DUE_DATE_ASC` | Milestone due date by ascending order. |
| <a id="milestonesortdue_date_desc"></a>`DUE_DATE_DESC` | Milestone due date by descending order. |
| <a id="milestonesortexpired_last_due_date_asc"></a>`EXPIRED_LAST_DUE_DATE_ASC` | Group milestones in this order: non-expired milestones with due dates, non-expired milestones without due dates and expired milestones then sort by due date in ascending order. |
| <a id="milestonesortexpired_last_due_date_desc"></a>`EXPIRED_LAST_DUE_DATE_DESC` | Group milestones in this order: non-expired milestones with due dates, non-expired milestones without due dates and expired milestones then sort by due date in descending order. |
| <a id="milestonesortupdated_asc"></a>`UPDATED_ASC` | Updated at ascending order. |
| <a id="milestonesortupdated_desc"></a>`UPDATED_DESC` | Updated at descending order. |
| <a id="milestonesortcreated_asc"></a>`created_asc` **{warning-solid}** | **Deprecated** in 13.5. This was renamed. Use: `CREATED_ASC`. |
| <a id="milestonesortcreated_desc"></a>`created_desc` **{warning-solid}** | **Deprecated** in 13.5. This was renamed. Use: `CREATED_DESC`. |
| <a id="milestonesortupdated_asc"></a>`updated_asc` **{warning-solid}** | **Deprecated** in 13.5. This was renamed. Use: `UPDATED_ASC`. |
| <a id="milestonesortupdated_desc"></a>`updated_desc` **{warning-solid}** | **Deprecated** in 13.5. This was renamed. Use: `UPDATED_DESC`. |
### `MilestoneStateEnum`
Current state of milestone.
......
......@@ -3,46 +3,68 @@
require 'spec_helper'
RSpec.describe MilestonesFinder do
let(:now) { Time.now }
let(:group) { create(:group) }
let(:project_1) { create(:project, namespace: group) }
let(:project_2) { create(:project, namespace: group) }
let!(:milestone_1) { create(:milestone, group: group, title: 'one test', start_date: now - 1.day, due_date: now) }
let!(:milestone_2) { create(:milestone, group: group, start_date: now + 1.day, due_date: now + 2.days) }
let!(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days, due_date: now + 3.days) }
let!(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
it 'returns milestones for projects' do
result = described_class.new(project_ids: [project_1.id, project_2.id], state: 'all').execute
expect(result).to contain_exactly(milestone_3, milestone_4)
end
let_it_be(:now) { Date.current }
let_it_be(:group) { create(:group) }
let_it_be(:project_1) { create(:project, namespace: group) }
let_it_be(:project_2) { create(:project, namespace: group) }
let_it_be(:milestone_2) { create(:milestone, group: group, start_date: now + 1.day, due_date: now + 2.days) }
let_it_be(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
context 'without filters' do
let_it_be(:milestone_1) { create(:milestone, group: group, start_date: now - 1.day, due_date: now) }
let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days) }
let_it_be(:milestone_5) { create(:milestone, group: group, due_date: now - 2.days) }
it 'returns milestones for projects' do
result = described_class.new(project_ids: [project_1.id, project_2.id], state: 'all').execute
expect(result).to contain_exactly(milestone_3, milestone_4)
end
it 'returns milestones for groups' do
result = described_class.new(group_ids: group.id, state: 'all').execute
it 'returns milestones for groups' do
result = described_class.new(group_ids: group.id, state: 'all').execute
expect(result).to contain_exactly(milestone_1, milestone_2)
end
context 'milestones for groups and project' do
let(:result) do
described_class.new(project_ids: [project_1.id, project_2.id], group_ids: group.id, state: 'all').execute
expect(result).to contain_exactly(milestone_5, milestone_1, milestone_2)
end
it 'returns milestones for groups and projects' do
expect(result).to contain_exactly(milestone_1, milestone_2, milestone_3, milestone_4)
context 'milestones for groups and project' do
let(:extra_params) {{}}
let(:result) do
described_class.new({ project_ids: [project_1.id, project_2.id], group_ids: group.id, state: 'all' }.merge(extra_params)).execute
end
it 'returns milestones for groups and projects' do
expect(result).to contain_exactly(milestone_5, milestone_1, milestone_2, milestone_3, milestone_4)
end
it 'orders milestones by due date', :aggregate_failures do
expect(result.first).to eq(milestone_5)
expect(result.second).to eq(milestone_1)
expect(result.third).to eq(milestone_2)
end
context 'when grouping and sorting by expired_last' do
let(:extra_params) { { sort: :expired_last_due_date_asc } }
it 'current milestones are returned first, then milestones without due date followed by expired milestones, sorted by due date in ascending order' do
expect(result).to eq([milestone_1, milestone_2, milestone_4, milestone_3, milestone_5])
end
end
end
it 'orders milestones by due date' do
milestone = create(:milestone, group: group, due_date: now - 2.days)
describe '#find_by' do
it 'finds a single milestone' do
finder = described_class.new(project_ids: [project_1.id], state: 'all')
expect(result.first).to eq(milestone)
expect(result.second).to eq(milestone_1)
expect(result.third).to eq(milestone_2)
expect(finder.find_by(iid: milestone_3.iid)).to eq(milestone_3)
end
end
end
context 'with filters' do
let_it_be(:milestone_1) { create(:milestone, group: group, state: 'closed', title: 'one test', start_date: now - 1.day, due_date: now) }
let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'closed', start_date: now + 2.days, due_date: now + 3.days) }
let(:params) do
{
project_ids: [project_1.id, project_2.id],
......@@ -51,11 +73,6 @@ RSpec.describe MilestonesFinder do
}
end
before do
milestone_1.close
milestone_3.close
end
it 'filters by id' do
params[:ids] = [milestone_1.id, milestone_2.id]
......@@ -118,12 +135,4 @@ RSpec.describe MilestonesFinder do
end
end
end
describe '#find_by' do
it 'finds a single milestone' do
finder = described_class.new(project_ids: [project_1.id], state: 'all')
expect(finder.find_by(iid: milestone_3.iid)).to eq(milestone_3)
end
end
end
......@@ -3,6 +3,7 @@
require 'spec_helper'
RSpec.describe Resolvers::GroupMilestonesResolver do
using RSpec::Parameterized::TableSyntax
include GraphqlHelpers
describe '#resolve' do
......@@ -79,6 +80,24 @@ RSpec.describe Resolvers::GroupMilestonesResolver do
end
end
context 'by sort' do
it 'calls MilestonesFinder with correct parameters' do
expect(MilestonesFinder).to receive(:new)
.with(args(group_ids: group.id, state: 'all', sort: :due_date_desc))
.and_call_original
resolve_group_milestones(sort: :due_date_desc)
end
%i[expired_last_due_date_asc expired_last_due_date_desc].each do |sort_by|
it "uses offset-pagination when sorting by #{sort_by}" do
resolved = resolve_group_milestones(sort: sort_by)
expect(resolved).to be_a(::Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection)
end
end
end
context 'by timeframe' do
context 'when start_date and end_date are present' do
context 'when start date is after end_date' do
......
......@@ -71,6 +71,24 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do
end
end
context 'by sort' do
it 'calls MilestonesFinder with correct parameters' do
expect(MilestonesFinder).to receive(:new)
.with(args(project_ids: project.id, state: 'all', sort: :due_date_desc))
.and_call_original
resolve_project_milestones(sort: :due_date_desc)
end
%i[expired_last_due_date_asc expired_last_due_date_desc].each do |sort_by|
it "uses offset-pagination when sorting by #{sort_by}" do
resolved = resolve_project_milestones(sort: sort_by)
expect(resolved).to be_a(::Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection)
end
end
end
context 'by timeframe' do
context 'when start_date and end_date are present' do
it 'calls MilestonesFinder with correct parameters' do
......
......@@ -9,7 +9,7 @@ RSpec.describe GitlabSchema.types['Milestone'] do
it 'has the expected fields' do
expected_fields = %w[
id iid title description state web_path
id iid title description state expired web_path
due_date start_date created_at updated_at
project_milestone group_milestone subgroup_milestone
stats
......
......@@ -3,10 +3,9 @@
require 'spec_helper'
RSpec.describe Milestone do
let(:user) { create(:user) }
let(:issue) { create(:issue, project: project) }
let(:milestone) { create(:milestone, project: project) }
let(:project) { create(:project, :public) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:issue) { create(:issue, project: project) }
it_behaves_like 'a timebox', :milestone do
describe "#uniqueness_of_title" do
......@@ -92,6 +91,8 @@ RSpec.describe Milestone do
end
describe '.predefined_id?' do
let_it_be(:milestone) { create(:milestone, project: project) }
it 'returns true for a predefined Milestone ID' do
expect(Milestone.predefined_id?(described_class::Upcoming.id)).to be true
end
......@@ -129,6 +130,8 @@ RSpec.describe Milestone do
end
describe "#percent_complete" do
let(:milestone) { create(:milestone, project: project) }
it "does not count open issues" do
milestone.issues << issue
expect(milestone.percent_complete).to eq(0)
......@@ -145,24 +148,22 @@ RSpec.describe Milestone do
end
end
describe '#expired?' do
describe '#expired? and #expired' do
context "expired" do
before do
allow(milestone).to receive(:due_date).and_return(Date.today.prev_year)
end
let(:milestone) { build(:milestone, project: project, due_date: Date.today.prev_year) }
it 'returns true when due_date is in the past' do
it 'returns true when due_date is in the past', :aggregate_failures do
expect(milestone.expired?).to be_truthy
expect(milestone.expired).to eq true
end
end
context "not expired" do
before do
allow(milestone).to receive(:due_date).and_return(Date.today.next_year)
end
let(:milestone) { build(:milestone, project: project, due_date: Date.today.next_year) }
it 'returns false when due_date is in the future' do
it 'returns false when due_date is in the future', :aggregate_failures do
expect(milestone.expired?).to be_falsey
expect(milestone.expired).to eq false
end
end
end
......@@ -180,10 +181,8 @@ RSpec.describe Milestone do
end
describe '#can_be_closed?' do
it { expect(milestone.can_be_closed?).to be_truthy }
end
let_it_be(:milestone) { build(:milestone, project: project) }
describe '#can_be_closed?' do
before do
milestone = create :milestone, project: project
create :closed_issue, milestone: milestone, project: project
......@@ -335,10 +334,10 @@ RSpec.describe Milestone do
it_behaves_like '#for_projects_and_groups'
describe '.upcoming_ids' do
let(:group_1) { create(:group) }
let(:group_2) { create(:group) }
let(:group_3) { create(:group) }
let(:groups) { [group_1, group_2, group_3] }
let_it_be(:group_1) { create(:group) }
let_it_be(:group_2) { create(:group) }
let_it_be(:group_3) { create(:group) }
let_it_be(:groups) { [group_1, group_2, group_3] }
let!(:past_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current - 1.day) }
let!(:current_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current + 1.day) }
......@@ -350,10 +349,10 @@ RSpec.describe Milestone do
let!(:past_milestone_group_3) { create(:milestone, group: group_3, due_date: Time.current - 1.day) }
let(:project_1) { create(:project) }
let(:project_2) { create(:project) }
let(:project_3) { create(:project) }
let(:projects) { [project_1, project_2, project_3] }
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
let_it_be(:project_3) { create(:project) }
let_it_be(:projects) { [project_1, project_2, project_3] }
let!(:past_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current - 1.day) }
let!(:current_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current + 1.day) }
......@@ -451,6 +450,32 @@ RSpec.describe Milestone do
end
end
describe '.sort_with_expired_last' do
let_it_be(:milestone) { create(:milestone, title: 'Due today', due_date: Date.current) }
let_it_be(:milestone_1) { create(:milestone, title: 'Current 1', due_date: Date.current + 1.day) }
let_it_be(:milestone_2) { create(:milestone, title: 'Current 2', due_date: Date.current + 2.days) }
let_it_be(:milestone_3) { create(:milestone, title: 'Without due date') }
let_it_be(:milestone_4) { create(:milestone, title: 'Expired 1', due_date: Date.current - 2.days) }
let_it_be(:milestone_5) { create(:milestone, title: 'Expired 2', due_date: Date.current - 1.day) }
let_it_be(:milestone_6) { create(:milestone, title: 'Without due date2') }
context 'ordering by due_date ascending' do
it 'sorts by due date in ascending order (ties broken by id in desc order)', :aggregate_failures do
expect(milestone_3.id).to be < (milestone_6.id)
expect(described_class.sort_with_expired_last(:expired_last_due_date_asc))
.to eq([milestone, milestone_1, milestone_2, milestone_6, milestone_3, milestone_4, milestone_5])
end
end
context 'ordering by due_date descending' do
it 'sorts by due date in descending order (ties broken by id in desc order)', :aggregate_failures do
expect(milestone_3.id).to be < (milestone_6.id)
expect(described_class.sort_with_expired_last(:expired_last_due_date_desc))
.to eq([milestone_2, milestone_1, milestone, milestone_6, milestone_3, milestone_5, milestone_4])
end
end
end
describe '.sort_by_attribute' do
let_it_be(:milestone_1) { create(:milestone, title: 'Foo') }
let_it_be(:milestone_2) { create(:milestone, title: 'Bar') }
......
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