BigW Consortium Gitlab

pipeline_serializer.rb 1.09 KB
Newer Older
1
class PipelineSerializer < BaseSerializer
2
  InvalidResourceError = Class.new(StandardError)
3

4
  entity PipelineDetailsEntity
5

6
  def with_pagination(request, response)
7
    tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
8 9 10
  end

  def paginated?
11
    @paginator.present?
12 13
  end

14
  def represent(resource, opts = {})
15
    if resource.is_a?(ActiveRecord::Relation)
16

17
      resource = resource.preload([
Kamil Trzcinski committed
18 19
        :retryable_builds,
        :cancelable_statuses,
Kamil Trzcinski committed
20 21
        :trigger_requests,
        :project,
22 23 24
        :manual_actions,
        :artifacts,
        { pending_builds: :project }
Kamil Trzcinski committed
25
      ])
26 27 28
    end

    if paginated?
29
      super(@paginator.paginate(resource), opts)
30 31 32 33
    else
      super(resource, opts)
    end
  end
34 35

  def represent_status(resource)
36
    return {} unless resource.present?
37

38
    data = represent(resource, { only: [{ details: [:status] }] })
Shinya Maeda committed
39
    data.dig(:details, :status) || {}
40
  end
41 42 43 44 45 46 47

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

    data = represent(resource, { only: [{ details: [:stages] }] })
    data.dig(:details, :stages) || []
  end
48
end