Commit 4cb3ca67 authored by Stan Hu's avatar Stan Hu

Only load project license if needed

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/24310 introduced a
unintended side effect: instead of loading the project license name only
when the explore tab were present, the license would always be loaded
even if it were not displayed.

To fix that, we now load the license only if it is needed and memoize
the value via `SafeRequestStore` in the helper.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/223740
parent 90051967
......@@ -384,9 +384,12 @@ module ProjectsHelper
end
def project_license_name(project)
project.repository.license&.name
key = "project:#{project.id}:license_name"
Gitlab::SafeRequestStore.fetch(key) { project.repository.license&.name }
rescue GRPC::Unavailable, GRPC::DeadlineExceeded, Gitlab::Git::CommandError => e
Gitlab::ErrorTracking.track_exception(e)
Gitlab::SafeRequestStore[key] = nil
nil
end
......
......@@ -16,7 +16,6 @@
- css_controls_class = compact_mode ? [] : ["flex-lg-row", "justify-content-lg-between"]
- css_controls_class << "with-pipeline-status" if show_pipeline_status_icon
- avatar_container_class = project.creator && use_creator_avatar ? '' : 'rect-avatar'
- license_name = project_license_name(project)
%li.project-row.d-flex{ class: css_class }
= cache(cache_key) do
......@@ -43,10 +42,10 @@
%span.metadata-info.visibility-icon.append-right-10.gl-mt-3.text-secondary.has-tooltip{ data: { container: 'body', placement: 'top' }, title: visibility_icon_description(project) }
= visibility_level_icon(project.visibility_level, fw: true)
- if explore_projects_tab? && license_name
- if explore_projects_tab? && project_license_name(project)
%span.metadata-info.d-inline-flex.align-items-center.append-right-10.gl-mt-3
= sprite_icon('scale', size: 14, css_class: 'append-right-4')
= license_name
= project_license_name(project)
- if !explore_projects_tab? && access&.nonzero?
-# haml-lint:disable UnnecessaryStringOutput
......
---
title: Only load project license if needed
merge_request: 35068
author:
type: performance
......@@ -977,19 +977,32 @@ describe ProjectsHelper do
end
end
describe '#project_license_name(project)' do
describe '#project_license_name(project)', :request_store do
let_it_be(:project) { create(:project) }
let_it_be(:repository) { project.repository }
subject { project_license_name(project) }
def license_name
project_license_name(project)
end
context 'gitaly is working appropriately' do
it 'returns the license name' do
license = Licensee::License.new('mit')
allow(repository).to receive(:license).and_return(license)
let(:license) { Licensee::License.new('mit') }
before do
expect(repository).to receive(:license).and_return(license)
end
it 'returns the license name' do
expect(subject).to eq(license.name)
end
it 'memoizes the value' do
expect do
2.times { expect(license_name).to eq(license.name) }
end.to change { Gitlab::GitalyClient.get_request_count }.by_at_most(1)
end
end
context 'gitaly is unreachable' do
......@@ -1003,10 +1016,16 @@ describe ProjectsHelper do
subject
end
it 'memoizes the nil value' do
expect do
2.times { expect(license_name).to be_nil }
end.to change { Gitlab::GitalyClient.get_request_count }.by_at_most(1)
end
end
before do
allow(repository).to receive(:license).and_raise(exception)
expect(repository).to receive(:license).and_raise(exception)
end
context "Gitlab::Git::CommandError" do
......
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