2016-09-13 08:14:55 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2016-11-14 04:31:45 -05:00
|
|
|
module Entry
|
2016-09-13 08:14:55 -04:00
|
|
|
##
|
2016-09-14 13:52:28 -04:00
|
|
|
# Entry that represents an environment.
|
2016-09-13 08:14:55 -04:00
|
|
|
#
|
2016-11-14 04:31:45 -05:00
|
|
|
class Environment < Node
|
2016-09-13 08:14:55 -04:00
|
|
|
include Validatable
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
ALLOWED_KEYS = %i[name url action on_stop].freeze
|
2016-09-14 17:00:15 -04:00
|
|
|
|
2016-09-13 08:14:55 -04:00
|
|
|
validations do
|
|
|
|
validate do
|
2016-09-14 13:52:28 -04:00
|
|
|
unless hash? || string?
|
|
|
|
errors.add(:config, 'should be a hash or a string')
|
2016-09-13 08:14:55 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-16 05:00:13 -04:00
|
|
|
|
2016-09-16 05:25:37 -04:00
|
|
|
validates :name, presence: true
|
2016-09-16 05:38:56 -04:00
|
|
|
validates :name,
|
|
|
|
type: {
|
|
|
|
with: String,
|
2017-02-22 12:44:44 -05:00
|
|
|
message: Gitlab::Regex.environment_name_regex_message
|
|
|
|
}
|
2016-09-16 05:38:56 -04:00
|
|
|
|
|
|
|
validates :name,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.environment_name_regex,
|
2017-02-22 12:44:44 -05:00
|
|
|
message: Gitlab::Regex.environment_name_regex_message
|
|
|
|
}
|
2016-09-16 05:25:37 -04:00
|
|
|
|
2016-09-16 05:00:13 -04:00
|
|
|
with_options if: :hash? do
|
|
|
|
validates :config, allowed_keys: ALLOWED_KEYS
|
|
|
|
|
|
|
|
validates :url,
|
|
|
|
length: { maximum: 255 },
|
|
|
|
allow_nil: true
|
2016-10-06 07:10:50 -04:00
|
|
|
|
2016-10-17 06:46:00 -04:00
|
|
|
validates :action,
|
2016-10-17 10:13:19 -04:00
|
|
|
inclusion: { in: %w[start stop], message: 'should be start or stop' },
|
2016-10-17 06:46:00 -04:00
|
|
|
allow_nil: true
|
|
|
|
|
2016-10-17 10:13:19 -04:00
|
|
|
validates :on_stop, type: String, allow_nil: true
|
2016-09-16 05:00:13 -04:00
|
|
|
end
|
2016-09-14 13:52:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def hash?
|
|
|
|
@config.is_a?(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def string?
|
|
|
|
@config.is_a?(String)
|
|
|
|
end
|
2016-09-13 08:14:55 -04:00
|
|
|
|
2016-09-14 13:52:28 -04:00
|
|
|
def name
|
2016-09-15 15:59:01 -04:00
|
|
|
value[:name]
|
2016-09-13 08:14:55 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 13:52:28 -04:00
|
|
|
def url
|
2016-09-15 15:59:01 -04:00
|
|
|
value[:url]
|
2016-09-14 13:52:28 -04:00
|
|
|
end
|
|
|
|
|
2016-10-17 06:46:00 -04:00
|
|
|
def action
|
|
|
|
value[:action] || 'start'
|
|
|
|
end
|
|
|
|
|
2016-10-17 10:23:17 -04:00
|
|
|
def on_stop
|
|
|
|
value[:on_stop]
|
|
|
|
end
|
|
|
|
|
2016-09-13 08:14:55 -04:00
|
|
|
def value
|
2016-09-16 05:00:13 -04:00
|
|
|
case @config
|
2016-10-17 06:46:00 -04:00
|
|
|
when String then { name: @config, action: 'start' }
|
2016-09-14 17:00:15 -04:00
|
|
|
when Hash then @config
|
2016-09-16 05:00:13 -04:00
|
|
|
else {}
|
2016-09-14 13:52:28 -04:00
|
|
|
end
|
2016-09-13 08:14:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|