diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-05-31 15:40:50 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-05-31 15:40:50 +0300 |
commit | c881425b665b9c0b022dc2e213486aecc320ec7e (patch) | |
tree | 0e6ac6a7a97cad92034973f9861077668c369aa2 | |
parent | 805715cc68aabb6992a63356ec7c19940f52c93a (diff) |
Refine pipeline stages seeds class
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seed.rb | 38 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seeds.rb | 58 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/stage/seed_spec.rb | 47 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/stage/seeds_spec.rb | 62 |
5 files changed, 121 insertions, 86 deletions
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index aa6c94f26c8..5e6c3d029c6 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -51,7 +51,7 @@ module Ci end def stages_for_ref(ref, tag = false, trigger_request = nil) - stages = @stages.map do |stage| + stages = @stages.uniq.map do |stage| builds = builds_for_stage_and_ref(stage, ref, tag, trigger_request) { name: stage, builds_attributes: builds.to_a } if builds.any? diff --git a/lib/gitlab/ci/stage/seed.rb b/lib/gitlab/ci/stage/seed.rb deleted file mode 100644 index 95237bff5d7..00000000000 --- a/lib/gitlab/ci/stage/seed.rb +++ /dev/null @@ -1,38 +0,0 @@ -module Gitlab - module Ci - module Stage - class Seed - attr_reader :name, :builds - - def initialize(name:, builds:) - @name = name - @builds = builds - end - - def pipeline=(pipeline) - trigger_request = pipeline.trigger_requests.first - - @builds.each do |attributes| - attributes.merge!( - pipeline: pipeline, - project: pipeline.project, - ref: pipeline.ref, - tag: pipeline.tag, - trigger_request: trigger_request - ) - end - end - - def user=(current_user) - @builds.each do |attributes| - attributes.merge!(user: current_user) - end - end - - def to_attributes - { name: @name, builds_attributes: @builds } - end - end - end - end -end diff --git a/lib/gitlab/ci/stage/seeds.rb b/lib/gitlab/ci/stage/seeds.rb new file mode 100644 index 00000000000..16c436b5d4f --- /dev/null +++ b/lib/gitlab/ci/stage/seeds.rb @@ -0,0 +1,58 @@ +module Gitlab + module Ci + module Stage + class Seeds + Seed = Struct.new(:stage, :jobs) + + def initialize + @stages = [] + end + + def stages + @stages.map(&:stage) + end + + def jobs + @stages.map(&:jobs).flatten + end + + def append_stage(stage, jobs) + @stages << Seed.new({ name: stage }, jobs) + end + + def pipeline=(pipeline) + trigger_request = pipeline.trigger_requests.first + + stages.each do |attributes| + attributes.merge!( + pipeline: pipeline, + project: pipeline.project, + ) + end + + jobs.each do |attributes| + attributes.merge!( + pipeline: pipeline, + project: pipeline.project, + ref: pipeline.ref, + tag: pipeline.tag, + trigger_request: trigger_request + ) + end + end + + def user=(current_user) + jobs.each do |attributes| + attributes.merge!(user: current_user) + end + end + + def to_attributes + @stages.map.with_index do |seed| + seed.stage.merge(builds_attributes: seed.jobs) + end + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/stage/seed_spec.rb b/spec/lib/gitlab/ci/stage/seed_spec.rb deleted file mode 100644 index a12093b2c7c..00000000000 --- a/spec/lib/gitlab/ci/stage/seed_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'spec_helper' - -describe Gitlab::Ci::Stage::Seed do - subject do - described_class.new(name: 'test', builds: builds) - end - - let(:builds) do - [{ name: 'rspec' }, { name: 'spinach' }] - end - - describe '#pipeline=' do - let(:pipeline) do - create(:ci_empty_pipeline, ref: 'feature', tag: true) - end - - it 'assignes relevant pipeline attributes' do - trigger_request = pipeline.trigger_requests.first - - subject.pipeline = pipeline - - expect(subject.builds).to all(include(pipeline: pipeline)) - expect(subject.builds).to all(include(project: pipeline.project)) - expect(subject.builds).to all(include(ref: 'feature')) - expect(subject.builds).to all(include(tag: true)) - expect(subject.builds).to all(include(trigger_request: trigger_request)) - end - end - - describe '#user=' do - let(:user) { create(:user) } - - it 'assignes relevant pipeline attributes' do - subject.user = user - - expect(subject.builds).to all(include(user: user)) - end - end - - describe '#to_attributes' do - it 'exposes stage attributes with nested jobs' do - expect(subject.to_attributes).to be_a Hash - expect(subject.to_attributes).to include(name: 'test') - expect(subject.to_attributes).to include(builds_attributes: builds) - end - end -end diff --git a/spec/lib/gitlab/ci/stage/seeds_spec.rb b/spec/lib/gitlab/ci/stage/seeds_spec.rb new file mode 100644 index 00000000000..cc4f37d236e --- /dev/null +++ b/spec/lib/gitlab/ci/stage/seeds_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +describe Gitlab::Ci::Stage::Seeds do + before do + subject.append_stage('test', [{ name: 'rspec' }, { name: 'spinach' }]) + subject.append_stage('deploy', [{ name: 'prod', script: 'cap deploy' }]) + end + + describe '#stages' do + it 'returns hashes of all stages' do + expect(subject.stages.size).to eq 2 + expect(subject.stages).to all(be_a Hash) + end + end + + describe '#jobs' do + it 'returns all jobs in all stages' do + expect(subject.jobs.size).to eq 3 + end + end + + describe '#pipeline=' do + let(:pipeline) do + create(:ci_empty_pipeline, ref: 'feature', tag: true) + end + + it 'assignes relevant pipeline attributes' do + trigger_request = pipeline.trigger_requests.first + + subject.pipeline = pipeline + + expect(subject.stages).to all(include(pipeline: pipeline)) + expect(subject.stages).to all(include(project: pipeline.project)) + expect(subject.jobs).to all(include(pipeline: pipeline)) + expect(subject.jobs).to all(include(project: pipeline.project)) + expect(subject.jobs).to all(include(ref: 'feature')) + expect(subject.jobs).to all(include(tag: true)) + expect(subject.jobs).to all(include(trigger_request: trigger_request)) + end + end + + describe '#user=' do + let(:user) { create(:user) } + + it 'assignes relevant pipeline attributes' do + subject.user = user + + expect(subject.jobs).to all(include(user: user)) + end + end + + describe '#to_attributes' do + it 'exposes stage attributes with nested jobs' do + attributes = [{ name: 'test', builds_attributes: + [{ name: 'rspec' }, { name: 'spinach' }] }, + { name: 'deploy', builds_attributes: + [{ name: 'prod', script: 'cap deploy' }] }] + + expect(subject.to_attributes).to eq attributes + end + end +end |