gitlab-org--gitlab-foss/lib/gitlab/ci/build/policy/refs.rb
Shinya Maeda f62dc67d26 Add suffix for merge request event
Fix

ok

Add spec

Fix

ok

Fix

Add changelog

Fix

Add memoization

a

fix
2019-03-01 22:52:38 +09:00

53 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module Ci
module Build
module Policy
class Refs < Policy::Specification
def initialize(refs)
@patterns = Array(refs)
end
def satisfied_by?(pipeline, seed = nil)
@patterns.any? do |pattern|
pattern, path = pattern.split('@', 2)
matches_path?(path, pipeline) &&
matches_pattern?(pattern, pipeline)
end
end
private
def matches_path?(path, pipeline)
return true unless path
pipeline.project_full_path == path
end
def matches_pattern?(pattern, pipeline)
return true if pipeline.tag? && pattern == 'tags'
return true if pipeline.branch? && pattern == 'branches'
return true if sanitized_source_name(pipeline) == pattern
return true if sanitized_source_name(pipeline)&.pluralize == pattern
# patterns can be matched only when branch or tag is used
# the pattern matching does not work for merge requests pipelines
if pipeline.branch? || pipeline.tag?
if pattern.first == "/" && pattern.last == "/"
Regexp.new(pattern[1...-1]) =~ pipeline.ref
else
pattern == pipeline.ref
end
end
end
def sanitized_source_name(pipeline)
@sanitized_source_name ||= pipeline&.source&.delete_suffix('_event')
end
end
end
end
end
end