2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-03 07:54:10 -04:00
|
|
|
class DeploymentEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
|
|
|
|
|
|
|
expose :id
|
|
|
|
expose :iid
|
|
|
|
expose :sha
|
|
|
|
|
|
|
|
expose :ref do
|
|
|
|
expose :name do |deployment|
|
|
|
|
deployment.ref
|
|
|
|
end
|
2017-04-24 16:34:36 -04:00
|
|
|
|
|
|
|
expose :ref_path do |deployment|
|
2017-06-29 13:06:35 -04:00
|
|
|
project_tree_path(deployment.project, id: deployment.ref)
|
2017-04-24 16:34:36 -04:00
|
|
|
end
|
2016-11-03 07:54:10 -04:00
|
|
|
end
|
|
|
|
|
2017-03-28 06:48:53 -04:00
|
|
|
expose :created_at
|
2016-11-03 07:54:10 -04:00
|
|
|
expose :tag
|
|
|
|
expose :last?
|
2017-04-24 16:34:36 -04:00
|
|
|
expose :user, using: UserEntity
|
2019-05-06 11:30:57 -04:00
|
|
|
|
2019-05-10 13:02:00 -04:00
|
|
|
expose :deployable do |deployment, opts|
|
|
|
|
deployment.deployable.yield_self do |deployable|
|
|
|
|
if include_details?
|
|
|
|
JobEntity.represent(deployable, opts)
|
|
|
|
elsif can_read_deployables?
|
|
|
|
{ name: deployable.name,
|
|
|
|
build_path: project_job_path(deployable.project, deployable) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-06 11:30:57 -04:00
|
|
|
expose :commit, using: CommitEntity, if: -> (*) { include_details? }
|
|
|
|
expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? }
|
|
|
|
expose :scheduled_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? }
|
2019-02-08 07:53:35 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-05-06 11:30:57 -04:00
|
|
|
def include_details?
|
|
|
|
options.fetch(:deployment_details, true)
|
|
|
|
end
|
|
|
|
|
2019-02-08 07:53:35 -05:00
|
|
|
def can_create_deployment?
|
|
|
|
can?(request.current_user, :create_deployment, request.project)
|
|
|
|
end
|
2019-05-10 13:02:00 -04:00
|
|
|
|
|
|
|
def can_read_deployables?
|
|
|
|
##
|
|
|
|
# We intentionally do not check `:read_build, deployment.deployable`
|
|
|
|
# because it triggers a policy evaluation that involves multiple
|
|
|
|
# Gitaly calls that might not be cached.
|
|
|
|
#
|
|
|
|
can?(request.current_user, :read_build, request.project)
|
|
|
|
end
|
2016-11-03 07:54:10 -04:00
|
|
|
end
|