diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
commit | db384e6b19af03b4c3c82a5760d83a3fd79f7982 (patch) | |
tree | 34beaef37df5f47ccbcf5729d7583aae093cffa0 /lib/gitlab/ci/config/yaml | |
parent | 54fd7b1bad233e3944434da91d257fa7f63c3996 (diff) |
Add latest changes from gitlab-org/gitlab@16-3-stable-eev16.3.0-rc42
Diffstat (limited to 'lib/gitlab/ci/config/yaml')
-rw-r--r-- | lib/gitlab/ci/config/yaml/interpolator.rb | 90 | ||||
-rw-r--r-- | lib/gitlab/ci/config/yaml/loader.rb | 11 | ||||
-rw-r--r-- | lib/gitlab/ci/config/yaml/result.rb | 7 |
3 files changed, 11 insertions, 97 deletions
diff --git a/lib/gitlab/ci/config/yaml/interpolator.rb b/lib/gitlab/ci/config/yaml/interpolator.rb deleted file mode 100644 index 2909c2ac798..00000000000 --- a/lib/gitlab/ci/config/yaml/interpolator.rb +++ /dev/null @@ -1,90 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module Ci - class Config - module Yaml - ## - # Config::Yaml::Interpolator performs CI config file interpolation, and surfaces all possible interpolation - # errors. It is designed to provide an external file's validation context too. - # - class Interpolator - attr_reader :config, :args, :current_user, :errors - - def initialize(config, args, current_user: nil) - @config = config - @args = args.to_h - @current_user = current_user - @errors = [] - end - - def valid? - @errors.none? - end - - def to_hash - @result.to_h - end - - def error_message - # Interpolator can have multiple error messages, like: ["interpolation interrupted by errors", "unknown - # interpolation key: `abc`"] ? - # - # We are joining them together into a single one, because only one error can be surfaced when an external - # file gets included and is invalid. The limit to three error messages combined is more than required. - # - @errors.first(3).join(', ') - end - - def interpolate! - return @errors.push(config.error) unless config.valid? - return @result ||= config.content unless config.has_header? - - return @errors.concat(header.errors) unless header.valid? - return @errors.concat(inputs.errors) unless inputs.valid? - return @errors.concat(context.errors) unless context.valid? - return @errors.concat(template.errors) unless template.valid? - - if current_user.present? - ::Gitlab::UsageDataCounters::HLLRedisCounter - .track_event('ci_interpolation_users', values: current_user.id) - end - - @result ||= template.interpolated.to_h.deep_symbolize_keys - end - - private - - def header - @entry ||= Ci::Config::Header::Root.new(config.header).tap do |header| - header.key = 'header' - - header.compose! - end - end - - def content - @content ||= config.content - end - - def spec - @spec ||= header.inputs_value - end - - def inputs - @inputs ||= Ci::Input::Inputs.new(spec, args) - end - - def context - @context ||= Ci::Interpolation::Context.new({ inputs: inputs.to_hash }) - end - - def template - @template ||= ::Gitlab::Ci::Interpolation::Template - .new(content, context) - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/yaml/loader.rb b/lib/gitlab/ci/config/yaml/loader.rb index fb24a2874e4..5d56061a8bb 100644 --- a/lib/gitlab/ci/config/yaml/loader.rb +++ b/lib/gitlab/ci/config/yaml/loader.rb @@ -10,9 +10,8 @@ module Gitlab AVAILABLE_TAGS = [Config::Yaml::Tags::Reference].freeze MAX_DOCUMENTS = 2 - def initialize(content, inputs: {}, current_user: nil) + def initialize(content, inputs: {}) @content = content - @current_user = current_user @inputs = inputs end @@ -21,21 +20,21 @@ module Gitlab return yaml_result unless yaml_result.valid? - interpolator = Yaml::Interpolator.new(yaml_result, inputs, current_user: current_user) + interpolator = Interpolation::Interpolator.new(yaml_result, inputs) interpolator.interpolate! if interpolator.valid? # This Result contains only the interpolated config and does not have a header - Yaml::Result.new(config: interpolator.to_hash, error: nil) + Yaml::Result.new(config: interpolator.to_hash, error: nil, interpolated: interpolator.interpolated?) else - Yaml::Result.new(error: interpolator.error_message) + Yaml::Result.new(error: interpolator.error_message, interpolated: interpolator.interpolated?) end end private - attr_reader :content, :current_user, :inputs + attr_reader :content, :inputs def load_uninterpolated_yaml Yaml::Result.new(config: load_yaml!, error: nil) diff --git a/lib/gitlab/ci/config/yaml/result.rb b/lib/gitlab/ci/config/yaml/result.rb index 6b20eeae203..a68cfde6653 100644 --- a/lib/gitlab/ci/config/yaml/result.rb +++ b/lib/gitlab/ci/config/yaml/result.rb @@ -7,16 +7,21 @@ module Gitlab class Result attr_reader :error, :error_class - def initialize(config: nil, error: nil, error_class: nil) + def initialize(config: nil, error: nil, error_class: nil, interpolated: false) @config = Array.wrap(config) @error = error @error_class = error_class + @interpolated = interpolated end def valid? error.nil? end + def interpolated? + !!@interpolated + end + def has_header? return false unless @config.first.is_a?(Hash) |