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 ...@@ -14,12 +14,20 @@ module Projects
@key ||= DeployKey.new.tap { |dk| dk.deploy_keys_projects.build } @key ||= DeployKey.new.tap { |dk| dk.deploy_keys_projects.build }
end end
# It includes:
# - The deploy keys enabled in the project.
def enabled_keys def enabled_keys
strong_memoize(:enabled_keys) do strong_memoize(:enabled_keys) do
project.deploy_keys.with_projects project.deploy_keys.with_projects
end end
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 def available_keys
strong_memoize(:available_keys) do strong_memoize(:available_keys) do
current_user current_user
...@@ -29,17 +37,40 @@ module Projects ...@@ -29,17 +37,40 @@ module Projects
end end
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 def available_project_keys
if Feature.enabled?(:optimize_deploy_keys_presenter, project, default_enabled: :yaml)
strong_memoize(:available_project_keys) do 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 current_user
.project_deploy_keys .project_deploy_keys
.id_not_in(enabled_keys.select(:id)) .id_not_in(enabled_keys.select(:id))
.with_projects .with_projects
end end
end end
end
# It includes:
# - Instance-level public deploy keys.
# It excludes:
# - The deploy keys enabled in the project.
def available_public_keys def available_public_keys
if Feature.enabled?(:optimize_deploy_keys_presenter, project, default_enabled: :yaml)
strong_memoize(:available_public_keys) do 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 DeployKey
.are_public .are_public
.id_not_in(enabled_keys.select(:id)) .id_not_in(enabled_keys.select(:id))
...@@ -47,6 +78,7 @@ module Projects ...@@ -47,6 +78,7 @@ module Projects
.with_projects .with_projects
end end
end end
end
def as_json def as_json
serializer = DeployKeySerializer.new # rubocop: disable CodeReuse/Serializer serializer = DeployKeySerializer.new # rubocop: disable CodeReuse/Serializer
...@@ -78,10 +110,17 @@ module Projects ...@@ -78,10 +110,17 @@ module Projects
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def user_readable_project_ids def user_readable_project_ids
project_ids = (available_keys + available_project_keys + available_public_keys) 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) } .flat_map { |deploy_key| deploy_key.deploy_keys_projects.map(&:project_id) }
.compact .compact
.uniq .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) current_user.authorized_projects(Gitlab::Access::GUEST).id_in(project_ids).pluck(:id)
end 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 @@ ...@@ -3,17 +3,70 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Projects::Settings::DeployKeysPresenter do RSpec.describe Projects::Settings::DeployKeysPresenter do
let(:project) { create(:project) } let_it_be(:project, refind: true) { create(:project) }
let(:user) { create(:user) } let_it_be(:other_project) { create(:project) }
let_it_be(:user) { create(:user) }
subject(:presenter) do subject(:presenter) do
described_class.new(project, current_user: user) described_class.new(project, current_user: user)
end end
before_all do
project.add_maintainer(user)
other_project.add_maintainer(user)
end
it 'inherits from Gitlab::View::Presenter::Simple' do it 'inherits from Gitlab::View::Presenter::Simple' do
expect(described_class.superclass).to eq(Gitlab::View::Presenter::Simple) expect(described_class.superclass).to eq(Gitlab::View::Presenter::Simple)
end 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 describe '#enabled_keys' do
let!(:deploy_key) { create(:deploy_key, public: true) } 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