2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
class Rules::Rule::Clause::Changes < Rules::Rule::Clause
|
2022-08-13 15:12:31 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def initialize(globs)
|
2022-08-13 15:12:31 +05:30
|
|
|
@globs = globs
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def satisfied_by?(pipeline, context)
|
2022-08-27 11:52:29 +05:30
|
|
|
modified_paths = find_modified_paths(pipeline)
|
|
|
|
|
|
|
|
return true unless modified_paths
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expanded_globs = expand_globs(context)
|
2022-08-27 11:52:29 +05:30
|
|
|
modified_paths.any? do |path|
|
2021-01-29 00:20:46 +05:30
|
|
|
expanded_globs.any? do |glob|
|
2019-12-04 20:38:33 +05:30
|
|
|
File.fnmatch?(glob, path, File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def expand_globs(context)
|
2022-08-13 15:12:31 +05:30
|
|
|
return paths unless context
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
paths.map do |glob|
|
2022-01-26 12:08:38 +05:30
|
|
|
ExpandVariables.expand_existing(glob, -> { context.variables_hash })
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
|
|
|
|
def paths
|
|
|
|
strong_memoize(:paths) do
|
2022-08-27 11:52:29 +05:30
|
|
|
Array(@globs[:paths])
|
2022-08-13 15:12:31 +05:30
|
|
|
end
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def find_modified_paths(pipeline)
|
|
|
|
return unless pipeline
|
|
|
|
return pipeline.modified_paths unless ::Feature.enabled?(:ci_rules_changes_compare, pipeline.project)
|
|
|
|
|
|
|
|
compare_to_sha = find_compare_to_sha(pipeline)
|
|
|
|
|
|
|
|
if compare_to_sha
|
|
|
|
pipeline.modified_paths_since(compare_to_sha)
|
|
|
|
else
|
|
|
|
pipeline.modified_paths
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_compare_to_sha(pipeline)
|
|
|
|
return unless @globs.include?(:compare_to)
|
|
|
|
|
|
|
|
commit = pipeline.project.commit(@globs[:compare_to])
|
|
|
|
raise Rules::Rule::Clause::ParseError, 'rules:changes:compare_to is not a valid ref' unless commit
|
|
|
|
|
|
|
|
commit.sha
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|