Commit 8b13b8c4 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Evaluate only/except policies outside of YAML processor

parent b16b754f
......@@ -3,6 +3,8 @@ module Gitlab
module Pipeline
module Seed
class Build < Seed::Base
include Gitlab::Utils::StrongMemoize
attr_reader :pipeline, :attributes
delegate :dig, to: :attributes
......@@ -10,6 +12,9 @@ module Gitlab
def initialize(pipeline, attributes)
@pipeline = pipeline
@attributes = attributes
@only = attributes.delete(:only)
@except = attributes.delete(:except)
end
# TODO find a different solution
......@@ -29,6 +34,16 @@ module Gitlab
)
end
def included?
strong_memoize(:inclusion) do
only_specs = Gitlab::Ci::Build::Policy.fabricate(@only)
except_specs = Gitlab::Ci::Build::Policy.fabricate(@except)
only_specs.all? { |spec| spec.satisfied_by?(pipeline) } &&
except_specs.none? { |spec| spec.satisfied_by?(pipeline) }
end
end
def to_resource
::Ci::Build.new(attributes)
end
......
......@@ -8,11 +8,11 @@ module Gitlab
delegate :size, to: :seeds
delegate :dig, to: :seeds
def initialize(pipeline, name, builds)
def initialize(pipeline, attributes)
@pipeline = pipeline
@name = name
@attributes = attributes
@seeds = builds.map do |attributes|
@seeds = attributes.fetch(:builds).map do |attributes|
Seed::Build.new(@pipeline, attributes)
end
end
......@@ -22,17 +22,23 @@ module Gitlab
end
def attributes
{ name: @name,
{ name: @attributes.fetch(:name),
pipeline: @pipeline,
project: @pipeline.project }
end
# TODO specs
#
def included?
@seeds.any?(&:included?)
end
def to_resource
::Ci::Stage.new(attributes).tap do |stage|
@stage ||= ::Ci::Stage.new(attributes).tap do |stage|
@seeds.each do |seed|
seed.to_resource.tap do |build|
stage.builds << build
end
next unless seed.included?
stage.builds << seed.to_resource
end
end
end
......
......@@ -53,6 +53,22 @@ module Gitlab
}.compact }
end
# REFACTORING, this needs improvement
#
def build_seed_attributes(stage)
selected = @jobs.values.select do |job|
job[:stage] == stage
end
selected.map do |job|
build_attributes(job[:name])
.merge(only: job.fetch(:only, {}))
.merge(except: job.fetch(:except, {}))
end
end
# REFACTORING, slated for removal
#
def pipeline_stage_builds(stage, pipeline)
selected_jobs = @jobs.select do |_, job|
next unless job[:stage] == stage
......@@ -69,13 +85,24 @@ module Gitlab
selected_jobs.map { |_, job| build_attributes(job[:name]) }
end
def stage_seed_attributes(stage)
{ name: stage,
index: @stages.index(stage),
builds: build_seed_attributes(stage) }
end
# REFACTORING, slated for removal
# * WARNING this method is currently evaluating only/except policies
# in two places - Seed::Build, and in pipeline_stage_builds
# * WARNING it needs to be refactored to use SSOT
#
def stage_seeds(pipeline)
seeds = @stages.uniq.map do |stage|
builds = pipeline_stage_builds(stage, pipeline)
if builds.any?
Gitlab::Ci::Pipeline::Seed::Stage
.new(pipeline, stage, builds)
.new(pipeline, stage_seed_attributes(stage))
end
end
......
......@@ -3,12 +3,14 @@ require 'spec_helper'
describe Gitlab::Ci::Pipeline::Seed::Stage do
let(:pipeline) { create(:ci_empty_pipeline) }
let(:builds) do
[{ name: 'rspec' }, { name: 'spinach' }]
let(:attributes) do
{ name: 'test',
index: 0,
builds: [{ name: 'rspec' }, { name: 'spinach' }] }
end
subject do
described_class.new(pipeline, 'test', builds)
described_class.new(pipeline, attributes)
end
describe '#size' do
......
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