stage_entity.rb 1.09 KB
Newer Older
1
class StageEntity < Grape::Entity
Kamil Trzciński's avatar
Kamil Trzciński committed
2 3
  include RequestAwareEntity

4
  expose :name
5

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

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

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

20
  expose :detailed_status, as: :status, with: StatusEntity
21

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

  expose :dropdown_path do |stage|
30
    stage_project_pipeline_path(
31 32 33 34 35 36 37 38 39 40 41
      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
42
    stage.detailed_status(request.current_user)
43
  end
Kamil Trzciński's avatar
Kamil Trzciński committed
44 45 46 47 48 49 50 51 52 53

  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
54
end