Read about our upcoming Code of Conduct on this issue

seed.rb 1.49 KB
Newer Older
1
2
3
4
module Gitlab
  module Ci
    module Stage
      class Seed
5
6
        include ::Gitlab::Utils::StrongMemoize

7
        attr_reader :pipeline
8

9
        delegate :project, to: :pipeline
10
        delegate :size, to: :@jobs
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

        def initialize(pipeline, stage, jobs)
          @pipeline = pipeline
          @stage = { name: stage }
          @jobs = jobs.to_a.dup
        end

        def user=(current_user)
          @jobs.map! do |attributes|
            attributes.merge(user: current_user)
          end
        end

        def stage
          @stage.merge(project: project)
        end

        def builds
          trigger = pipeline.trigger_requests.first

          @jobs.map do |attributes|
            attributes.merge(project: project,
                             ref: pipeline.ref,
                             tag: pipeline.tag,
35
                             trigger_request: trigger,
Shinya Maeda's avatar
Shinya Maeda committed
36
                             protected: protected_ref?)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
          end
        end

        def create!
          pipeline.stages.create!(stage).tap do |stage|
            builds_attributes = builds.map do |attributes|
              attributes.merge(stage_id: stage.id)
            end

            pipeline.builds.create!(builds_attributes).each do |build|
              yield build if block_given?
            end
          end
        end
Shinya Maeda's avatar
Shinya Maeda committed
51
52
53
54

        private

        def protected_ref?
55
56
57
          strong_memoize(:protected_ref) do
            project.protected_for?(pipeline.ref)
          end
Shinya Maeda's avatar
Shinya Maeda committed
58
        end
59
60
61
62
      end
    end
  end
end