Commit 241197c2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract job refs policy specs into separate class

parent dd784b15
module Gitlab
module Ci
module Build
module Policy
class Refs < Policy::Specification
def initialize(refs)
@patterns = Array(refs)
end
def satisfied_by?(pipeline, path:)
@patterns.any? do |pattern|
pattern, ref_path = pattern.split('@', 2)
matches_path?(ref_path, path) &&
matches_pattern?(pattern, pipeline)
end
end
private
def matches_path?(ref_path, expected_path)
return true unless ref_path
expected_path == ref_path
end
def matches_pattern?(pattern, pipeline)
return true if pipeline.tag? && pattern == 'tags'
return true if !pipeline.tag? && pattern == 'branches'
return true if source_to_pattern(pipeline.source) == pattern
if pattern.first == "/" && pattern.last == "/"
Regexp.new(pattern[1...-1]) =~ pipeline.ref
else
pattern == pipeline.ref
end
end
def source_to_pattern(source)
if %w[api external web].include?(source)
source
else
source&.pluralize
end
end
end
end
end
end
end
...@@ -21,10 +21,11 @@ module Gitlab ...@@ -21,10 +21,11 @@ module Gitlab
raise ValidationError, e.message raise ValidationError, e.message
end end
# REFACTORING STUB, remove this method, used only in tests.
#
def builds_for_stage_and_ref(stage, ref, tag = false, source = nil) def builds_for_stage_and_ref(stage, ref, tag = false, source = nil)
jobs_for_stage_and_ref(stage, ref, tag, source).map do |name, _| pipeline_stage_builds(stage, ::Ci::Pipeline.new(ref: ref, source: source, tag: tag))
build_attributes(name)
end
end end
def builds def builds
...@@ -84,32 +85,19 @@ module Gitlab ...@@ -84,32 +85,19 @@ module Gitlab
private private
def pipeline_stage_builds(stage, pipeline) def pipeline_stage_builds(stage, pipeline)
builds = builds_for_stage_and_ref( stage_jobs = @jobs.select do |_, job|
stage, pipeline.ref, pipeline.tag?, pipeline.source) next unless job[:stage] == stage
builds.select do |build|
job = @jobs[build.fetch(:name).to_sym]
only_specs = Gitlab::Ci::Build::Policy only_specs = Gitlab::Ci::Build::Policy
.fabricate(job.fetch(:only, {})) .fabricate(job.fetch(:only, {}))
except_specs = Gitlab::Ci::Build::Policy except_specs = Gitlab::Ci::Build::Policy
.fabricate(job.fetch(:except, {})) .fabricate(job.fetch(:except, {}))
only_specs.all? { |spec| spec.satisfied_by?(pipeline) } && only_specs.all? { |spec| spec.satisfied_by?(pipeline, path: @path) } &&
except_specs.none? { |spec| spec.satisfied_by?(pipeline) } except_specs.none? { |spec| spec.satisfied_by?(pipeline, path: @path) }
end
end end
def jobs_for_ref(ref, tag = false, source = nil) stage_jobs.map { |_, job| build_attributes(job[:name]) }
@jobs.select do |_, job|
process?(job.dig(:only, :refs), job.dig(:except, :refs), ref, tag, source)
end
end
def jobs_for_stage_and_ref(stage, ref, tag = false, source = nil)
jobs_for_ref(ref, tag, source).select do |_, job|
job[:stage] == stage
end
end end
def initial_parsing def initial_parsing
...@@ -204,51 +192,6 @@ module Gitlab ...@@ -204,51 +192,6 @@ module Gitlab
raise ValidationError, "#{name} job: on_stop job #{on_stop} needs to have action stop defined" raise ValidationError, "#{name} job: on_stop job #{on_stop} needs to have action stop defined"
end end
end end
def process?(only_params, except_params, ref, tag, source)
if only_params.present?
return false unless matching?(only_params, ref, tag, source)
end
if except_params.present?
return false if matching?(except_params, ref, tag, source)
end
true
end
def matching?(patterns, ref, tag, source)
patterns.any? do |pattern|
pattern, path = pattern.split('@', 2)
matches_path?(path) && matches_pattern?(pattern, ref, tag, source)
end
end
def matches_path?(path)
return true unless path
path == self.path
end
def matches_pattern?(pattern, ref, tag, source)
return true if tag && pattern == 'tags'
return true if !tag && pattern == 'branches'
return true if source_to_pattern(source) == pattern
if pattern.first == "/" && pattern.last == "/"
Regexp.new(pattern[1...-1]) =~ ref
else
pattern == ref
end
end
def source_to_pattern(source)
if %w[api external web].include?(source)
source
else
source&.pluralize
end
end
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