Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cache.rb « entry « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9688c500d297090834bb38ec5dfd2caa0b185e8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents a cache configuration
        #
        class Cache < ::Gitlab::Config::Entry::Simplifiable
          strategy :Caches, if: -> (config) { Feature.enabled?(:multiple_cache_per_job, default_enabled: :yaml) }
          strategy :Cache, if: -> (config) { Feature.disabled?(:multiple_cache_per_job, default_enabled: :yaml) }

          class Caches < ::Gitlab::Config::Entry::ComposableArray
            include ::Gitlab::Config::Entry::Validatable

            MULTIPLE_CACHE_LIMIT = 4

            validations do
              validate do
                unless config.is_a?(Hash) || config.is_a?(Array)
                  errors.add(:config, 'can only be a Hash or an Array')
                end

                if config.is_a?(Array) && config.count > MULTIPLE_CACHE_LIMIT
                  errors.add(:config, "no more than #{MULTIPLE_CACHE_LIMIT} caches can be created")
                end
              end
            end

            def initialize(*args)
              super

              @key = nil
            end

            def composable_class
              Entry::Cache::Cache
            end
          end

          class Cache < ::Gitlab::Config::Entry::Node
            include ::Gitlab::Config::Entry::Configurable
            include ::Gitlab::Config::Entry::Validatable
            include ::Gitlab::Config::Entry::Attributable

            ALLOWED_KEYS = %i[key untracked paths when policy].freeze
            ALLOWED_POLICY = %w[pull-push push pull].freeze
            DEFAULT_POLICY = 'pull-push'
            ALLOWED_WHEN = %w[on_success on_failure always].freeze
            DEFAULT_WHEN = 'on_success'

            validations do
              validates :config, type: Hash, allowed_keys: ALLOWED_KEYS
              validates :policy,
                inclusion: { in: ALLOWED_POLICY, message: 'should be pull-push, push, or pull' },
                allow_blank: true

              with_options allow_nil: true do
                validates :when,
                    inclusion: {
                      in: ALLOWED_WHEN,
                      message: 'should be on_success, on_failure or always'
                    }
              end
            end

            entry :key, Entry::Key,
              description: 'Cache key used to define a cache affinity.'

            entry :untracked, ::Gitlab::Config::Entry::Boolean,
              description: 'Cache all untracked files.'

            entry :paths, Entry::Paths,
              description: 'Specify which paths should be cached across builds.'

            attributes :policy, :when

            def value
              result = super

              result[:key] = key_value
              result[:policy] = policy || DEFAULT_POLICY
              # Use self.when to avoid conflict with reserved word
              result[:when] = self.when || DEFAULT_WHEN

              result
            end
          end

          class UnknownStrategy < ::Gitlab::Config::Entry::Node
          end
        end
      end
    end
  end
end