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
|
|
|
|
|
2020-10-15 08:09:06 -04:00
|
|
|
expose :status
|
2017-03-28 06:48:53 -04:00
|
|
|
expose :created_at
|
2019-08-20 05:28:55 -04:00
|
|
|
expose :deployed_at
|
2016-11-03 07:54:10 -04:00
|
|
|
expose :tag
|
|
|
|
expose :last?
|
2021-12-13 22:13:32 -05:00
|
|
|
expose :last?, as: :is_last
|
2022-04-01 11:08:40 -04:00
|
|
|
expose :tier_in_yaml
|
2021-12-13 22:13:32 -05:00
|
|
|
|
2019-08-20 06:29:16 -04:00
|
|
|
expose :deployed_by, as: :user, using: UserEntity
|
2019-05-06 11:30:57 -04:00
|
|
|
|
2019-08-26 23:51:28 -04:00
|
|
|
expose :deployable, if: -> (deployment) { deployment.deployable.present? } do |deployment, opts|
|
2019-05-10 13:02:00 -04:00
|
|
|
deployment.deployable.yield_self do |deployable|
|
|
|
|
if include_details?
|
2021-12-13 16:14:32 -05:00
|
|
|
Ci::JobEntity.represent(deployable, opts)
|
2019-05-10 13:02:00 -04:00
|
|
|
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? }
|
2021-12-13 16:14:32 -05:00
|
|
|
expose :manual_actions, using: Ci::JobEntity, if: -> (*) { include_details? && can_create_deployment? }
|
|
|
|
expose :scheduled_actions, using: Ci::JobEntity, if: -> (*) { include_details? && can_create_deployment? }
|
2021-05-18 11:10:46 -04:00
|
|
|
expose :playable_build, if: -> (deployment) { include_details? && can_create_deployment? && deployment.playable_build } do |deployment, options|
|
2021-12-13 16:14:32 -05:00
|
|
|
Ci::JobEntity.represent(deployment.playable_build, options.merge(only: [:play_path, :retry_path]))
|
2019-11-29 04:06:31 -05:00
|
|
|
end
|
2019-02-08 07:53:35 -05:00
|
|
|
|
2020-02-14 04:08:43 -05:00
|
|
|
expose :cluster do |deployment, options|
|
|
|
|
# Until data is copied over from deployments.cluster_id, this entity must represent Deployment instead of DeploymentCluster
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/202628
|
|
|
|
DeploymentClusterEntity.represent(deployment, options) unless deployment.cluster.nil?
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
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?
|
2019-11-29 04:06:31 -05:00
|
|
|
can?(request.current_user, :create_deployment, project)
|
2019-02-08 07:53:35 -05:00
|
|
|
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.
|
|
|
|
#
|
2019-11-29 04:06:31 -05:00
|
|
|
can?(request.current_user, :read_build, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
request.try(:project) || options[:project]
|
2019-05-10 13:02:00 -04:00
|
|
|
end
|
2016-11-03 07:54:10 -04:00
|
|
|
end
|
2022-02-18 07:15:31 -05:00
|
|
|
|
|
|
|
DeploymentEntity.prepend_mod
|