2015-08-25 21:42:46 -04:00
|
|
|
module Ci
|
|
|
|
class GitlabCiYamlProcessor
|
2016-05-25 00:06:09 -04:00
|
|
|
class ValidationError < StandardError; end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-06-17 05:55:55 -04:00
|
|
|
include Gitlab::Ci::Config::Node::LegacyValidationHelpers
|
2016-06-06 05:05:15 -04:00
|
|
|
|
2016-06-29 03:49:46 -04:00
|
|
|
attr_reader :path, :cache, :stages
|
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,
|
2016-07-19 07:23:14 -04:00
|
|
|
allow_failure: job[:allow_failure] || false,
|
|
|
|
when: job[:when] || 'on_success',
|
2016-09-16 05:25:37 -04:00
|
|
|
environment: job[:environment_name],
|
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-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|
|
|
|
|
{ key: key, value: value, public: true }
|
|
|
|
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-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
|