gitlab-org--gitlab-foss/app/serializers/pipeline_serializer.rb

79 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class PipelineSerializer < BaseSerializer
include WithPagination
entity PipelineDetailsEntity
# rubocop: disable CodeReuse/ActiveRecord
def represent(resource, opts = {})
if resource.is_a?(ActiveRecord::Relation)
# We don't want PipelineDetailsEntity to preload the job_artifacts_archive
# because we do it with preloaded_relations in a more optimal way
# if the given resource is a collection of multiple pipelines.
opts[:preload_job_artifacts_archive] = false
resource = resource.preload(preloaded_relations)
end
if paginated?
resource = paginator.paginate(resource)
end
if opts.delete(:preload)
resource = Gitlab::Ci::Pipeline::Preloader.preload!(resource)
end
super(resource, opts)
end
# rubocop: enable CodeReuse/ActiveRecord
2017-03-10 10:16:48 -05:00
def represent_status(resource)
return {} unless resource.present?
2017-03-23 06:37:27 -04:00
2017-03-10 10:16:48 -05:00
data = represent(resource, { only: [{ details: [:status] }] })
2017-03-21 08:15:20 -04:00
data.dig(:details, :status) || {}
2017-03-10 10:16:48 -05:00
end
2017-05-09 00:15:34 -04:00
def represent_stages(resource)
return {} unless resource.present?
data = represent(resource, { only: [{ details: [:stages] }], preload: true })
2017-05-09 00:15:34 -04:00
data.dig(:details, :stages) || []
end
private
def preloaded_relations
[
:latest_statuses_ordered_by_stage,
:project,
:stages,
{
failed_builds: %i(project metadata)
},
:retryable_builds,
:cancelable_statuses,
:trigger_requests,
:manual_actions,
:scheduled_actions,
:artifacts,
:user,
{
merge_request: {
source_project: [:route, { namespace: :route }],
target_project: [:route, { namespace: :route }]
}
},
{
pending_builds: :project,
project: [:route, { namespace: :route }],
artifacts: {
project: [:route, { namespace: :route }],
job_artifacts_archive: []
}
},
{ triggered_by_pipeline: [:project, :user] },
{ triggered_pipelines: [:project, :user] }
]
end
end