pipeline_serializer.rb 1.12 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1
class PipelineSerializer < BaseSerializer
2
  include WithPagination
3
  entity PipelineDetailsEntity
4

5
  def represent(resource, opts = {})
6
    if resource.is_a?(ActiveRecord::Relation)
7
      resource = resource.preload([
8
        :stages,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
9 10
        :retryable_builds,
        :cancelable_statuses,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
11 12
        :trigger_requests,
        :project,
13 14
        { triggered_by_pipeline: [:project, :user] },
        { triggered_pipelines: [:project, :user] },
15 16 17
        :manual_actions,
        :artifacts,
        { pending_builds: :project }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
18
      ])
19 20 21
    end

    if paginated?
22
      resource = paginator.paginate(resource)
23
    end
24

25 26
    if opts.delete(:preload)
      resource = Gitlab::Ci::Pipeline::Preloader.preload!(resource)
27
    end
28 29

    super(resource, opts)
30
  end
31 32

  def represent_status(resource)
33
    return {} unless resource.present?
34

35
    data = represent(resource, { only: [{ details: [:status] }] })
Shinya Maeda's avatar
Shinya Maeda committed
36
    data.dig(:details, :status) || {}
37
  end
38 39 40 41

  def represent_stages(resource)
    return {} unless resource.present?

42
    data = represent(resource, { only: [{ details: [:stages] }], preload: true })
43 44
    data.dig(:details, :stages) || []
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
45
end