stage_entity.rb 1.53 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 23 24 25 26 27
  expose :retried,
         if: -> (_, opts) { opts[:retried] },
         with: JobEntity do |stage|
    retried_statuses
  end

28
  expose :detailed_status, as: :status, with: DetailedStatusEntity
29

30
  expose :path do |stage|
31
    project_pipeline_path(
Kamil Trzciński's avatar
Kamil Trzciński committed
32
      stage.pipeline.project,
33
      stage.pipeline,
Kamil Trzciński's avatar
Kamil Trzciński committed
34 35
      anchor: stage.name)
  end
36 37

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

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

57 58 59 60
  def grouped_retried_statuses
    @grouped_retried_statuses ||= stage.statuses.retried_ordered.group_by(&:status)
  end

Kamil Trzciński's avatar
Kamil Trzciński committed
61 62 63 64 65
  def latest_statuses
    HasStatus::ORDERED_STATUSES.map do |ordered_status|
      grouped_statuses.fetch(ordered_status, [])
    end.flatten
  end
66 67 68 69 70 71

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