Commit 8324b2d7 authored by Etienne Baqué's avatar Etienne Baqué

Merge branch 'optimize-deploy-key-presenter' into 'master'

Optimize Deploy Key Presenter [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!56305
parents 0e8b8a6b 40f2bc10
......@@ -14,12 +14,20 @@ module Projects
@key ||= DeployKey.new.tap { |dk| dk.deploy_keys_projects.build }
end
# It includes:
# - The deploy keys enabled in the project.
def enabled_keys
strong_memoize(:enabled_keys) do
project.deploy_keys.with_projects
end
end
# NOTE: This method is redundant. Use `available_project_keys` and `available_public_keys` instead.
# It includes:
# - Enabled deploy keys in projects that can be accessed by the user.
# - Instance-level public deploy keys.
# It excludes:
# - The deploy keys enabled in the project.
def available_keys
strong_memoize(:available_keys) do
current_user
......@@ -29,22 +37,46 @@ module Projects
end
end
# It includes:
# - Enabled deploy keys in projects that can be accessed by the user.
# It excludes:
# - The deploy keys enabled in the project
def available_project_keys
strong_memoize(:available_project_keys) do
current_user
.project_deploy_keys
.id_not_in(enabled_keys.select(:id))
.with_projects
if Feature.enabled?(:optimize_deploy_keys_presenter, project, default_enabled: :yaml)
strong_memoize(:available_project_keys) do
current_user.project_deploy_keys.with_projects - enabled_keys
end
else
strong_memoize(:legacy_available_project_keys) do
current_user
.project_deploy_keys
.id_not_in(enabled_keys.select(:id))
.with_projects
end
end
end
# It includes:
# - Instance-level public deploy keys.
# It excludes:
# - The deploy keys enabled in the project.
def available_public_keys
strong_memoize(:available_public_keys) do
DeployKey
.are_public
.id_not_in(enabled_keys.select(:id))
.id_not_in(available_project_keys.select(:id))
.with_projects
if Feature.enabled?(:optimize_deploy_keys_presenter, project, default_enabled: :yaml)
strong_memoize(:available_public_keys) do
DeployKey.are_public.with_projects - enabled_keys
end
else
strong_memoize(:legacy_available_public_keys) do
# This also excludes "Enabled deploy keys in projects that can be accessed by the user".
# However, this means we are filtering out a public key that enabled
# in the other project, which should be also available for this project.
# We should expose the public keys that has not been enabled on the project yet.
DeployKey
.are_public
.id_not_in(enabled_keys.select(:id))
.id_not_in(available_project_keys.select(:id))
.with_projects
end
end
end
......@@ -78,10 +110,17 @@ module Projects
# rubocop: disable CodeReuse/ActiveRecord
def user_readable_project_ids
project_ids = (available_keys + available_project_keys + available_public_keys)
.flat_map { |deploy_key| deploy_key.deploy_keys_projects.map(&:project_id) }
.compact
.uniq
project_ids = if Feature.enabled?(:optimize_deploy_keys_presenter, project, default_enabled: :yaml)
(available_project_keys + available_public_keys)
.flat_map { |deploy_key| deploy_key.deploy_keys_projects.map(&:project_id) }
.compact
.uniq
else
(available_keys + available_project_keys + available_public_keys)
.flat_map { |deploy_key| deploy_key.deploy_keys_projects.map(&:project_id) }
.compact
.uniq
end
current_user.authorized_projects(Gitlab::Access::GUEST).id_in(project_ids).pluck(:id)
end
......
---
name: optimize_deploy_keys_presenter
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56305/diffs
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/324143
milestone: '13.10'
type: development
group: group::release
default_enabled: false
......@@ -3,17 +3,70 @@
require 'spec_helper'
RSpec.describe Projects::Settings::DeployKeysPresenter do
let(:project) { create(:project) }
let(:user) { create(:user) }
let_it_be(:project, refind: true) { create(:project) }
let_it_be(:other_project) { create(:project) }
let_it_be(:user) { create(:user) }
subject(:presenter) do
described_class.new(project, current_user: user)
end
before_all do
project.add_maintainer(user)
other_project.add_maintainer(user)
end
it 'inherits from Gitlab::View::Presenter::Simple' do
expect(described_class.superclass).to eq(Gitlab::View::Presenter::Simple)
end
describe 'deploy key groups' do
let_it_be(:deploy_key) { create(:deploy_key, projects: [project]) }
let_it_be(:other_deploy_key) { create(:deploy_key, projects: [other_project]) }
let_it_be(:public_deploy_key) { create(:deploy_key, public: true) }
let_it_be(:unrelated_project) { create(:project, :private) }
let_it_be(:unrelated_deploy_key) { create(:deploy_key, projects: [unrelated_project]) }
shared_examples_for 'correct behavior' do
context 'with enabled keys' do
it 'returns correct deploy keys' do
expect(presenter.enabled_keys).to eq([deploy_key])
expect(presenter.enabled_keys_size).to eq(1)
end
end
context 'with available keys' do
it 'returns correct deploy keys' do
expect(presenter.available_keys).to eq([other_deploy_key, public_deploy_key])
end
end
context 'with available project keys' do
it 'returns correct deploy keys' do
expect(presenter.available_project_keys).to eq([other_deploy_key])
expect(presenter.available_project_keys_size).to eq(1)
end
end
context 'with available public keys' do
it 'returns correct deploy keys' do
expect(presenter.available_public_keys).to eq([public_deploy_key])
expect(presenter.available_public_keys_size).to eq(1)
end
end
end
it_behaves_like 'correct behavior'
context 'when optimize_deploy_keys_presenter feature flag is disabled' do
before do
stub_feature_flags(optimize_deploy_keys_presenter: false)
end
it_behaves_like 'correct behavior'
end
end
describe '#enabled_keys' do
let!(:deploy_key) { create(:deploy_key, public: true) }
......
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