summaryrefslogtreecommitdiff
path: root/lib/gitlab/checks/base_checker.rb
blob: a14fa02c2a4e3d59a008c56d2150cfc0c8f18e94 (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
# frozen_string_literal: true

module Gitlab
  module Checks
    class BaseChecker
      prepend_if_ee('EE::Gitlab::Checks::BaseChecker') # rubocop: disable Cop/InjectEnterpriseEditionModule
      include Gitlab::Utils::StrongMemoize

      attr_reader :change_access
      delegate(*ChangeAccess::ATTRIBUTES, to: :change_access)

      def initialize(change_access)
        @change_access = change_access
      end

      def validate!
        raise NotImplementedError
      end

      private

      def creation?
        Gitlab::Git.blank_ref?(oldrev)
      end

      def deletion?
        Gitlab::Git.blank_ref?(newrev)
      end

      def update?
        !creation? && !deletion?
      end

      def updated_from_web?
        protocol == 'web'
      end

      def tag_exists?
        project.repository.tag_exists?(tag_name)
      end

      def validate_once(resource)
        Gitlab::SafeRequestStore.fetch(cache_key_for_resource(resource)) do
          yield(resource)

          true
        end
      end

      def cache_key_for_resource(resource)
        "git_access:#{checker_cache_key}:#{resource.cache_key}"
      end

      def checker_cache_key
        self.class.name.demodulize.underscore
      end
    end
  end
end