summaryrefslogtreecommitdiff
path: root/app/serializers/stage_entity.rb
blob: 00e6d32ee3a9b21dd61e9e3ce04326e2aa7c2daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

class StageEntity < Grape::Entity
  include RequestAwareEntity

  expose :name

  expose :title do |stage|
    "#{stage.name}: #{detailed_status.label}"
  end

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

  expose :latest_statuses,
    if: -> (_, opts) { opts[:details] },
    with: JobEntity do |stage|
    latest_statuses
  end

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

  expose :path do |stage|
    project_pipeline_path(
      stage.pipeline.project,
      stage.pipeline,
      anchor: stage.name)
  end

  expose :dropdown_path do |stage|
    stage_project_pipeline_path(
      stage.pipeline.project,
      stage.pipeline,
      stage: stage.name,
      format: :json)
  end

  private

  alias_method :stage, :object

  def detailed_status
    stage.detailed_status(request.current_user)
  end

  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
end