BigW Consortium Gitlab

environment.rb 1.94 KB
Newer Older
1 2 3
module Gitlab
  module Ci
    class Config
4
      module Entry
5
        ##
6
        # Entry that represents an environment.
7
        #
8
        class Environment < Node
9 10
          include Validatable

11
          ALLOWED_KEYS = %i[name url action on_stop]
12

13 14
          validations do
            validate do
15 16
              unless hash? || string?
                errors.add(:config, 'should be a hash or a string')
17 18
              end
            end
19

20
            validates :name, presence: true
21 22 23 24 25 26 27 28 29
            validates :name,
              type: {
                with: String,
                message: Gitlab::Regex.environment_name_regex_message }

            validates :name,
              format: {
                with: Gitlab::Regex.environment_name_regex,
                message: Gitlab::Regex.environment_name_regex_message }
30

31 32 33 34 35 36 37
            with_options if: :hash? do
              validates :config, allowed_keys: ALLOWED_KEYS

              validates :url,
                        length: { maximum: 255 },
                        addressable_url: true,
                        allow_nil: true
38

39
              validates :action,
40
                        inclusion: { in: %w[start stop], message: 'should be start or stop' },
41 42
                        allow_nil: true

43
              validates :on_stop, type: String, allow_nil: true
44
            end
45 46 47 48 49 50 51 52 53
          end

          def hash?
            @config.is_a?(Hash)
          end

          def string?
            @config.is_a?(String)
          end
54

55
          def name
Kamil Trzcinski committed
56
            value[:name]
57 58
          end

59
          def url
Kamil Trzcinski committed
60
            value[:url]
61 62
          end

63 64 65 66
          def action
            value[:action] || 'start'
          end

67 68 69 70
          def on_stop
            value[:on_stop]
          end

71
          def value
72
            case @config
73
            when String then { name: @config, action: 'start' }
74
            when Hash then @config
75
            else {}
76
            end
77 78 79 80 81 82
          end
        end
      end
    end
  end
end