debian-mirror-gitlab/app/graphql/resolvers/milestones_resolver.rb

87 lines
2.4 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module Resolvers
class MilestonesResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
include TimeFrameArguments
2021-10-27 15:23:28 +05:30
argument :ids, [GraphQL::Types::ID],
2020-10-24 23:57:45 +05:30
required: false,
2021-04-29 21:17:54 +05:30
description: 'Array of global milestone IDs, e.g., `"gid://gitlab/Milestone/1"`.'
2020-10-24 23:57:45 +05:30
argument :state, Types::MilestoneStateEnum,
required: false,
2021-03-08 18:12:59 +05:30
description: 'Filter milestones by state.'
2020-10-24 23:57:45 +05:30
2021-10-27 15:23:28 +05:30
argument :title, GraphQL::Types::String,
2021-01-03 14:25:43 +05:30
required: false,
2021-03-08 18:12:59 +05:30
description: 'The title of the milestone.'
2021-01-03 14:25:43 +05:30
2021-10-27 15:23:28 +05:30
argument :search_title, GraphQL::Types::String,
2021-01-03 14:25:43 +05:30
required: false,
2021-03-08 18:12:59 +05:30
description: 'A search string for the title.'
2021-01-03 14:25:43 +05:30
argument :containing_date, Types::TimeType,
required: false,
2021-03-08 18:12:59 +05:30
description: 'A date that the milestone contains.'
2021-01-03 14:25:43 +05:30
2021-09-30 23:02:18 +05:30
argument :sort, Types::MilestoneSortEnum,
description: 'Sort milestones by this criteria.',
required: false,
default_value: :due_date_asc
2021-01-29 00:20:46 +05:30
type Types::MilestoneType.connection_type, null: true
2020-10-24 23:57:45 +05:30
2021-09-30 23:02:18 +05:30
NON_STABLE_CURSOR_SORTS = %i[expired_last_due_date_asc expired_last_due_date_desc].freeze
2020-10-24 23:57:45 +05:30
def resolve(**args)
validate_timeframe_params!(args)
authorize!
2021-09-30 23:02:18 +05:30
milestones = MilestonesFinder.new(milestones_finder_params(args)).execute
if non_stable_cursor_sort?(args[:sort])
offset_pagination(milestones)
else
milestones
end
2020-10-24 23:57:45 +05:30
end
private
def milestones_finder_params(args)
{
ids: parse_gids(args[:ids]),
state: args[:state] || 'all',
2021-01-03 14:25:43 +05:30
title: args[:title],
search_title: args[:search_title],
2021-09-30 23:02:18 +05:30
sort: args[:sort],
2021-01-03 14:25:43 +05:30
containing_date: args[:containing_date]
2021-09-04 01:27:46 +05:30
}.merge!(transform_timeframe_parameters(args)).merge!(parent_id_parameters(args))
2020-10-24 23:57:45 +05:30
end
def parent
2021-04-29 21:17:54 +05:30
object
2020-10-24 23:57:45 +05:30
end
def parent_id_parameters(args)
raise NotImplementedError
end
# MilestonesFinder does not check for current_user permissions,
# so for now we need to keep it here.
def authorize!
Ability.allowed?(context[:current_user], :read_milestone, parent) || raise_resource_not_available_error!
end
def parse_gids(gids)
gids&.map { |gid| GitlabSchema.parse_gid(gid, expected_type: Milestone).model_id }
end
2021-09-30 23:02:18 +05:30
def non_stable_cursor_sort?(sort)
NON_STABLE_CURSOR_SORTS.include?(sort)
end
2020-10-24 23:57:45 +05:30
end
end