Evaluate only/except policies outside of YAML processor

This commit is contained in:
Grzegorz Bizon 2018-03-21 14:40:27 +01:00
parent 9b5a912f93
commit db3d031969
4 changed files with 62 additions and 12 deletions

View file

@ -3,6 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Seed module Seed
class Build < Seed::Base class Build < Seed::Base
include Gitlab::Utils::StrongMemoize
attr_reader :pipeline, :attributes attr_reader :pipeline, :attributes
delegate :dig, to: :attributes delegate :dig, to: :attributes
@ -10,6 +12,9 @@ module Gitlab
def initialize(pipeline, attributes) def initialize(pipeline, attributes)
@pipeline = pipeline @pipeline = pipeline
@attributes = attributes @attributes = attributes
@only = attributes.delete(:only)
@except = attributes.delete(:except)
end end
# TODO find a different solution # TODO find a different solution
@ -29,6 +34,16 @@ module Gitlab
) )
end 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 def to_resource
::Ci::Build.new(attributes) ::Ci::Build.new(attributes)
end end

View file

@ -8,11 +8,11 @@ module Gitlab
delegate :size, to: :seeds delegate :size, to: :seeds
delegate :dig, to: :seeds delegate :dig, to: :seeds
def initialize(pipeline, name, builds) def initialize(pipeline, attributes)
@pipeline = pipeline @pipeline = pipeline
@name = name @attributes = attributes
@seeds = builds.map do |attributes| @seeds = attributes.fetch(:builds).map do |attributes|
Seed::Build.new(@pipeline, attributes) Seed::Build.new(@pipeline, attributes)
end end
end end
@ -22,17 +22,23 @@ module Gitlab
end end
def attributes def attributes
{ name: @name, { name: @attributes.fetch(:name),
pipeline: @pipeline, pipeline: @pipeline,
project: @pipeline.project } project: @pipeline.project }
end end
# TODO specs
#
def included?
@seeds.any?(&:included?)
end
def to_resource def to_resource
::Ci::Stage.new(attributes).tap do |stage| @stage ||= ::Ci::Stage.new(attributes).tap do |stage|
@seeds.each do |seed| @seeds.each do |seed|
seed.to_resource.tap do |build| next unless seed.included?
stage.builds << build
end stage.builds << seed.to_resource
end end
end end
end end

View file

@ -53,6 +53,22 @@ module Gitlab
}.compact } }.compact }
end 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) def pipeline_stage_builds(stage, pipeline)
selected_jobs = @jobs.select do |_, job| selected_jobs = @jobs.select do |_, job|
next unless job[:stage] == stage next unless job[:stage] == stage
@ -69,13 +85,24 @@ module Gitlab
selected_jobs.map { |_, job| build_attributes(job[:name]) } selected_jobs.map { |_, job| build_attributes(job[:name]) }
end 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) def stage_seeds(pipeline)
seeds = @stages.uniq.map do |stage| seeds = @stages.uniq.map do |stage|
builds = pipeline_stage_builds(stage, pipeline) builds = pipeline_stage_builds(stage, pipeline)
if builds.any? if builds.any?
Gitlab::Ci::Pipeline::Seed::Stage Gitlab::Ci::Pipeline::Seed::Stage
.new(pipeline, stage, builds) .new(pipeline, stage_seed_attributes(stage))
end end
end end

View file

@ -3,12 +3,14 @@ require 'spec_helper'
describe Gitlab::Ci::Pipeline::Seed::Stage do describe Gitlab::Ci::Pipeline::Seed::Stage do
let(:pipeline) { create(:ci_empty_pipeline) } let(:pipeline) { create(:ci_empty_pipeline) }
let(:builds) do let(:attributes) do
[{ name: 'rspec' }, { name: 'spinach' }] { name: 'test',
index: 0,
builds: [{ name: 'rspec' }, { name: 'spinach' }] }
end end
subject do subject do
described_class.new(pipeline, 'test', builds) described_class.new(pipeline, attributes)
end end
describe '#size' do describe '#size' do