2018-10-26 04:12:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-03 12:20:34 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
2018-09-07 18:53:28 +00:00
|
|
|
#
|
2016-06-09 11:01:19 +00:00
|
|
|
# Base GitLab CI Configuration facade
|
|
|
|
#
|
2016-06-03 12:20:34 +00:00
|
|
|
class Config
|
2018-08-17 13:04:58 +00:00
|
|
|
ConfigError = Class.new(StandardError)
|
2019-09-24 18:06:05 +00:00
|
|
|
TIMEOUT_SECONDS = 30.seconds
|
|
|
|
TIMEOUT_MESSAGE = 'Resolving config took longer than expected'
|
2018-08-17 13:04:58 +00:00
|
|
|
|
2019-06-06 18:49:04 +00:00
|
|
|
RESCUE_ERRORS = [
|
|
|
|
Gitlab::Config::Loader::FormatError,
|
|
|
|
Extendable::ExtensionError,
|
|
|
|
External::Processor::IncludeError
|
|
|
|
].freeze
|
|
|
|
|
2019-06-18 10:36:07 +00:00
|
|
|
attr_reader :root
|
|
|
|
|
2020-03-06 15:08:05 +00:00
|
|
|
def initialize(config, project: nil, sha: nil, user: nil, parent_pipeline: nil)
|
|
|
|
@context = build_context(project: project, sha: sha, user: user, parent_pipeline: parent_pipeline)
|
|
|
|
@context.set_deadline(TIMEOUT_SECONDS)
|
2019-09-24 18:06:05 +00:00
|
|
|
|
|
|
|
@config = expand_config(config)
|
2018-09-08 12:55:36 +00:00
|
|
|
|
2019-06-18 10:36:07 +00:00
|
|
|
@root = Entry::Root.new(@config)
|
|
|
|
@root.compose!
|
2019-07-02 06:23:06 +00:00
|
|
|
|
2019-06-06 18:49:04 +00:00
|
|
|
rescue *rescue_errors => e
|
2018-09-11 14:29:48 +00:00
|
|
|
raise Config::ConfigError, e.message
|
2016-06-03 12:20:34 +00:00
|
|
|
end
|
|
|
|
|
2016-06-09 12:48:40 +00:00
|
|
|
def valid?
|
2019-06-18 10:36:07 +00:00
|
|
|
@root.valid?
|
2016-06-09 12:48:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
2019-06-18 10:36:07 +00:00
|
|
|
@root.errors
|
2016-06-09 12:48:40 +00:00
|
|
|
end
|
|
|
|
|
2016-06-03 12:20:34 +00:00
|
|
|
def to_hash
|
|
|
|
@config
|
|
|
|
end
|
2016-11-18 13:30:27 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Temporary method that should be removed after refactoring
|
|
|
|
#
|
|
|
|
def variables
|
2019-06-18 10:36:07 +00:00
|
|
|
root.variables_value
|
2016-11-18 13:30:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stages
|
2019-06-18 10:36:07 +00:00
|
|
|
root.stages_value
|
2016-11-18 13:30:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2019-06-18 10:36:07 +00:00
|
|
|
root.jobs_value
|
2016-11-18 13:30:27 +00:00
|
|
|
end
|
2018-08-17 13:04:58 +00:00
|
|
|
|
2018-09-08 12:55:36 +00:00
|
|
|
private
|
|
|
|
|
2019-09-24 18:06:05 +00:00
|
|
|
def expand_config(config)
|
|
|
|
build_config(config)
|
|
|
|
|
|
|
|
rescue Gitlab::Config::Loader::Yaml::DataTooLargeError => e
|
2019-12-13 12:07:41 +00:00
|
|
|
track_and_raise_for_dev_exception(e)
|
2019-09-24 18:06:05 +00:00
|
|
|
raise Config::ConfigError, e.message
|
|
|
|
|
|
|
|
rescue Gitlab::Ci::Config::External::Context::TimeoutError => e
|
2019-12-13 12:07:41 +00:00
|
|
|
track_and_raise_for_dev_exception(e)
|
2019-09-24 18:06:05 +00:00
|
|
|
raise Config::ConfigError, TIMEOUT_MESSAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_config(config)
|
2018-11-29 11:44:48 +00:00
|
|
|
initial_config = Gitlab::Config::Loader::Yaml.new(config).load!
|
2019-09-24 18:06:05 +00:00
|
|
|
initial_config = Config::External::Processor.new(initial_config, @context).perform
|
2019-10-17 12:07:33 +00:00
|
|
|
initial_config = Config::Extendable.new(initial_config).to_hash
|
2018-09-07 19:33:06 +00:00
|
|
|
|
2019-10-17 12:07:33 +00:00
|
|
|
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-08-17 13:04:58 +00:00
|
|
|
end
|
2018-09-07 20:03:05 +00:00
|
|
|
|
2020-03-06 15:08:05 +00:00
|
|
|
def build_context(project:, sha:, user:, parent_pipeline:)
|
2019-09-24 18:06:05 +00:00
|
|
|
Config::External::Context.new(
|
2018-12-01 11:39:13 +00:00
|
|
|
project: project,
|
2019-04-05 10:18:10 +00:00
|
|
|
sha: sha || project&.repository&.root_ref_sha,
|
2020-03-06 15:08:05 +00:00
|
|
|
user: user,
|
|
|
|
parent_pipeline: parent_pipeline)
|
2019-09-24 18:06:05 +00:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
def track_and_raise_for_dev_exception(error)
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error, @context.sentry_payload)
|
2018-09-07 20:03:05 +00:00
|
|
|
end
|
2019-06-06 18:49:04 +00:00
|
|
|
|
|
|
|
# Overriden in EE
|
|
|
|
def rescue_errors
|
|
|
|
RESCUE_ERRORS
|
|
|
|
end
|
2016-06-03 12:20:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Gitlab::Ci::Config.prepend_if_ee('EE::Gitlab::Ci::ConfigEE')
|