summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/seed/stage.rb
blob: 42970d8f5ac4c1f12b6827b9b89fc24f981ff9b2 (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
57
58
59
60
61
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Seed
        class Stage < Seed::Base
          include Gitlab::Utils::StrongMemoize

          delegate :size, to: :seeds
          delegate :dig, to: :seeds

          def initialize(context, attributes, previous_stages)
            @context = context
            @pipeline = context.pipeline
            @attributes = attributes
            @previous_stages = previous_stages

            @builds = attributes.fetch(:builds).map do |attributes|
              Seed::Build.new(context, attributes, previous_stages + [self])
            end
          end

          def attributes
            { name: @attributes.fetch(:name),
              position: @attributes.fetch(:index),
              pipeline: @pipeline,
              project: @pipeline.project,
              partition_id: @pipeline.partition_id }
          end

          def seeds
            @builds.select(&:included?)
          end
          strong_memoize_attr :seeds

          def errors
            @builds.flat_map(&:errors).compact
          end
          strong_memoize_attr :errors

          def seeds_names
            seeds.map(&:name).to_set
          end
          strong_memoize_attr :seeds_names

          def included?
            seeds.any?
          end

          def to_resource
            ::Ci::Stage.new(attributes).tap do |stage|
              stage.statuses = seeds.map(&:to_resource)
            end
          end
          strong_memoize_attr :to_resource
        end
      end
    end
  end
end