Commit d887ba7a authored by Furkan Ayhan's avatar Furkan Ayhan Committed by Kamil Trzciński

Resolve cross-db queries for Runner scopes

The changes are behind the FF ci_find_runners_by_ci_mirrors
parent f1083234
......@@ -112,6 +112,13 @@ module Ci
from("(#{union_sql}) #{table_name}")
}
scope :belonging_to_group_and_ancestors, -> (group_id) {
group_self_and_ancestors_ids = ::Group.find_by(id: group_id)&.self_and_ancestor_ids
joins(:runner_namespaces)
.where(ci_runner_namespaces: { namespace_id: group_self_and_ancestors_ids })
}
# deprecated
# split this into: belonging_to_group & belonging_to_group_and_ancestors
scope :legacy_belonging_to_group, -> (group_id, include_ancestors: false) {
......
......@@ -61,8 +61,12 @@ module Analytics
# rubocop: enable CodeReuse/ActiveRecord
def runner_configured
::Gitlab::Database.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/337541') do
Ci::Runner.active.legacy_belonging_to_group_or_project(snapshot_groups, snapshot_project_ids).exists?
if ::Feature.enabled?(:ci_find_runners_by_ci_mirrors, enabled_namespace.namespace, default_enabled: :yaml)
Ci::Runner.active.belonging_to_group_or_project_descendants(enabled_namespace.namespace.id).exists?
else
::Gitlab::Database.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/337541') do
Ci::Runner.active.legacy_belonging_to_group_or_project(snapshot_groups, snapshot_project_ids).exists?
end
end
end
......
......@@ -61,7 +61,7 @@ RSpec.describe Analytics::DevopsAdoption::SnapshotCalculator do
it { is_expected.to eq false }
end
describe 'runner_configured' do
shared_examples 'runner_configured' do
subject { data[:runner_configured] }
let!(:inactive_runner) { create(:ci_runner, :project, active: false) }
......@@ -77,6 +77,16 @@ RSpec.describe Analytics::DevopsAdoption::SnapshotCalculator do
it { is_expected.to eq false }
end
it_behaves_like 'runner_configured'
context 'when the FF ci_find_runners_by_ci_mirrors is disabled' do
before do
stub_feature_flags(ci_find_runners_by_ci_mirrors: false)
end
it_behaves_like 'runner_configured'
end
describe 'pipeline_succeeded' do
subject { data[:pipeline_succeeded] }
......
......@@ -229,7 +229,12 @@ module API
use :pagination
end
get ':id/runners' do
runners = ::Ci::Runner.legacy_belonging_to_group(user_group.id, include_ancestors: true)
runners = if ::Feature.enabled?(:ci_find_runners_by_ci_mirrors, user_group, default_enabled: :yaml)
::Ci::Runner.belonging_to_group_and_ancestors(user_group.id)
else
::Ci::Runner.legacy_belonging_to_group(user_group.id, include_ancestors: true)
end
runners = apply_filter(runners, params)
present paginate(runners), with: Entities::Ci::Runner
......
......@@ -1438,6 +1438,16 @@ RSpec.describe Ci::Runner do
end
end
describe '.belonging_to_group_and_ancestors' do
let_it_be(:parent_group) { create(:group) }
let_it_be(:parent_runner) { create(:ci_runner, :group, groups: [parent_group]) }
let_it_be(:group) { create(:group, parent: parent_group) }
it 'returns the group runner from the parent group' do
expect(described_class.belonging_to_group_and_ancestors(group.id)).to contain_exactly(parent_runner)
end
end
describe '.belonging_to_group_or_project_descendants' do
it 'returns the specific group runners' do
group1 = create(:group)
......
......@@ -980,7 +980,7 @@ RSpec.describe API::Ci::Runners do
end
end
describe 'GET /groups/:id/runners' do
shared_context 'GET /groups/:id/runners' do
context 'authorized user with maintainer privileges' do
it 'returns all runners' do
get api("/groups/#{group.id}/runners", user)
......@@ -1048,6 +1048,16 @@ RSpec.describe API::Ci::Runners do
end
end
it_behaves_like 'GET /groups/:id/runners'
context 'when the FF ci_find_runners_by_ci_mirrors is disabled' do
before do
stub_feature_flags(ci_find_runners_by_ci_mirrors: false)
end
it_behaves_like 'GET /groups/:id/runners'
end
describe 'POST /projects/:id/runners' do
context 'authorized user' do
let_it_be(:project_runner2) { create(:ci_runner, :project, projects: [project2]) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment