summaryrefslogtreecommitdiff
path: root/lib/gitlab
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-02-23 12:13:44 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-02-23 12:13:44 +0000
commitb286069fdfe9a02beb2a26ce73511159a372002d (patch)
treeef6a92fae93c1a66be3a98921da93383542bd2c1 /lib/gitlab
parente9c3815d3d3e8a9ba07b899c491db38ac621fe57 (diff)
downloadgitlab-ce-b286069fdfe9a02beb2a26ce73511159a372002d.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab')
-rw-r--r--lib/gitlab/ci/variables/builder.rb19
-rw-r--r--lib/gitlab/ci/variables/builder/group.rb48
-rw-r--r--lib/gitlab/database/each_database.rb19
3 files changed, 82 insertions, 4 deletions
diff --git a/lib/gitlab/ci/variables/builder.rb b/lib/gitlab/ci/variables/builder.rb
index 9ef6e7f5fa9..ec2ec3396ea 100644
--- a/lib/gitlab/ci/variables/builder.rb
+++ b/lib/gitlab/ci/variables/builder.rb
@@ -10,6 +10,7 @@ module Gitlab
@pipeline = pipeline
@instance_variables_builder = Builder::Instance.new
@project_variables_builder = Builder::Project.new(project)
+ @group_variables_builder = Builder::Group.new(project.group)
end
def scoped_variables(job, environment:, dependencies:)
@@ -72,9 +73,13 @@ module Gitlab
end
def secret_group_variables(environment:, ref:)
- return [] unless project.group
+ if memoize_secret_variables?
+ memoized_secret_group_variables(environment: environment)
+ else
+ return [] unless project.group
- project.group.ci_variables_for(ref, project, environment: environment)
+ project.group.ci_variables_for(ref, project, environment: environment)
+ end
end
def secret_project_variables(environment:, ref:)
@@ -90,6 +95,7 @@ module Gitlab
attr_reader :pipeline
attr_reader :instance_variables_builder
attr_reader :project_variables_builder
+ attr_reader :group_variables_builder
delegate :project, to: :pipeline
def predefined_variables(job)
@@ -119,6 +125,15 @@ module Gitlab
end
end
+ def memoized_secret_group_variables(environment:)
+ strong_memoize_with(:secret_group_variables, environment) do
+ group_variables_builder
+ .secret_variables(
+ environment: environment,
+ protected_ref: protected_ref?)
+ end
+ end
+
def ci_node_total_value(job)
parallel = job.options&.dig(:parallel)
parallel = parallel.dig(:total) if parallel.is_a?(Hash)
diff --git a/lib/gitlab/ci/variables/builder/group.rb b/lib/gitlab/ci/variables/builder/group.rb
new file mode 100644
index 00000000000..3f3e04038df
--- /dev/null
+++ b/lib/gitlab/ci/variables/builder/group.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ module Variables
+ class Builder
+ class Group
+ include Gitlab::Utils::StrongMemoize
+
+ def initialize(group)
+ @group = group
+ end
+
+ def secret_variables(environment:, protected_ref: false)
+ return [] unless group
+
+ variables = base_scope
+ variables = variables.unprotected unless protected_ref
+ variables = variables.for_environment(environment)
+ variables = variables.group_by(&:group_id)
+ variables = list_of_ids.reverse.flat_map { |group| variables[group.id] }.compact
+ Gitlab::Ci::Variables::Collection.new(variables)
+ end
+
+ private
+
+ attr_reader :group
+
+ def base_scope
+ strong_memoize(:base_scope) do
+ ::Ci::GroupVariable.for_groups(list_of_ids)
+ end
+ end
+
+ def list_of_ids
+ strong_memoize(:list_of_ids) do
+ if group.root_ancestor.use_traversal_ids?
+ [group] + group.ancestors(hierarchy_order: :asc)
+ else
+ [group] + group.ancestors
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/database/each_database.rb b/lib/gitlab/database/each_database.rb
index c3eea0515d4..61d20cfe071 100644
--- a/lib/gitlab/database/each_database.rb
+++ b/lib/gitlab/database/each_database.rb
@@ -4,8 +4,11 @@ module Gitlab
module Database
module EachDatabase
class << self
- def each_database_connection
- Gitlab::Database.database_base_models.each_pair do |connection_name, model|
+ def each_database_connection(only: nil)
+ selected_names = Array.wrap(only)
+ base_models = select_base_models(selected_names)
+
+ base_models.each_pair do |connection_name, model|
connection = model.connection
with_shared_connection(connection, connection_name) do
@@ -28,6 +31,18 @@ module Gitlab
private
+ def select_base_models(names)
+ base_models = Gitlab::Database.database_base_models
+
+ return base_models if names.empty?
+
+ names.each_with_object(HashWithIndifferentAccess.new) do |name, hash|
+ raise ArgumentError, "#{name} is not a valid database name" unless base_models.key?(name)
+
+ hash[name] = base_models[name]
+ end
+ end
+
def with_shared_model_connections(shared_model, &blk)
Gitlab::Database.database_base_models.each_pair do |connection_name, connection_model|
if shared_model.limit_connection_names