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