Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/ci/config')
-rw-r--r--lib/gitlab/ci/config/entry/include.rb18
-rw-r--r--lib/gitlab/ci/config/entry/include/rules.rb28
-rw-r--r--lib/gitlab/ci/config/entry/include/rules/rule.rb30
-rw-r--r--lib/gitlab/ci/config/entry/inherit/variables.rb11
-rw-r--r--lib/gitlab/ci/config/entry/job.rb3
-rw-r--r--lib/gitlab/ci/config/entry/processable.rb21
-rw-r--r--lib/gitlab/ci/config/entry/rules.rb2
-rw-r--r--lib/gitlab/ci/config/external/file/remote.rb2
-rw-r--r--lib/gitlab/ci/config/external/mapper.rb12
-rw-r--r--lib/gitlab/ci/config/external/rules.rb31
-rw-r--r--lib/gitlab/ci/config/normalizer/matrix_strategy.rb1
11 files changed, 120 insertions, 39 deletions
diff --git a/lib/gitlab/ci/config/entry/include.rb b/lib/gitlab/ci/config/entry/include.rb
index ad0ed00aa6f..368d8f07f8d 100644
--- a/lib/gitlab/ci/config/entry/include.rb
+++ b/lib/gitlab/ci/config/entry/include.rb
@@ -9,8 +9,10 @@ module Gitlab
#
class Include < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
+ include ::Gitlab::Config::Entry::Configurable
+ include ::Gitlab::Config::Entry::Attributable
- ALLOWED_KEYS = %i[local file remote template artifact job project ref].freeze
+ ALLOWED_KEYS = %i[local file remote template artifact job project ref rules].freeze
validations do
validates :config, hash_or_string: true
@@ -27,6 +29,20 @@ module Gitlab
errors.add(:config, "must specify the file where to fetch the config from")
end
end
+
+ with_options allow_nil: true do
+ validates :rules, array_of_hashes: true
+ end
+ end
+
+ entry :rules, ::Gitlab::Ci::Config::Entry::Include::Rules,
+ description: 'List of evaluable Rules to determine file inclusion.',
+ inherit: false
+
+ attributes :rules
+
+ def skip_config_hash_validation?
+ true
end
end
end
diff --git a/lib/gitlab/ci/config/entry/include/rules.rb b/lib/gitlab/ci/config/entry/include/rules.rb
new file mode 100644
index 00000000000..8eaf9e35aaf
--- /dev/null
+++ b/lib/gitlab/ci/config/entry/include/rules.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ class Config
+ module Entry
+ class Include
+ class Rules < ::Gitlab::Config::Entry::ComposableArray
+ include ::Gitlab::Config::Entry::Validatable
+
+ validations do
+ validates :config, presence: true
+ validates :config, type: Array
+ end
+
+ def value
+ @config
+ end
+
+ def composable_class
+ Entry::Include::Rules::Rule
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/config/entry/include/rules/rule.rb b/lib/gitlab/ci/config/entry/include/rules/rule.rb
new file mode 100644
index 00000000000..d3d0f098814
--- /dev/null
+++ b/lib/gitlab/ci/config/entry/include/rules/rule.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ class Config
+ module Entry
+ class Include
+ class Rules::Rule < ::Gitlab::Config::Entry::Node
+ include ::Gitlab::Config::Entry::Validatable
+ include ::Gitlab::Config::Entry::Attributable
+
+ ALLOWED_KEYS = %i[if].freeze
+
+ attributes :if
+
+ validations do
+ validates :config, presence: true
+ validates :config, type: { with: Hash }
+ validates :config, allowed_keys: ALLOWED_KEYS
+
+ with_options allow_nil: true do
+ validates :if, expression: true
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/config/entry/inherit/variables.rb b/lib/gitlab/ci/config/entry/inherit/variables.rb
index aa68833bdb8..adef4d1636a 100644
--- a/lib/gitlab/ci/config/entry/inherit/variables.rb
+++ b/lib/gitlab/ci/config/entry/inherit/variables.rb
@@ -13,9 +13,6 @@ module Gitlab
strategy :ArrayStrategy, if: -> (config) { config.is_a?(Array) }
class BooleanStrategy < ::Gitlab::Config::Entry::Boolean
- def inherit?(_key)
- value
- end
end
class ArrayStrategy < ::Gitlab::Config::Entry::Node
@@ -25,20 +22,12 @@ module Gitlab
validates :config, type: Array
validates :config, array_of_strings: true
end
-
- def inherit?(key)
- value.include?(key.to_s)
- end
end
class UnknownStrategy < ::Gitlab::Config::Entry::Node
def errors
["#{location} should be a bool or array of strings"]
end
-
- def inherit?(key)
- false
- end
end
end
end
diff --git a/lib/gitlab/ci/config/entry/job.rb b/lib/gitlab/ci/config/entry/job.rb
index e6d63969161..bd4d5f33689 100644
--- a/lib/gitlab/ci/config/entry/job.rb
+++ b/lib/gitlab/ci/config/entry/job.rb
@@ -16,11 +16,8 @@ module Gitlab
environment coverage retry parallel interruptible timeout
release dast_configuration secrets].freeze
- REQUIRED_BY_NEEDS = %i[stage].freeze
-
validations do
validates :config, allowed_keys: ALLOWED_KEYS + PROCESSABLE_ALLOWED_KEYS
- validates :config, required_keys: REQUIRED_BY_NEEDS, if: :has_needs?
validates :script, presence: true
with_options allow_nil: true do
diff --git a/lib/gitlab/ci/config/entry/processable.rb b/lib/gitlab/ci/config/entry/processable.rb
index 79dfb0eec1d..3543b5493bd 100644
--- a/lib/gitlab/ci/config/entry/processable.rb
+++ b/lib/gitlab/ci/config/entry/processable.rb
@@ -31,7 +31,7 @@ module Gitlab
with_options allow_nil: true do
validates :extends, array_of_strings_or_string: true
- validates :rules, array_of_hashes: true
+ validates :rules, nested_array_of_hashes: true
validates :resource_group, type: String
end
end
@@ -88,9 +88,6 @@ module Gitlab
validate_against_warnings
end
- # inherit root variables
- @root_variables_value = deps&.variables_value # rubocop:disable Gitlab/ModuleWithInstanceVariables
-
yield if block_given?
end
end
@@ -123,27 +120,13 @@ module Gitlab
stage: stage_value,
extends: extends,
rules: rules_value,
- variables: root_and_job_variables_value, # https://gitlab.com/gitlab-org/gitlab/-/issues/300581
- job_variables: job_variables,
+ job_variables: variables_value.to_h,
root_variables_inheritance: root_variables_inheritance,
only: only_value,
except: except_value,
resource_group: resource_group }.compact
end
- def root_and_job_variables_value
- root_variables = @root_variables_value.to_h # rubocop:disable Gitlab/ModuleWithInstanceVariables
- root_variables = root_variables.select do |key, _|
- inherit_entry&.variables_entry&.inherit?(key)
- end
-
- root_variables.merge(variables_value.to_h)
- end
-
- def job_variables
- variables_value.to_h
- end
-
def root_variables_inheritance
inherit_entry&.variables_entry&.value
end
diff --git a/lib/gitlab/ci/config/entry/rules.rb b/lib/gitlab/ci/config/entry/rules.rb
index bf74f995e80..53e52981471 100644
--- a/lib/gitlab/ci/config/entry/rules.rb
+++ b/lib/gitlab/ci/config/entry/rules.rb
@@ -13,7 +13,7 @@ module Gitlab
end
def value
- @config
+ [@config].flatten
end
def composable_class
diff --git a/lib/gitlab/ci/config/external/file/remote.rb b/lib/gitlab/ci/config/external/file/remote.rb
index 567a86c47e5..4bd8e250d7a 100644
--- a/lib/gitlab/ci/config/external/file/remote.rb
+++ b/lib/gitlab/ci/config/external/file/remote.rb
@@ -45,7 +45,7 @@ module Gitlab
errors.push("Remote file `#{location}` could not be fetched because of HTTP code `#{response.code}` error!")
end
- response.to_s if errors.none?
+ response.body if errors.none?
end
end
end
diff --git a/lib/gitlab/ci/config/external/mapper.rb b/lib/gitlab/ci/config/external/mapper.rb
index 3216d4eaac4..97e4922b2a1 100644
--- a/lib/gitlab/ci/config/external/mapper.rb
+++ b/lib/gitlab/ci/config/external/mapper.rb
@@ -33,6 +33,7 @@ module Gitlab
locations
.compact
.map(&method(:normalize_location))
+ .filter_map(&method(:verify_rules))
.flat_map(&method(:expand_project_files))
.flat_map(&method(:expand_wildcard_paths))
.map(&method(:expand_variables))
@@ -56,6 +57,15 @@ module Gitlab
end
end
+ def verify_rules(location)
+ # Behaves like there is no `rules`
+ return location unless ::Feature.enabled?(:ci_include_rules, context.project, default_enabled: :yaml)
+
+ return unless Rules.new(location[:rules]).evaluate(context).pass?
+
+ location
+ end
+
def expand_project_files(location)
return location unless location[:project]
@@ -65,8 +75,6 @@ module Gitlab
end
def expand_wildcard_paths(location)
- return location unless ::Feature.enabled?(:ci_wildcard_file_paths, context.project, default_enabled: :yaml)
-
# We only support local files for wildcard paths
return location unless location[:local] && location[:local].include?('*')
diff --git a/lib/gitlab/ci/config/external/rules.rb b/lib/gitlab/ci/config/external/rules.rb
new file mode 100644
index 00000000000..5a788427172
--- /dev/null
+++ b/lib/gitlab/ci/config/external/rules.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ class Config
+ module External
+ class Rules
+ def initialize(rule_hashes)
+ @rule_list = Build::Rules::Rule.fabricate_list(rule_hashes)
+ end
+
+ def evaluate(context)
+ Result.new(@rule_list.nil? || match_rule(context))
+ end
+
+ private
+
+ def match_rule(context)
+ @rule_list.find { |rule| rule.matches?(nil, context) }
+ end
+
+ Result = Struct.new(:result) do
+ def pass?
+ !!result
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/config/normalizer/matrix_strategy.rb b/lib/gitlab/ci/config/normalizer/matrix_strategy.rb
index 5cabbc86d3e..312f98f850a 100644
--- a/lib/gitlab/ci/config/normalizer/matrix_strategy.rb
+++ b/lib/gitlab/ci/config/normalizer/matrix_strategy.rb
@@ -43,7 +43,6 @@ module Gitlab
{
name: name,
instance: instance,
- variables: variables, # https://gitlab.com/gitlab-org/gitlab/-/issues/300581
job_variables: variables,
parallel: { total: total }
}.compact