2018-10-26 04:12:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
# This is the CI Linter component that runs the syntax validations
|
|
|
|
# while parsing the YAML config into a data structure that is
|
|
|
|
# then presented to the caller as result object.
|
|
|
|
# After syntax validations (done by Ci::Config), this component also
|
|
|
|
# runs logical validation on the built data structure.
|
2017-09-06 11:23:24 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class YamlProcessor
|
2020-09-01 18:10:48 +00:00
|
|
|
ValidationError = Class.new(StandardError)
|
2017-09-06 11:23:24 +00:00
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
def initialize(config_content, opts = {})
|
|
|
|
@config_content = config_content
|
|
|
|
@opts = opts
|
|
|
|
end
|
2020-07-13 12:09:18 +00:00
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
def execute
|
|
|
|
if @config_content.blank?
|
|
|
|
return Result.new(errors: ['Please provide content of .gitlab-ci.yml'])
|
2019-12-16 12:07:43 +00:00
|
|
|
end
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
@ci_config = Gitlab::Ci::Config.new(@config_content, **@opts)
|
2017-09-06 11:23:24 +00:00
|
|
|
|
|
|
|
unless @ci_config.valid?
|
2020-09-01 18:10:48 +00:00
|
|
|
return Result.new(ci_config: @ci_config, errors: @ci_config.errors, warnings: @ci_config.warnings)
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
run_logical_validations!
|
2019-12-16 12:07:43 +00:00
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
Result.new(ci_config: @ci_config, warnings: @ci_config&.warnings)
|
2020-07-13 12:09:18 +00:00
|
|
|
|
|
|
|
rescue Gitlab::Ci::Config::ConfigError => e
|
2020-09-01 18:10:48 +00:00
|
|
|
Result.new(ci_config: @ci_config, errors: [e.message], warnings: @ci_config&.warnings)
|
2017-09-06 11:23:24 +00:00
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
rescue ValidationError => e
|
|
|
|
Result.new(ci_config: @ci_config, errors: [e.message], warnings: @ci_config&.warnings)
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
2017-09-14 11:53:02 +00:00
|
|
|
private
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
def run_logical_validations!
|
2017-09-06 11:23:24 +00:00
|
|
|
@stages = @ci_config.stages
|
2020-09-01 18:10:48 +00:00
|
|
|
@jobs = @ci_config.normalized_jobs
|
2017-09-06 11:23:24 +00:00
|
|
|
|
|
|
|
@jobs.each do |name, job|
|
2020-09-01 18:10:48 +00:00
|
|
|
validate_job!(name, job)
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
def validate_job!(name, job)
|
|
|
|
validate_job_stage!(name, job)
|
|
|
|
validate_job_dependencies!(name, job)
|
|
|
|
validate_job_needs!(name, job)
|
|
|
|
validate_dynamic_child_pipeline_dependencies!(name, job)
|
|
|
|
validate_job_environment!(name, job)
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_job_stage!(name, job)
|
|
|
|
return unless job[:stage]
|
|
|
|
|
|
|
|
unless job[:stage].is_a?(String) && job[:stage].in?(@stages)
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: chosen stage does not exist; available stages are #{@stages.join(", ")}")
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_job_dependencies!(name, job)
|
|
|
|
return unless job[:dependencies]
|
|
|
|
|
2020-04-06 12:10:44 +00:00
|
|
|
job[:dependencies].each do |dependency|
|
2020-04-09 15:09:29 +00:00
|
|
|
validate_job_dependency!(name, dependency)
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 12:06:01 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
def validate_dynamic_child_pipeline_dependencies!(name, job)
|
|
|
|
return unless includes = job.dig(:trigger, :include)
|
2020-04-03 15:09:56 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
Array(includes).each do |included|
|
|
|
|
next unless included.is_a?(Hash)
|
|
|
|
next unless dependency = included[:job]
|
|
|
|
|
|
|
|
validate_job_dependency!(name, dependency)
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-01 14:26:49 +00:00
|
|
|
def validate_job_needs!(name, job)
|
2020-04-09 15:09:29 +00:00
|
|
|
return unless needs = job.dig(:needs, :job)
|
2019-08-01 14:26:49 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
needs.each do |need|
|
|
|
|
validate_job_dependency!(name, need[:name], 'need')
|
|
|
|
end
|
|
|
|
end
|
2019-11-08 12:06:32 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
def validate_job_dependency!(name, dependency, dependency_type = 'dependency')
|
|
|
|
unless @jobs[dependency.to_sym]
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: undefined #{dependency_type}: #{dependency}")
|
2020-04-09 15:09:29 +00:00
|
|
|
end
|
2019-08-01 14:26:49 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
job_stage_index = stage_index(name)
|
|
|
|
dependency_stage_index = stage_index(dependency)
|
2019-08-01 14:26:49 +00:00
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
# A dependency might be defined later in the configuration
|
|
|
|
# with a stage that does not exist
|
|
|
|
unless dependency_stage_index.present? && dependency_stage_index < job_stage_index
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: #{dependency_type} #{dependency} is not defined in prior stages")
|
2020-04-06 12:10:44 +00:00
|
|
|
end
|
2020-04-03 15:09:56 +00:00
|
|
|
end
|
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
def stage_index(name)
|
|
|
|
stage = @jobs.dig(name.to_sym, :stage)
|
|
|
|
@stages.index(stage)
|
|
|
|
end
|
|
|
|
|
2017-09-06 11:23:24 +00: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
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: on_stop job #{on_stop} is not defined")
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment]
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: on_stop job #{on_stop} does not have environment defined")
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:name] == environment[:name]
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: on_stop job #{on_stop} have different environment name")
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:action] == 'stop'
|
2020-07-13 12:09:18 +00:00
|
|
|
error!("#{name} job: on_stop job #{on_stop} needs to have action stop defined")
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
end
|
2020-09-01 18:10:48 +00:00
|
|
|
|
|
|
|
def error!(message)
|
|
|
|
raise ValidationError.new(message)
|
|
|
|
end
|
2017-09-06 11:23:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|