summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/build/rules/rule/clause/changes.rb
blob: 4069a683ceb4d84158212c5edb8cdde3f515754a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Gitlab
  module Ci
    module Build
      class Rules::Rule::Clause::Changes < Rules::Rule::Clause
        include Gitlab::Utils::StrongMemoize

        def initialize(globs)
          @globs = globs
        end

        def satisfied_by?(pipeline, context)
          modified_paths = find_modified_paths(pipeline)

          return true unless modified_paths

          expanded_globs = expand_globs(context)
          modified_paths.any? do |path|
            expanded_globs.any? do |glob|
              File.fnmatch?(glob, path, File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB)
            end
          end
        end

        private

        def expand_globs(context)
          return paths unless context

          paths.map do |glob|
            ExpandVariables.expand_existing(glob, -> { context.variables_hash })
          end
        end

        def paths
          strong_memoize(:paths) do
            Array(@globs[:paths])
          end
        end

        def find_modified_paths(pipeline)
          return unless pipeline

          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
      end
    end
  end
end