stage_entity.rb 1.13 KB
Newer Older
1 2
# frozen_string_literal: true

3
class StageEntity < Grape::Entity
Kamil Trzciński's avatar
Kamil Trzciński committed
4 5
  include RequestAwareEntity

6
  expose :name
7

8 9
  expose :title do |stage|
    "#{stage.name}: #{detailed_status.label}"
10
  end
Kamil Trzciński's avatar
Kamil Trzciński committed
11

12 13 14 15
  expose :groups,
    if: -> (_, opts) { opts[:grouped] },
    with: JobGroupEntity

Kamil Trzciński's avatar
Kamil Trzciński committed
16 17 18 19 20 21
  expose :latest_statuses,
    if: -> (_, opts) { opts[:details] },
    with: JobEntity do |stage|
    latest_statuses
  end

22
  expose :detailed_status, as: :status, with: DetailedStatusEntity
23

24
  expose :path do |stage|
25
    project_pipeline_path(
Kamil Trzciński's avatar
Kamil Trzciński committed
26
      stage.pipeline.project,
27
      stage.pipeline,
Kamil Trzciński's avatar
Kamil Trzciński committed
28 29
      anchor: stage.name)
  end
30 31

  expose :dropdown_path do |stage|
32
    stage_project_pipeline_path(
33 34 35 36 37 38 39 40 41 42 43
      stage.pipeline.project,
      stage.pipeline,
      stage: stage.name,
      format: :json)
  end

  private

  alias_method :stage, :object

  def detailed_status
Fatih Acet's avatar
Fatih Acet committed
44
    stage.detailed_status(request.current_user)
45
  end
Kamil Trzciński's avatar
Kamil Trzciński committed
46 47 48 49 50 51 52 53 54 55

  def grouped_statuses
    @grouped_statuses ||= stage.statuses.latest_ordered.group_by(&:status)
  end

  def latest_statuses
    HasStatus::ORDERED_STATUSES.map do |ordered_status|
      grouped_statuses.fetch(ordered_status, [])
    end.flatten
  end
Kamil Trzciński's avatar
Kamil Trzciński committed
56
end