2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
2018-12-05 23:21:45 +05:30
|
|
|
#
|
2016-06-16 23:09:34 +05:30
|
|
|
# Base GitLab CI Configuration facade
|
|
|
|
#
|
|
|
|
class Config
|
2018-11-20 20:47:30 +05:30
|
|
|
ConfigError = Class.new(StandardError)
|
2019-12-21 20:55:43 +05:30
|
|
|
TIMEOUT_SECONDS = 30.seconds
|
|
|
|
TIMEOUT_MESSAGE = 'Resolving config took longer than expected'
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
RESCUE_ERRORS = [
|
|
|
|
Gitlab::Config::Loader::FormatError,
|
|
|
|
Extendable::ExtensionError,
|
|
|
|
External::Processor::IncludeError
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
attr_reader :root
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def initialize(config, project: nil, sha: nil, user: nil)
|
2019-12-21 20:55:43 +05:30
|
|
|
@context = build_context(project: project, sha: sha, user: user)
|
|
|
|
|
|
|
|
if Feature.enabled?(:ci_limit_yaml_expansion, project, default_enabled: true)
|
|
|
|
@context.set_deadline(TIMEOUT_SECONDS)
|
|
|
|
end
|
|
|
|
|
|
|
|
@config = expand_config(config)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
@root = Entry::Root.new(@config)
|
|
|
|
@root.compose!
|
|
|
|
|
|
|
|
rescue *rescue_errors => e
|
2018-11-20 20:47:30 +05:30
|
|
|
raise Config::ConfigError, e.message
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def valid?
|
2019-09-30 21:07:59 +05:30
|
|
|
@root.valid?
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
2019-09-30 21:07:59 +05:30
|
|
|
@root.errors
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def to_hash
|
|
|
|
@config
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
##
|
|
|
|
# Temporary method that should be removed after refactoring
|
|
|
|
#
|
|
|
|
def variables
|
2019-09-30 21:07:59 +05:30
|
|
|
root.variables_value
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def stages
|
2019-09-30 21:07:59 +05:30
|
|
|
root.stages_value
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2019-09-30 21:07:59 +05:30
|
|
|
root.jobs_value
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
private
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def expand_config(config)
|
|
|
|
build_config(config)
|
|
|
|
|
|
|
|
rescue Gitlab::Config::Loader::Yaml::DataTooLargeError => e
|
2020-01-01 13:55:28 +05:30
|
|
|
track_and_raise_for_dev_exception(e)
|
2019-12-21 20:55:43 +05:30
|
|
|
raise Config::ConfigError, e.message
|
|
|
|
|
|
|
|
rescue Gitlab::Ci::Config::External::Context::TimeoutError => e
|
2020-01-01 13:55:28 +05:30
|
|
|
track_and_raise_for_dev_exception(e)
|
2019-12-21 20:55:43 +05:30
|
|
|
raise Config::ConfigError, TIMEOUT_MESSAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_config(config)
|
2019-02-15 15:39:39 +05:30
|
|
|
initial_config = Gitlab::Config::Loader::Yaml.new(config).load!
|
2019-12-21 20:55:43 +05:30
|
|
|
initial_config = Config::External::Processor.new(initial_config, @context).perform
|
|
|
|
initial_config = Config::Extendable.new(initial_config).to_hash
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
if Feature.enabled?(:ci_pre_post_pipeline_stages, @context.project, default_enabled: true)
|
|
|
|
initial_config = Config::EdgeStagesInjector.new(initial_config).to_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
initial_config
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def build_context(project:, sha:, user:)
|
|
|
|
Config::External::Context.new(
|
2019-02-15 15:39:39 +05:30
|
|
|
project: project,
|
2019-07-31 22:56:46 +05:30
|
|
|
sha: sha || project&.repository&.root_ref_sha,
|
2019-12-21 20:55:43 +05:30
|
|
|
user: user)
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def track_and_raise_for_dev_exception(error)
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error, @context.sentry_payload)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
# Overriden in EE
|
|
|
|
def rescue_errors
|
|
|
|
RESCUE_ERRORS
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Gitlab::Ci::Config.prepend_if_ee('EE::Gitlab::Ci::ConfigEE')
|