BigW Consortium Gitlab

environment.rb 1.92 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].freeze
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
            validates :name,
              type: {
                with: String,
24 25
                message: Gitlab::Regex.environment_name_regex_message
              }
26 27 28 29

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

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

              validates :url,
                        length: { maximum: 255 },
                        allow_nil: true
39

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

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

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

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

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

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

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

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

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