2018-10-26 00:12:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-14 06:59:41 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
module Policy
|
|
|
|
class Refs < Policy::Specification
|
|
|
|
def initialize(refs)
|
|
|
|
@patterns = Array(refs)
|
|
|
|
end
|
|
|
|
|
2019-11-17 16:06:14 -05:00
|
|
|
def satisfied_by?(pipeline, context = nil)
|
2017-09-14 06:59:41 -04:00
|
|
|
@patterns.any? do |pattern|
|
2017-09-18 08:29:43 -04:00
|
|
|
pattern, path = pattern.split('@', 2)
|
2017-09-14 06:59:41 -04:00
|
|
|
|
2017-09-18 08:29:43 -04:00
|
|
|
matches_path?(path, pipeline) &&
|
2017-09-14 06:59:41 -04:00
|
|
|
matches_pattern?(pattern, pipeline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-18 08:29:43 -04:00
|
|
|
def matches_path?(path, pipeline)
|
|
|
|
return true unless path
|
2017-09-14 06:59:41 -04:00
|
|
|
|
2017-09-18 08:29:43 -04:00
|
|
|
pipeline.project_full_path == path
|
2017-09-14 06:59:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches_pattern?(pattern, pipeline)
|
|
|
|
return true if pipeline.tag? && pattern == 'tags'
|
2017-09-14 07:53:02 -04:00
|
|
|
return true if pipeline.branch? && pattern == 'branches'
|
2019-02-25 06:05:26 -05:00
|
|
|
return true if sanitized_source_name(pipeline) == pattern
|
|
|
|
return true if sanitized_source_name(pipeline)&.pluralize == pattern
|
2017-09-14 06:59:41 -04:00
|
|
|
|
2018-12-07 09:38:09 -05:00
|
|
|
# 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?
|
2019-04-04 11:00:56 -04:00
|
|
|
if regexp = Gitlab::UntrustedRegexp::RubySyntax.fabricate(pattern, fallback: true)
|
2019-02-25 09:41:52 -05:00
|
|
|
regexp.match?(pipeline.ref)
|
2018-12-07 09:38:09 -05:00
|
|
|
else
|
|
|
|
pattern == pipeline.ref
|
|
|
|
end
|
2017-09-14 06:59:41 -04:00
|
|
|
end
|
|
|
|
end
|
2019-02-25 06:05:26 -05:00
|
|
|
|
|
|
|
def sanitized_source_name(pipeline)
|
|
|
|
@sanitized_source_name ||= pipeline&.source&.delete_suffix('_event')
|
|
|
|
end
|
2017-09-14 06:59:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|