Commit bb8bf642 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move job environment validation to new CI config

parent 943ae747
......@@ -93,21 +93,10 @@ module Ci
def validate_job!(name, job)
raise ValidationError, "Unknown parameter: #{name}" unless job.is_a?(Hash) && job.has_key?(:script)
validate_job_types!(name, job)
validate_job_stage!(name, job) if job[:stage]
validate_job_dependencies!(name, job) if job[:dependencies]
end
def validate_job_types!(name, job)
if job[:when] && !job[:when].in?(%w[on_success on_failure always])
raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always"
end
if job[:environment] && !validate_environment(job[:environment])
raise ValidationError, "#{name} job: environment parameter #{Gitlab::Regex.environment_name_regex_message}"
end
end
def validate_job_stage!(name, job)
unless job[:stage].is_a?(String) && job[:stage].in?(@stages)
raise ValidationError, "#{name} job: stage parameter should be #{@stages.join(", ")}"
......
......@@ -9,7 +9,7 @@ module Gitlab
include Configurable
include Attributable
attributes :tags, :allow_failure
attributes :tags, :allow_failure, :when, :environment
validations do
validates :config, allowed_keys:
......@@ -24,6 +24,18 @@ module Gitlab
with_options allow_nil: true do
validates :tags, array_of_strings: true
validates :allow_failure, boolean: true
validates :when,
inclusion: { in: %w[on_success on_failure always],
message: 'should be on_success, on_failure ' \
'or always' }
validates :environment,
type: {
with: String,
message: Gitlab::Regex.environment_name_regex_message }
validates :environment,
format: {
with: Gitlab::Regex.environment_name_regex,
message: Gitlab::Regex.environment_name_regex_message }
end
end
......
......@@ -41,10 +41,6 @@ module Gitlab
false
end
def validate_environment(value)
value.is_a?(String) && value =~ Gitlab::Regex.environment_name_regex
end
def validate_boolean(value)
value.in?([true, false])
end
......
......@@ -69,7 +69,8 @@ module Gitlab
raise unless type.is_a?(Class)
unless value.is_a?(type)
record.errors.add(attribute, "should be a #{type.name}")
message = options[:message] || "should be a #{type.name}"
record.errors.add(attribute, message)
end
end
end
......
......@@ -754,7 +754,7 @@ module Ci
let(:environment) { 1 }
it 'raises error' do
expect { builds }.to raise_error("deploy_to_production job: environment parameter #{Gitlab::Regex.environment_name_regex_message}")
expect { builds }.to raise_error("jobs:deploy_to_production environment #{Gitlab::Regex.environment_name_regex_message}")
end
end
......@@ -762,7 +762,7 @@ module Ci
let(:environment) { 'production staging' }
it 'raises error' do
expect { builds }.to raise_error("deploy_to_production job: environment parameter #{Gitlab::Regex.environment_name_regex_message}")
expect { builds }.to raise_error("jobs:deploy_to_production environment #{Gitlab::Regex.environment_name_regex_message}")
end
end
end
......@@ -1131,7 +1131,7 @@ EOT
config = YAML.dump({ rspec: { script: "test", when: 1 } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: when parameter should be on_success, on_failure or always")
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec when should be on_success, on_failure or always")
end
it "returns errors if job artifacts:name is not an a string" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment