diff options
-rw-r--r-- | lib/gitlab/ci/pipeline/seed/base.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/ci/pipeline/seed/stage.rb | 68 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seed.rb | 66 | ||||
-rw-r--r-- | lib/gitlab/ci/yaml_processor.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb (renamed from spec/lib/gitlab/ci/stage/seed_spec.rb) | 2 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 3 |
6 files changed, 90 insertions, 69 deletions
diff --git a/lib/gitlab/ci/pipeline/seed/base.rb b/lib/gitlab/ci/pipeline/seed/base.rb new file mode 100644 index 00000000000..44c85aa6716 --- /dev/null +++ b/lib/gitlab/ci/pipeline/seed/base.rb @@ -0,0 +1,17 @@ +module Gitlab + module Ci + module Pipeline + module Seed + class Base + def attributes + raise NotImplementedError + end + + def excluded? + raise NotImplementedError + end + end + end + end + end +end diff --git a/lib/gitlab/ci/pipeline/seed/stage.rb b/lib/gitlab/ci/pipeline/seed/stage.rb new file mode 100644 index 00000000000..32631a30e8f --- /dev/null +++ b/lib/gitlab/ci/pipeline/seed/stage.rb @@ -0,0 +1,68 @@ +module Gitlab + module Ci + module Pipeline + module Seed + class Stage < Seed::Base + include ::Gitlab::Utils::StrongMemoize + + attr_reader :pipeline + + delegate :project, to: :pipeline + delegate :size, to: :@builds + + def initialize(pipeline, stage, builds) + @pipeline = pipeline + @stage = stage # stage name + @builds = builds.to_a.dup # builds array of hashes + end + + def user=(current_user) + @builds.map! do |attributes| + attributes.merge(user: current_user) + end + end + + def stage_attributes + { name: @stage, project: project } + end + + def builds_attributes + trigger = pipeline.trigger_requests.first + + @builds.map do |attributes| + attributes.merge(project: project, + ref: pipeline.ref, + tag: pipeline.tag, + trigger_request: trigger, + protected: protected_ref?) + end + end + + def create! + pipeline.stages.build(stage_attributes).tap do |stage| + builds_attributes.each do |build_attributes| + stage.builds.build(build_attributes).tap do |build| + build.pipeline = pipeline + end + end + + stage.save! + + stage.builds.each do |build| + yield build if block_given? + end + end + end + + private + + def protected_ref? + strong_memoize(:protected_ref) do + project.protected_for?(pipeline.ref) + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/stage/seed.rb b/lib/gitlab/ci/stage/seed.rb deleted file mode 100644 index 493af6b0868..00000000000 --- a/lib/gitlab/ci/stage/seed.rb +++ /dev/null @@ -1,66 +0,0 @@ -module Gitlab - module Ci - module Stage - class Seed - include ::Gitlab::Utils::StrongMemoize - - attr_reader :pipeline - - delegate :project, to: :pipeline - delegate :size, to: :@builds - - def initialize(pipeline, stage, builds) - @pipeline = pipeline - @stage = stage # stage name - @builds = builds.to_a.dup # builds array of hashes - end - - def user=(current_user) - @builds.map! do |attributes| - attributes.merge(user: current_user) - end - end - - def stage_attributes - { name: @stage, project: project } - end - - def builds_attributes - trigger = pipeline.trigger_requests.first - - @builds.map do |attributes| - attributes.merge(project: project, - ref: pipeline.ref, - tag: pipeline.tag, - trigger_request: trigger, - protected: protected_ref?) - end - end - - def create! - pipeline.stages.build(stage_attributes).tap do |stage| - builds_attributes.each do |build_attributes| - stage.builds.build(build_attributes).tap do |build| - build.pipeline = pipeline - end - end - - stage.save! - - stage.builds.each do |build| - yield build if block_given? - end - end - end - - private - - def protected_ref? - strong_memoize(:protected_ref) do - project.protected_for?(pipeline.ref) - end - end - end - end - end -end diff --git a/lib/gitlab/ci/yaml_processor.rb b/lib/gitlab/ci/yaml_processor.rb index a7285ac8f9d..60bd3b0d1b7 100644 --- a/lib/gitlab/ci/yaml_processor.rb +++ b/lib/gitlab/ci/yaml_processor.rb @@ -73,7 +73,8 @@ module Gitlab seeds = @stages.uniq.map do |stage| builds = pipeline_stage_builds(stage, pipeline) - Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any? + Gitlab::Ci::Pipeline::Seed::Stage + .new(pipeline, stage, builds) if builds.any? end seeds.compact diff --git a/spec/lib/gitlab/ci/stage/seed_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb index 71e0bc0dbab..d4a72b7c62c 100644 --- a/spec/lib/gitlab/ci/stage/seed_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::Ci::Stage::Seed do +describe Gitlab::Ci::Pipeline::Seed::Stage do let(:pipeline) { create(:ci_empty_pipeline) } let(:builds) do diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 4635f8cfe9d..0ceb25153ab 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -247,7 +247,8 @@ describe Ci::Pipeline, :mailer do end it 'returns preseeded stage seeds object' do - expect(pipeline.stage_seeds).to all(be_a Gitlab::Ci::Stage::Seed) + expect(pipeline.stage_seeds) + .to all(be_a Gitlab::Ci::Pipeline::Seed::Base) expect(pipeline.stage_seeds.count).to eq 1 end end |