BigW Consortium Gitlab

Commit c881425b by Grzegorz Bizon

Refine pipeline stages seeds class

parent 805715cc
...@@ -51,7 +51,7 @@ module Ci ...@@ -51,7 +51,7 @@ module Ci
end end
def stages_for_ref(ref, tag = false, trigger_request = nil) 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) builds = builds_for_stage_and_ref(stage, ref, tag, trigger_request)
{ name: stage, builds_attributes: builds.to_a } if builds.any? { name: stage, builds_attributes: builds.to_a } if builds.any?
......
module Gitlab module Gitlab
module Ci module Ci
module Stage module Stage
class Seed class Seeds
attr_reader :name, :builds Seed = Struct.new(:stage, :jobs)
def initialize(name:, builds:) def initialize
@name = name @stages = []
@builds = builds 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 end
def pipeline=(pipeline) def pipeline=(pipeline)
trigger_request = pipeline.trigger_requests.first trigger_request = pipeline.trigger_requests.first
@builds.each do |attributes| stages.each do |attributes|
attributes.merge!(
pipeline: pipeline,
project: pipeline.project,
)
end
jobs.each do |attributes|
attributes.merge!( attributes.merge!(
pipeline: pipeline, pipeline: pipeline,
project: pipeline.project, project: pipeline.project,
...@@ -24,13 +42,15 @@ module Gitlab ...@@ -24,13 +42,15 @@ module Gitlab
end end
def user=(current_user) def user=(current_user)
@builds.each do |attributes| jobs.each do |attributes|
attributes.merge!(user: current_user) attributes.merge!(user: current_user)
end end
end end
def to_attributes def to_attributes
{ name: @name, builds_attributes: @builds } @stages.map.with_index do |seed|
seed.stage.merge(builds_attributes: seed.jobs)
end
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Stage::Seed do describe Gitlab::Ci::Stage::Seeds do
subject do before do
described_class.new(name: 'test', builds: builds) subject.append_stage('test', [{ name: 'rspec' }, { name: 'spinach' }])
subject.append_stage('deploy', [{ name: 'prod', script: 'cap deploy' }])
end end
let(:builds) do describe '#stages' do
[{ name: 'rspec' }, { name: 'spinach' }] 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 end
describe '#pipeline=' do describe '#pipeline=' do
...@@ -19,11 +29,13 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -19,11 +29,13 @@ describe Gitlab::Ci::Stage::Seed do
subject.pipeline = pipeline subject.pipeline = pipeline
expect(subject.builds).to all(include(pipeline: pipeline)) expect(subject.stages).to all(include(pipeline: pipeline))
expect(subject.builds).to all(include(project: pipeline.project)) expect(subject.stages).to all(include(project: pipeline.project))
expect(subject.builds).to all(include(ref: 'feature')) expect(subject.jobs).to all(include(pipeline: pipeline))
expect(subject.builds).to all(include(tag: true)) expect(subject.jobs).to all(include(project: pipeline.project))
expect(subject.builds).to all(include(trigger_request: trigger_request)) 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
end end
...@@ -33,15 +45,18 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -33,15 +45,18 @@ describe Gitlab::Ci::Stage::Seed do
it 'assignes relevant pipeline attributes' do it 'assignes relevant pipeline attributes' do
subject.user = user subject.user = user
expect(subject.builds).to all(include(user: user)) expect(subject.jobs).to all(include(user: user))
end end
end end
describe '#to_attributes' do describe '#to_attributes' do
it 'exposes stage attributes with nested jobs' do it 'exposes stage attributes with nested jobs' do
expect(subject.to_attributes).to be_a Hash attributes = [{ name: 'test', builds_attributes:
expect(subject.to_attributes).to include(name: 'test') [{ name: 'rspec' }, { name: 'spinach' }] },
expect(subject.to_attributes).to include(builds_attributes: builds) { name: 'deploy', builds_attributes:
[{ name: 'prod', script: 'cap deploy' }] }]
expect(subject.to_attributes).to eq attributes
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment