2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# 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.
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class YamlProcessor
|
2020-11-24 15:15:51 +05:30
|
|
|
ValidationError = Class.new(StandardError)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def initialize(config_content, opts = {})
|
|
|
|
@config_content = config_content
|
|
|
|
@opts = opts
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def execute
|
2022-08-13 15:12:31 +05:30
|
|
|
Gitlab::Ci::YamlProcessor::FeatureFlags.with_actor(project) do
|
|
|
|
parse_config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parse_config
|
2020-11-24 15:15:51 +05:30
|
|
|
if @config_content.blank?
|
|
|
|
return Result.new(errors: ['Please provide content of .gitlab-ci.yml'])
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
@ci_config = Gitlab::Ci::Config.new(@config_content, **@opts)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
unless @ci_config.valid?
|
2020-11-24 15:15:51 +05:30
|
|
|
return Result.new(ci_config: @ci_config, errors: @ci_config.errors, warnings: @ci_config.warnings)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
run_logical_validations!
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
Result.new(ci_config: @ci_config, warnings: @ci_config&.warnings)
|
2020-07-28 23:09:34 +05:30
|
|
|
rescue Gitlab::Ci::Config::ConfigError => e
|
2020-11-24 15:15:51 +05:30
|
|
|
Result.new(ci_config: @ci_config, errors: [e.message], warnings: @ci_config&.warnings)
|
|
|
|
rescue ValidationError => e
|
|
|
|
Result.new(ci_config: @ci_config, errors: [e.message], warnings: @ci_config&.warnings)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def project
|
|
|
|
@opts[:project]
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def run_logical_validations!
|
2018-03-17 18:26:18 +05:30
|
|
|
@stages = @ci_config.stages
|
2020-11-24 15:15:51 +05:30
|
|
|
@jobs = @ci_config.normalized_jobs
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
@jobs.each do |name, job|
|
2020-11-24 15:15:51 +05:30
|
|
|
validate_job!(name, job)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
check_circular_dependencies
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
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)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def validate_job_stage!(name, job)
|
|
|
|
return unless job[:stage]
|
|
|
|
|
|
|
|
unless job[:stage].is_a?(String) && job[:stage].in?(@stages)
|
2020-07-28 23:09:34 +05:30
|
|
|
error!("#{name} job: chosen stage does not exist; available stages are #{@stages.join(", ")}")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_job_dependencies!(name, job)
|
|
|
|
return unless job[:dependencies]
|
|
|
|
|
|
|
|
job[:dependencies].each do |dependency|
|
2020-04-22 19:07:51 +05:30
|
|
|
validate_job_dependency!(name, dependency)
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def validate_dynamic_child_pipeline_dependencies!(name, job)
|
|
|
|
return unless includes = job.dig(:trigger, :include)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
Array(includes).each do |included|
|
|
|
|
next unless included.is_a?(Hash)
|
|
|
|
next unless dependency = included[:job]
|
|
|
|
|
|
|
|
validate_job_dependency!(name, dependency)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def validate_job_needs!(name, job)
|
2023-07-09 08:55:56 +05:30
|
|
|
validate_needs_specification!(name, job.dig(:needs, :job))
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
job[:rules]&.each do |rule|
|
|
|
|
validate_needs_specification!(name, rule.dig(:needs, :job))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_needs_specification!(name, needs)
|
|
|
|
return unless needs
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
needs.each do |need|
|
2023-06-20 00:43:36 +05:30
|
|
|
validate_job_dependency!(name, need[:name], 'need', optional: need[:optional])
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
duplicated_needs =
|
|
|
|
needs
|
|
|
|
.group_by { |need| need[:name] }
|
|
|
|
.select { |_, items| items.count > 1 }
|
|
|
|
.keys
|
|
|
|
|
|
|
|
unless duplicated_needs.empty?
|
|
|
|
error!("#{name} has the following needs duplicated: #{duplicated_needs.join(', ')}.")
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def validate_job_dependency!(name, dependency, dependency_type = 'dependency', optional: false)
|
2020-04-22 19:07:51 +05:30
|
|
|
unless @jobs[dependency.to_sym]
|
2023-06-20 00:43:36 +05:30
|
|
|
# Here, we ignore the optional needed job if it is not in the result YAML due to the `include`
|
|
|
|
# rules. In `lib/gitlab/ci/pipeline/seed/build.rb`, we use `optional` again to ignore the
|
|
|
|
# optional needed job in case it is excluded from the pipeline due to the job's rules.
|
|
|
|
return if optional
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
error!("#{name} job: undefined #{dependency_type}: #{dependency}")
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
job_stage_index = stage_index(name)
|
|
|
|
dependency_stage_index = stage_index(dependency)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
unless dependency_stage_index.present? && dependency_stage_index <= job_stage_index
|
|
|
|
error!("#{name} job: #{dependency_type} #{dependency} is not defined in current or prior stages")
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def stage_index(name)
|
|
|
|
stage = @jobs.dig(name.to_sym, :stage)
|
|
|
|
@stages.index(stage)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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-28 23:09:34 +05:30
|
|
|
error!("#{name} job: on_stop job #{on_stop} is not defined")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment]
|
2020-07-28 23:09:34 +05:30
|
|
|
error!("#{name} job: on_stop job #{on_stop} does not have environment defined")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:name] == environment[:name]
|
2020-07-28 23:09:34 +05:30
|
|
|
error!("#{name} job: on_stop job #{on_stop} have different environment name")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless on_stop_job[:environment][:action] == 'stop'
|
2020-07-28 23:09:34 +05:30
|
|
|
error!("#{name} job: on_stop job #{on_stop} needs to have action stop defined")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def check_circular_dependencies
|
|
|
|
jobs = @jobs.values.to_h do |job|
|
|
|
|
name = job[:name].to_s
|
|
|
|
needs = job.dig(:needs, :job).to_a
|
|
|
|
|
|
|
|
[name, needs.map { |need| need[:name].to_s }]
|
|
|
|
end
|
|
|
|
|
|
|
|
Dag.check_circular_dependencies!(jobs)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def error!(message)
|
2021-06-08 01:23:25 +05:30
|
|
|
raise ValidationError, message
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|