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
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def jobs_for_ref(ref, tag = false, source = nil)
|
2016-07-16 14:10:22 -04:00
|
|
|
@jobs.select do |_, job|
|
2017-06-01 15:55:57 -04:00
|
|
|
process?(job[:only], job[:except], ref, tag, source)
|
2016-06-02 07:52:19 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def jobs_for_stage_and_ref(stage, ref, tag = false, source = nil)
|
|
|
|
jobs_for_ref(ref, tag, source).select do |_, job|
|
2016-07-16 14:10:22 -04:00
|
|
|
job[:stage] == stage
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def builds_for_ref(ref, tag = false, source = nil)
|
|
|
|
jobs_for_ref(ref, tag, source).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
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def builds_for_stage_and_ref(stage, ref, tag = false, source = nil)
|
|
|
|
jobs_for_stage_and_ref(stage, ref, tag, source).map do |name, _|
|
2016-07-19 07:23:14 -04:00
|
|
|
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
|
|
|
|
|
2017-06-02 06:16:11 -04:00
|
|
|
def stage_seeds(pipeline)
|
|
|
|
seeds = @stages.uniq.map do |stage|
|
|
|
|
builds = builds_for_stage_and_ref(
|
2017-06-07 09:45:24 -04:00
|
|
|
stage, pipeline.ref, pipeline.tag?, pipeline.source)
|
2017-06-02 06:16:11 -04:00
|
|
|
|
|
|
|
Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any?
|
2017-05-30 06:48:05 -04:00
|
|
|
end
|
2017-06-02 06:16:11 -04:00
|
|
|
|
|
|
|
seeds.compact
|
2017-05-30 06:48:05 -04:00
|
|
|
end
|
|
|
|
|
2016-07-19 07:23:14 -04:00
|
|
|
def build_attributes(name)
|
|
|
|
job = @jobs[name.to_sym] || {}
|
2017-05-30 06:48:05 -04:00
|
|
|
|
|
|
|
{ stage_idx: @stages.index(job[:stage]),
|
2016-07-19 07:23:14 -04:00
|
|
|
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],
|
2017-05-03 07:22:03 -04:00
|
|
|
environment: job[:environment]
|
2017-05-30 09:30:45 -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
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def process?(only_params, except_params, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
if only_params.present?
|
2017-06-01 15:55:57 -04:00
|
|
|
return false unless matching?(only_params, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if except_params.present?
|
2017-06-01 15:55:57 -04:00
|
|
|
return false if matching?(except_params, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-06-01 15:55:57 -04:00
|
|
|
def matching?(patterns, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
patterns.any? do |pattern|
|
2017-06-05 11:15:15 -04:00
|
|
|
pattern, path = pattern.split('@', 2)
|
2017-06-07 09:24:11 -04:00
|
|
|
matches_path?(path) && matches_pattern?(pattern, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-07 09:24:11 -04:00
|
|
|
def matches_path?(path)
|
|
|
|
return true unless path
|
2017-06-07 09:26:04 -04:00
|
|
|
|
2017-06-07 09:24:11 -04:00
|
|
|
path == self.path
|
2017-06-05 11:15:15 -04:00
|
|
|
end
|
|
|
|
|
2017-06-07 09:14:36 -04:00
|
|
|
def matches_pattern?(pattern, ref, tag, source)
|
2015-11-02 08:44:06 -05:00
|
|
|
return true if tag && pattern == 'tags'
|
|
|
|
return true if !tag && pattern == 'branches'
|
2017-06-05 11:15:15 -04:00
|
|
|
return true if source_to_pattern(source) == pattern
|
2015-11-02 08:44:06 -05:00
|
|
|
|
|
|
|
if pattern.first == "/" && pattern.last == "/"
|
|
|
|
Regexp.new(pattern[1...-1]) =~ ref
|
|
|
|
else
|
|
|
|
pattern == ref
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 13:02:56 -04:00
|
|
|
|
|
|
|
def source_to_pattern(source)
|
2017-06-07 09:59:06 -04:00
|
|
|
if %w[api external web].include?(source)
|
2017-06-07 09:14:36 -04:00
|
|
|
source
|
|
|
|
else
|
2017-06-07 09:26:04 -04:00
|
|
|
source&.pluralize
|
2017-06-07 09:14:36 -04:00
|
|
|
end
|
2017-06-02 13:02:56 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
2015-12-09 15:58:53 -05:00
|
|
|
end
|