pipeline_serializer.rb 1.04 KB
Newer Older
1 2
# frozen_string_literal: true

Kamil Trzciński's avatar
Kamil Trzciński committed
3
class PipelineSerializer < BaseSerializer
4
  include WithPagination
5
  entity PipelineDetailsEntity
6

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

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

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 Trzciński's avatar
Kamil Trzciński committed
45
end