debian-mirror-gitlab/app/serializers/deployment_entity.rb

73 lines
2.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class DeploymentEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :iid
expose :sha
expose :ref do
expose :name do |deployment|
deployment.ref
end
expose :ref_path do |deployment|
2017-09-10 17:25:29 +05:30
project_tree_path(deployment.project, id: deployment.ref)
2017-08-17 22:00:37 +05:30
end
end
expose :created_at
2019-12-04 20:38:33 +05:30
expose :deployed_at
2017-08-17 22:00:37 +05:30
expose :tag
expose :last?
2019-12-04 20:38:33 +05:30
expose :deployed_by, as: :user, using: UserEntity
2019-09-04 21:01:54 +05:30
2019-12-04 20:38:33 +05:30
expose :deployable, if: -> (deployment) { deployment.deployable.present? } do |deployment, opts|
2019-09-04 21:01:54 +05:30
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
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? }
2020-01-01 13:55:28 +05:30
expose :playable_build, expose_nil: false, if: -> (*) { include_details? && can_create_deployment? } do |deployment, options|
JobEntity.represent(deployment.playable_build, options.merge(only: [:play_path, :retry_path]))
end
2019-03-02 22:35:43 +05:30
2020-03-13 15:44:24 +05:30
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-12-04 20:38:33 +05:30
2019-03-02 22:35:43 +05:30
private
2019-09-04 21:01:54 +05:30
def include_details?
options.fetch(:deployment_details, true)
end
2019-03-02 22:35:43 +05:30
def can_create_deployment?
2020-01-01 13:55:28 +05:30
can?(request.current_user, :create_deployment, project)
2019-03-02 22:35:43 +05:30
end
2019-09-04 21:01:54 +05:30
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.
#
2020-01-01 13:55:28 +05:30
can?(request.current_user, :read_build, project)
end
def project
request.try(:project) || options[:project]
2019-09-04 21:01:54 +05:30
end
2017-08-17 22:00:37 +05:30
end