summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/seed/stage.rb
blob: 3e5abf8c02b91aa303a2ac4ca2b0812e41b310c6 (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
module Gitlab
  module Ci
    module Pipeline
      module Seed
        class Stage < Seed::Base
          delegate :size, to: :@seeds

          def initialize(pipeline, name, builds)
            @pipeline = pipeline
            @name = name

            @seeds = builds.map do |attributes|
              Seed::Build.new(@pipeline, attributes)
            end
          end

          def user=(current_user)
            @seeds.each { |seed| seed.user = current_user }
          end

          def attributes
            { name: @name, pipeline: @pipeline, project: @pipeline.project }
          end

          # TODO decouple
          #
          def builds_attributes
            @seeds.map(&:attributes)
          end

          def to_resource
            ::Ci::Stage.new(attributes)
          end

          def create!
            to_resource.tap do |stage|
              @seeds.each do |seed|
                seed.to_resource.tap do |build|
                  stage.builds << build
                end
              end

              @pipeline.stages << stage

              stage.save!

              stage.builds.each do |build|
                yield build if block_given?
              end
            end
          end
        end
      end
    end
  end
end