summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/clusters/clusters_controller.rb4
-rw-r--r--app/finders/cluster_ancestors_finder.rb31
-rw-r--r--spec/finders/cluster_ancestors_finder_spec.rb8
3 files changed, 41 insertions, 2 deletions
diff --git a/app/controllers/clusters/clusters_controller.rb b/app/controllers/clusters/clusters_controller.rb
index 2e9c77ae55c..a6d40062c46 100644
--- a/app/controllers/clusters/clusters_controller.rb
+++ b/app/controllers/clusters/clusters_controller.rb
@@ -18,8 +18,8 @@ class Clusters::ClustersController < Clusters::BaseController
STATUS_POLLING_INTERVAL = 10_000
def index
- clusters = ClustersFinder.new(clusterable, current_user, :all).execute
- @clusters = clusters.page(params[:page]).per(20)
+ clusters = ClusterAncestorsFinder.new(clusterable.subject, current_user).execute
+ @clusters = Kaminari.paginate_array(clusters).page(params[:page]).per(20)
end
def new
diff --git a/app/finders/cluster_ancestors_finder.rb b/app/finders/cluster_ancestors_finder.rb
new file mode 100644
index 00000000000..0d23599459f
--- /dev/null
+++ b/app/finders/cluster_ancestors_finder.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+class ClusterAncestorsFinder
+ def initialize(clusterable, user)
+ @clusterable = clusterable
+ @user = user
+ end
+
+ def execute
+ clusterable.clusters + ancestor_clusters
+ end
+
+ private
+
+ attr_reader :clusterable, :user
+
+ def ancestor_clusters
+ Gitlab::GroupHierarchy.new(base_query).base_and_ancestors.flat_map do |group|
+ group.clusters
+ end
+ end
+
+ def base_query
+ case clusterable
+ when Project
+ Group.joins(:projects).where(projects: { id: clusterable.id })
+ when Group
+ Group.where(id: clusterable.parent_id)
+ end
+ end
+end
diff --git a/spec/finders/cluster_ancestors_finder_spec.rb b/spec/finders/cluster_ancestors_finder_spec.rb
new file mode 100644
index 00000000000..d421fe23c27
--- /dev/null
+++ b/spec/finders/cluster_ancestors_finder_spec.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe ClusterAncestorsFinder do
+ let(:group) { create(:group) }
+ let(:project) { create(:project, group: group) }
+end