Commit c881425b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refine pipeline stages seeds class

parent 805715cc
......@@ -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?
......
module Gitlab
module Ci
module Stage
class Seed
attr_reader :name, :builds
class Seeds
Seed = Struct.new(:stage, :jobs)
def initialize(name:, builds:)
@name = name
@builds = builds
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
@builds.each do |attributes|
stages.each do |attributes|
attributes.merge!(
pipeline: pipeline,
project: pipeline.project,
)
end
jobs.each do |attributes|
attributes.merge!(
pipeline: pipeline,
project: pipeline.project,
......@@ -24,13 +42,15 @@ module Gitlab
end
def user=(current_user)
@builds.each do |attributes|
jobs.each do |attributes|
attributes.merge!(user: current_user)
end
end
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
......
require 'spec_helper'
describe Gitlab::Ci::Stage::Seed do
subject do
described_class.new(name: 'test', builds: builds)
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
let(:builds) do
[{ name: 'rspec' }, { name: 'spinach' }]
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
......@@ -19,11 +29,13 @@ describe Gitlab::Ci::Stage::Seed do
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))
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
......@@ -33,15 +45,18 @@ describe Gitlab::Ci::Stage::Seed do
it 'assignes relevant pipeline attributes' do
subject.user = user
expect(subject.builds).to all(include(user: user))
expect(subject.jobs).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)
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
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