2015-08-25 21:42:46 -04:00
|
|
|
module Ci
|
|
|
|
class GitlabCiYamlProcessor
|
2017-03-01 06:00:37 -05:00
|
|
|
ValidationError = Class.new(StandardError)
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-11-14 04:31:45 -05:00
|
|
|
include Gitlab::Ci::Config::Entry::LegacyValidationHelpers
|
2016-06-06 05:05:15 -04:00
|
|
|
|
2016-09-20 09:19:55 -04:00
|
|
|
attr_reader :path, :cache, :stages, :jobs
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-11-02 08:44:06 -05:00
|
|
|
def initialize(config, path = nil)
|
2016-06-06 06:23:27 -04:00
|
|
|
@ci_config = Gitlab::Ci::Config.new(config)
|
2016-07-20 04:59:49 -04:00
|
|
|
@config = @ci_config.to_hash
|
|
|
|
@path = path
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-06-21 06:10:13 -04:00
|
|
|
unless @ci_config.valid?
|
|
|
|
raise ValidationError, @ci_config.errors.first
|
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-06-21 06:10:13 -04:00
|
|
|
initial_parsing
|
2016-06-07 04:26:38 -04:00
|
|
|
rescue Gitlab::Ci::Config::Loader::FormatError => e
|
2016-06-03 08:20:34 -04:00
|
|
|
raise ValidationError, e.message
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def jobs_for_ref(ref, tag = false, trigger_request = nil)
|
|
|
|
@jobs.select do |_, job|
|
|
|
|
process?(job[:only], job[:except], ref, tag, trigger_request)
|
2016-06-02 07:52:19 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def jobs_for_stage_and_ref(stage, ref, tag = false, trigger_request = nil)
|
|
|
|
jobs_for_ref(ref, tag, trigger_request).select do |_, job|
|
|
|
|
job[:stage] == stage
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def builds_for_ref(ref, tag = false, trigger_request = nil)
|
2016-07-19 08:53:31 -04:00
|
|
|
jobs_for_ref(ref, tag, trigger_request).map do |name, _|
|
2016-07-19 08:52:12 -04:00
|
|
|
build_attributes(name)
|
2016-07-16 14:10:22 -04:00
|
|
|
end
|
2016-04-15 06:18:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def builds_for_stage_and_ref(stage, ref, tag = false, trigger_request = nil)
|
2016-07-19 07:23:14 -04:00
|
|
|
jobs_for_stage_and_ref(stage, ref, tag, trigger_request).map do |name, _|
|
|
|
|
build_attributes(name)
|
2016-07-16 14:10:22 -04:00
|
|
|
end
|
|
|
|
end
|
2016-04-18 06:41:13 -04:00
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def builds
|
2016-07-19 07:23:14 -04:00
|
|
|
@jobs.map do |name, _|
|
|
|
|
build_attributes(name)
|
2016-07-16 14:10:22 -04:00
|
|
|
end
|
2016-04-15 06:18:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-19 07:23:14 -04:00
|
|
|
def build_attributes(name)
|
|
|
|
job = @jobs[name.to_sym] || {}
|
|
|
|
{
|
|
|
|
stage_idx: @stages.index(job[:stage]),
|
|
|
|
stage: job[:stage],
|
2016-08-29 07:11:35 -04:00
|
|
|
commands: job[:commands],
|
2016-07-19 07:23:14 -04:00
|
|
|
tag_list: job[:tags] || [],
|
2016-08-11 09:22:35 -04:00
|
|
|
name: job[:name].to_s,
|
2017-03-06 06:01:33 -05:00
|
|
|
allow_failure: job[:ignore],
|
2016-07-19 07:23:14 -04:00
|
|
|
when: job[:when] || 'on_success',
|
2016-09-16 05:25:37 -04:00
|
|
|
environment: job[:environment_name],
|
2016-11-25 22:02:08 -05:00
|
|
|
coverage_regex: job[:coverage],
|
2016-07-19 07:23:14 -04:00
|
|
|
yaml_variables: yaml_variables(name),
|
|
|
|
options: {
|
2016-08-29 07:10:57 -04:00
|
|
|
image: job[:image],
|
|
|
|
services: job[:services],
|
2016-07-19 07:23:14 -04:00
|
|
|
artifacts: job[:artifacts],
|
2016-08-29 07:10:57 -04:00
|
|
|
cache: job[:cache],
|
2016-07-19 07:23:14 -04:00
|
|
|
dependencies: job[:dependencies],
|
2016-08-29 07:10:57 -04:00
|
|
|
after_script: job[:after_script],
|
2016-09-14 13:52:28 -04:00
|
|
|
environment: job[:environment],
|
2016-07-19 07:23:14 -04:00
|
|
|
}.compact
|
|
|
|
}
|
2016-04-15 06:18:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-29 09:07:19 -04:00
|
|
|
def self.validation_message(content)
|
2016-08-30 07:03:29 -04:00
|
|
|
return 'Please provide content of .gitlab-ci.yml' if content.blank?
|
2016-08-30 09:38:36 -04:00
|
|
|
|
2016-08-30 07:03:29 -04:00
|
|
|
begin
|
|
|
|
Ci::GitlabCiYamlProcessor.new(content)
|
|
|
|
nil
|
|
|
|
rescue ValidationError, Psych::SyntaxError => e
|
|
|
|
e.message
|
2016-08-25 07:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def initial_parsing
|
2016-07-09 11:06:53 -04:00
|
|
|
##
|
|
|
|
# Global config
|
|
|
|
#
|
2016-06-21 06:10:13 -04:00
|
|
|
@before_script = @ci_config.before_script
|
|
|
|
@image = @ci_config.image
|
2016-06-21 07:02:14 -04:00
|
|
|
@after_script = @ci_config.after_script
|
2016-06-21 06:40:52 -04:00
|
|
|
@services = @ci_config.services
|
2016-06-22 05:22:53 -04:00
|
|
|
@variables = @ci_config.variables
|
2016-06-23 07:51:07 -04:00
|
|
|
@stages = @ci_config.stages
|
2016-06-29 03:49:46 -04:00
|
|
|
@cache = @ci_config.cache
|
2016-06-21 07:02:14 -04:00
|
|
|
|
2016-07-09 11:06:53 -04:00
|
|
|
##
|
|
|
|
# Jobs
|
|
|
|
#
|
|
|
|
@jobs = @ci_config.jobs
|
2016-07-06 08:24:31 -04:00
|
|
|
|
|
|
|
@jobs.each do |name, job|
|
2016-07-20 08:15:18 -04:00
|
|
|
# logical validation for job
|
|
|
|
|
|
|
|
validate_job_stage!(name, job)
|
|
|
|
validate_job_dependencies!(name, job)
|
2016-10-18 06:22:51 -04:00
|
|
|
validate_job_environment!(name, job)
|
2016-07-06 08:24:31 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
def yaml_variables(name)
|
2016-07-20 08:15:18 -04:00
|
|
|
variables = (@variables || {})
|
|
|
|
.merge(job_variables(name))
|
|
|
|
|
2016-07-16 14:10:22 -04:00
|
|
|
variables.map do |key, value|
|
2016-12-19 08:15:47 -05:00
|
|
|
{ key: key.to_s, value: value, public: true }
|
2016-07-16 14:10:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_variables(name)
|
|
|
|
job = @jobs[name.to_sym]
|
|
|
|
return {} unless job
|
|
|
|
|
|
|
|
job[:variables] || {}
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-14 09:23:52 -04:00
|
|
|
def validate_job_stage!(name, job)
|
2016-07-20 08:15:18 -04:00
|
|
|
return unless job[:stage]
|
|
|
|
|
2016-07-14 09:23:52 -04:00
|
|
|
unless job[:stage].is_a?(String) && job[:stage].in?(@stages)
|
|
|
|
raise ValidationError, "#{name} job: stage parameter should be #{@stages.join(", ")}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 08:15:13 -05:00
|
|
|
def validate_job_dependencies!(name, job)
|
2016-07-20 08:15:18 -04:00
|
|
|
return unless job[:dependencies]
|
2016-03-11 08:15:13 -05:00
|
|
|
|
2016-06-23 07:51:07 -04:00
|
|
|
stage_index = @stages.index(job[:stage])
|
2016-03-11 08:15:13 -05:00
|
|
|
|
|
|
|
job[:dependencies].each do |dependency|
|
2016-03-22 10:25:53 -04:00
|
|
|
raise ValidationError, "#{name} job: undefined dependency: #{dependency}" unless @jobs[dependency.to_sym]
|
2016-03-11 08:15:13 -05:00
|
|
|
|
2016-06-23 07:51:07 -04:00
|
|
|
unless @stages.index(@jobs[dependency.to_sym][:stage]) < stage_index
|
2016-03-11 08:15:13 -05:00
|
|
|
raise ValidationError, "#{name} job: dependency #{dependency} is not defined in prior stages"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-18 06:22:51 -04:00
|
|
|
def validate_job_environment!(name, job)
|
|
|
|
return unless job[:environment]
|
|
|
|
return unless job[:environment].is_a?(Hash)
|
|
|
|
|
|
|
|
environment = job[:environment]
|
|
|
|
validate_on_stop_job!(name, environment, environment[:on_stop])
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_on_stop_job!(name, environment, on_stop)
|
|
|
|
return unless on_stop
|
|
|
|
|
|
|
|
on_stop_job = @jobs[on_stop.to_sym]
|
|
|
|
unless on_stop_job
|
|
|
|
raise ValidationError, "#{name} job: on_stop job #{on_stop} is not defined"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment]
|
|
|
|
raise ValidationError, "#{name} job: on_stop job #{on_stop} does not have environment defined"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:name] == environment[:name]
|
|
|
|
raise ValidationError, "#{name} job: on_stop job #{on_stop} have different environment name"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:action] == 'stop'
|
|
|
|
raise ValidationError, "#{name} job: on_stop job #{on_stop} needs to have action stop defined"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-15 09:27:33 -04:00
|
|
|
def process?(only_params, except_params, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
if only_params.present?
|
2016-03-15 09:27:33 -04:00
|
|
|
return false unless matching?(only_params, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if except_params.present?
|
2016-03-15 09:27:33 -04:00
|
|
|
return false if matching?(except_params, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-03-15 09:27:33 -04:00
|
|
|
def matching?(patterns, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
patterns.any? do |pattern|
|
2016-03-15 09:27:33 -04:00
|
|
|
match_ref?(pattern, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-15 11:03:00 -04:00
|
|
|
def match_ref?(pattern, ref, tag, trigger_request)
|
2015-11-02 08:44:06 -05:00
|
|
|
pattern, path = pattern.split('@', 2)
|
|
|
|
return false if path && path != self.path
|
|
|
|
return true if tag && pattern == 'tags'
|
|
|
|
return true if !tag && pattern == 'branches'
|
2016-03-15 21:28:06 -04:00
|
|
|
return true if trigger_request.present? && pattern == 'triggers'
|
2015-11-02 08:44:06 -05:00
|
|
|
|
|
|
|
if pattern.first == "/" && pattern.last == "/"
|
|
|
|
Regexp.new(pattern[1...-1]) =~ ref
|
|
|
|
else
|
|
|
|
pattern == ref
|
|
|
|
end
|
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
2015-12-09 15:58:53 -05:00
|
|
|
end
|