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

79 lines
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 PipelineSerializer < BaseSerializer
2018-03-17 18:26:18 +05:30
include WithPagination
2017-09-10 17:25:29 +05:30
entity PipelineDetailsEntity
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def represent(resource, opts = {})
if resource.is_a?(ActiveRecord::Relation)
2020-03-13 15:44:24 +05:30
# 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
2019-07-31 22:56:46 +05:30
resource = resource.preload(preloaded_relations)
2017-08-17 22:00:37 +05:30
end
if paginated?
2018-11-08 19:23:39 +05:30
resource = paginator.paginate(resource)
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
if opts.delete(:preload)
resource = Gitlab::Ci::Pipeline::Preloader.preload!(resource)
end
super(resource, opts)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def represent_status(resource)
return {} unless resource.present?
data = represent(resource, { only: [{ details: [:status] }] })
data.dig(:details, :status) || {}
end
def represent_stages(resource)
return {} unless resource.present?
2018-11-08 19:23:39 +05:30
data = represent(resource, { only: [{ details: [:stages] }], preload: true })
2017-08-17 22:00:37 +05:30
data.dig(:details, :stages) || []
end
2019-07-31 22:56:46 +05:30
private
def preloaded_relations
[
2020-01-01 13:55:28 +05:30
:latest_statuses_ordered_by_stage,
2020-03-13 15:44:24 +05:30
:project,
2019-07-31 22:56:46 +05:30
:stages,
2020-01-01 13:55:28 +05:30
{
failed_builds: %i(project metadata)
},
2019-07-31 22:56:46 +05:30
:retryable_builds,
:cancelable_statuses,
:trigger_requests,
:manual_actions,
:scheduled_actions,
:artifacts,
2020-01-01 13:55:28 +05:30
:user,
2020-04-08 14:13:33 +05:30
{
merge_request: {
source_project: [:route, { namespace: :route }],
target_project: [:route, { namespace: :route }]
}
},
2019-07-31 22:56:46 +05:30
{
pending_builds: :project,
project: [:route, { namespace: :route }],
artifacts: {
2020-03-13 15:44:24 +05:30
project: [:route, { namespace: :route }],
job_artifacts_archive: []
2019-07-31 22:56:46 +05:30
}
2019-12-21 20:55:43 +05:30
},
{ triggered_by_pipeline: [:project, :user] },
{ triggered_pipelines: [:project, :user] }
2019-07-31 22:56:46 +05:30
]
end
2017-08-17 22:00:37 +05:30
end