Commit 2695d26e authored by Sean McGivern's avatar Sean McGivern

Merge branch 'id-use-cached-readme-path' into 'master'

Use cached readme path for projects#show

See merge request gitlab-org/gitlab!49419
parents 33f825f8 37903edd
...@@ -20,8 +20,6 @@ class ProjectsController < Projects::ApplicationController ...@@ -20,8 +20,6 @@ class ProjectsController < Projects::ApplicationController
before_action :project, except: [:index, :new, :create, :resolve] before_action :project, except: [:index, :new, :create, :resolve]
before_action :repository, except: [:index, :new, :create, :resolve] before_action :repository, except: [:index, :new, :create, :resolve]
before_action :assign_ref_vars, if: -> { action_name == 'show' && repo_exists? } before_action :assign_ref_vars, if: -> { action_name == 'show' && repo_exists? }
before_action :tree,
if: -> { action_name == 'show' && repo_exists? && project_view_files? }
before_action :project_export_enabled, only: [:export, :download_export, :remove_export, :generate_new_export] before_action :project_export_enabled, only: [:export, :download_export, :remove_export, :generate_new_export]
before_action :present_project, only: [:edit] before_action :present_project, only: [:edit]
before_action :authorize_download_code!, only: [:refs] before_action :authorize_download_code!, only: [:refs]
......
...@@ -618,7 +618,7 @@ class Repository ...@@ -618,7 +618,7 @@ class Repository
end end
def readme_path def readme_path
readme&.path head_tree&.readme_path
end end
cache_method :readme_path cache_method :readme_path
......
...@@ -77,19 +77,19 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -77,19 +77,19 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end end
def readme_path def readme_path
filename_path(:readme) filename_path(repository.readme_path)
end end
def changelog_path def changelog_path
filename_path(:changelog) filename_path(repository.changelog&.name)
end end
def license_path def license_path
filename_path(:license_blob) filename_path(repository.license_blob&.name)
end end
def ci_configuration_path def ci_configuration_path
filename_path(:gitlab_ci_yml) filename_path(repository.gitlab_ci_yml&.name)
end end
def contribution_guide_path def contribution_guide_path
...@@ -244,11 +244,11 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -244,11 +244,11 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end end
def readme_anchor_data def readme_anchor_data
if current_user && can_current_user_push_to_default_branch? && repository.readme.nil? if current_user && can_current_user_push_to_default_branch? && readme_path.nil?
AnchorData.new(false, AnchorData.new(false,
statistic_icon + _('Add README'), statistic_icon + _('Add README'),
empty_repo? ? add_readme_ide_path : add_readme_path) empty_repo? ? add_readme_ide_path : add_readme_path)
elsif repository.readme elsif readme_path
AnchorData.new(false, AnchorData.new(false,
statistic_icon('doc-text') + _('README'), statistic_icon('doc-text') + _('README'),
default_view != 'readme' ? readme_path : '#readme', default_view != 'readme' ? readme_path : '#readme',
...@@ -397,13 +397,10 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -397,13 +397,10 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
current_user && can?(current_user, :create_cluster, project) current_user && can?(current_user, :create_cluster, project)
end end
def filename_path(filename) def filename_path(filepath)
if blob = repository.public_send(filename) # rubocop:disable GitlabSecurity/PublicSend return if filepath.blank?
project_blob_path(
project, project_blob_path(project, tree_join(default_branch, filepath))
tree_join(default_branch, blob.name)
)
end
end end
def anonymous_project_view def anonymous_project_view
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
- project = local_assigns.fetch(:project) { @project } - project = local_assigns.fetch(:project) { @project }
- show_auto_devops_callout = show_auto_devops_callout?(@project) - show_auto_devops_callout = show_auto_devops_callout?(@project)
- add_page_startup_api_call logs_file_project_ref_path(@project, ref, @path, format: "json", offset: 0) - add_page_startup_api_call logs_file_project_ref_path(@project, ref, @path, format: "json", offset: 0)
- if @tree.readme - if readme_path = @project.repository.readme_path
- add_page_startup_api_call project_blob_path(@project, tree_join(@ref, @tree.readme.path), viewer: "rich", format: "json") - add_page_startup_api_call project_blob_path(@project, tree_join(@ref, readme_path), viewer: "rich", format: "json")
#tree-holder.tree-holder.clearfix #tree-holder.tree-holder.clearfix
.nav-block .nav-block
......
...@@ -165,7 +165,7 @@ RSpec.describe 'Projects > Show > User sees setup shortcut buttons' do ...@@ -165,7 +165,7 @@ RSpec.describe 'Projects > Show > User sees setup shortcut buttons' do
context 'when the project does not have a README' do context 'when the project does not have a README' do
it 'shows the single file editor "Add README" button' do it 'shows the single file editor "Add README" button' do
allow(project.repository).to receive(:readme).and_return(nil) allow(project.repository).to receive(:readme_path).and_return(nil)
visit project_path(project) visit project_path(project)
......
...@@ -2335,7 +2335,7 @@ RSpec.describe Repository do ...@@ -2335,7 +2335,7 @@ RSpec.describe Repository do
end end
it 'caches the response' do it 'caches the response' do
expect(repository).to receive(:readme).and_call_original.once expect(repository.head_tree).to receive(:readme_path).and_call_original.once
2.times do 2.times do
expect(repository.readme_path).to eq("README.md") expect(repository.readme_path).to eq("README.md")
......
...@@ -375,7 +375,7 @@ RSpec.describe ProjectPresenter do ...@@ -375,7 +375,7 @@ RSpec.describe ProjectPresenter do
it 'returns anchor data' do it 'returns anchor data' do
project.add_developer(user) project.add_developer(user)
allow(project.repository).to receive(:readme).and_return(nil) allow(project.repository).to receive(:readme_path).and_return(nil)
expect(presenter.readme_anchor_data).to have_attributes( expect(presenter.readme_anchor_data).to have_attributes(
is_link: false, is_link: false,
...@@ -387,7 +387,7 @@ RSpec.describe ProjectPresenter do ...@@ -387,7 +387,7 @@ RSpec.describe ProjectPresenter do
context 'when README exists' do context 'when README exists' do
it 'returns anchor data' do it 'returns anchor data' do
allow(project.repository).to receive(:readme).and_return(double(name: 'readme')) allow(project.repository).to receive(:readme_path).and_return('readme')
expect(presenter.readme_anchor_data).to have_attributes( expect(presenter.readme_anchor_data).to have_attributes(
is_link: false, is_link: false,
...@@ -561,7 +561,7 @@ RSpec.describe ProjectPresenter do ...@@ -561,7 +561,7 @@ RSpec.describe ProjectPresenter do
let(:project) { build_stubbed(:project) } let(:project) { build_stubbed(:project) }
it 'orders the items correctly' do it 'orders the items correctly' do
allow(project.repository).to receive(:readme).and_return(double(name: 'readme')) allow(project.repository).to receive(:readme_path).and_return('readme')
allow(project.repository).to receive(:license_blob).and_return(nil) allow(project.repository).to receive(:license_blob).and_return(nil)
allow(project.repository).to receive(:changelog).and_return(nil) allow(project.repository).to receive(:changelog).and_return(nil)
allow(project.repository).to receive(:contribution_guide).and_return(double(name: 'foo')) allow(project.repository).to receive(:contribution_guide).and_return(double(name: 'foo'))
......
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