From 9c5977c821c1958709227a7a5976e1faedc53923 Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Sun, 25 Nov 2018 21:26:28 +1300 Subject: [PATCH] Teach Project about #all_clusters This returns a union of the project level clusters and group level clusters associated with this project. --- app/models/clusters/cluster.rb | 1 + app/models/project.rb | 6 ++++++ spec/models/project_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb index a3d26d62a22..e85e7a4e8be 100644 --- a/app/models/clusters/cluster.rb +++ b/app/models/clusters/cluster.rb @@ -4,6 +4,7 @@ module Clusters class Cluster < ActiveRecord::Base include Presentable include Gitlab::Utils::StrongMemoize + include FromUnion self.table_name = 'clusters' diff --git a/app/models/project.rb b/app/models/project.rb index 20e3ada9eb8..8f41629e5e5 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1061,6 +1061,12 @@ class Project < ActiveRecord::Base path end + def all_clusters + group_clusters = Clusters::Cluster.joins(:groups).where(cluster_groups: { group_id: ancestors_upto } ) + + Clusters::Cluster.from_union([clusters, group_clusters]) + end + def items_for(entity) case entity when 'issue' then diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index edb9ff4e734..5cce8d87c96 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3984,6 +3984,27 @@ describe Project do end end + describe '#all_clusters' do + let(:project) { create(:project) } + let(:cluster) { create(:cluster, cluster_type: :project_type, projects: [project]) } + + subject { project.all_clusters } + + it 'returns project level cluster' do + expect(subject).to eq([cluster]) + end + + context 'project belongs to a group' do + let(:group_cluster) { create(:cluster, :group) } + let(:group) { group_cluster.group } + let(:project) { create(:project, group: group) } + + it 'returns clusters for groups of this project' do + expect(subject).to contain_exactly(cluster, group_cluster) + end + end + end + def rugged_config rugged_repo(project.repository).config end