2020-05-13 11:08:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Resolvers
|
|
|
|
module DesignManagement
|
|
|
|
class VersionsResolver < BaseResolver
|
|
|
|
type Types::DesignManagement::VersionType.connection_type, null: false
|
|
|
|
|
|
|
|
alias_method :design_or_collection, :object
|
|
|
|
|
2020-11-03 19:09:12 -05:00
|
|
|
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
|
|
|
|
|
2020-12-14 16:09:47 -05:00
|
|
|
extras [:parent]
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :earlier_or_equal_to_sha, GraphQL::Types::String,
|
2020-05-13 11:08:23 -04:00
|
|
|
as: :sha,
|
|
|
|
required: false,
|
2021-08-17 20:11:18 -04:00
|
|
|
description: 'SHA256 of the most recent acceptable version.'
|
2020-05-13 11:08:23 -04:00
|
|
|
|
2020-11-03 19:09:12 -05:00
|
|
|
argument :earlier_or_equal_to_id, VersionID,
|
2020-05-13 11:08:23 -04:00
|
|
|
as: :id,
|
|
|
|
required: false,
|
2021-08-17 20:11:18 -04:00
|
|
|
description: 'Global ID of the most recent acceptable version.'
|
2020-05-13 11:08:23 -04:00
|
|
|
|
|
|
|
# This resolver has a custom singular resolver
|
|
|
|
def self.single
|
|
|
|
::Resolvers::DesignManagement::VersionInCollectionResolver
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(parent: nil, id: nil, sha: nil)
|
2020-11-03 19:09:12 -05:00
|
|
|
# TODO: remove this line when the compatibility layer is removed
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
|
|
|
|
id &&= VersionID.coerce_isolated_input(id)
|
2020-05-13 11:08:23 -04:00
|
|
|
version = cutoff(parent, id, sha)
|
|
|
|
|
|
|
|
raise ::Gitlab::Graphql::Errors::ResourceNotAvailable, 'cutoff not found' unless version.present?
|
|
|
|
|
|
|
|
if version == :unconstrained
|
|
|
|
find
|
|
|
|
else
|
|
|
|
find(earlier_or_equal_to: version)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Find the most recent version that the client will accept
|
|
|
|
def cutoff(parent, id, sha)
|
|
|
|
if sha.present? || id.present?
|
|
|
|
specific_version(id, sha)
|
|
|
|
elsif at_version = at_version_arg(parent)
|
|
|
|
by_id(at_version)
|
|
|
|
else
|
|
|
|
:unconstrained
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 19:09:12 -05:00
|
|
|
def specific_version(gid, sha)
|
2020-05-13 11:08:23 -04:00
|
|
|
find(sha: sha, version_id: gid&.model_id).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(**params)
|
|
|
|
::DesignManagement::VersionsFinder
|
|
|
|
.new(design_or_collection, current_user, params)
|
|
|
|
.execute
|
2021-05-14 08:10:58 -04:00
|
|
|
.with_author
|
2020-05-13 11:08:23 -04:00
|
|
|
end
|
|
|
|
|
2020-11-03 19:09:12 -05:00
|
|
|
def by_id(gid)
|
|
|
|
::Gitlab::Graphql::Lazy.force(GitlabSchema.find_by_gid(gid))
|
2020-05-13 11:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find an `at_version` argument passed to a parent node.
|
|
|
|
#
|
|
|
|
# If one is found, then a design collection further up the AST
|
|
|
|
# has been filtered to reflect designs at that version, and so
|
|
|
|
# for consistency we should only present versions up to the given
|
|
|
|
# version here.
|
|
|
|
def at_version_arg(parent)
|
2020-11-03 19:09:12 -05:00
|
|
|
# TODO: remove coercion when the compatibility layer is removed
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
|
|
|
|
version_id = ::Gitlab::Graphql::FindArgumentInParent.find(parent, :at_version, limit_depth: 4)
|
|
|
|
version_id &&= VersionID.coerce_isolated_input(version_id)
|
|
|
|
version_id
|
2020-05-13 11:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|