summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-03-21 14:40:27 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-03-21 14:40:27 +0100
commitdb3d0319699cf39e55f79678969bfdb4cc8d2924 (patch)
treeca0551e8c20c8203a2bd744b84f73e0c69d26e71
parent9b5a912f9377a77e52e9acdf7471acd27c709264 (diff)
downloadgitlab-ce-db3d0319699cf39e55f79678969bfdb4cc8d2924.tar.gz
Evaluate only/except policies outside of YAML processor
-rw-r--r--lib/gitlab/ci/pipeline/seed/build.rb15
-rw-r--r--lib/gitlab/ci/pipeline/seed/stage.rb22
-rw-r--r--lib/gitlab/ci/yaml_processor.rb29
-rw-r--r--spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb8
4 files changed, 62 insertions, 12 deletions
diff --git a/lib/gitlab/ci/pipeline/seed/build.rb b/lib/gitlab/ci/pipeline/seed/build.rb
index 2c29e647fe6..5f7721ecb2c 100644
--- a/lib/gitlab/ci/pipeline/seed/build.rb
+++ b/lib/gitlab/ci/pipeline/seed/build.rb
@@ -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
diff --git a/lib/gitlab/ci/pipeline/seed/stage.rb b/lib/gitlab/ci/pipeline/seed/stage.rb
index 6d824e52334..bed514c02ca 100644
--- a/lib/gitlab/ci/pipeline/seed/stage.rb
+++ b/lib/gitlab/ci/pipeline/seed/stage.rb
@@ -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
diff --git a/lib/gitlab/ci/yaml_processor.rb b/lib/gitlab/ci/yaml_processor.rb
index 70ed20d6913..7519331c9de 100644
--- a/lib/gitlab/ci/yaml_processor.rb
+++ b/lib/gitlab/ci/yaml_processor.rb
@@ -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
diff --git a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
index e71f2f54e96..75ab2cf1e7f 100644
--- a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
@@ -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