Commit 0b49e64b authored by Dylan Griffith's avatar Dylan Griffith

Ensure code search results link to searched ref

Code searches allow you to choose branch name or tag when searching for
code. Previously the blob search results were linking to the URL with
the SHA of the branch and not the actual branch name being searched.
This works OK in some cases but usually the user expecting to be linked
to `/master/` instead of `/<some-sha>/` and in particular it is
problematic because when you are loading a file in a specific SHA you
cannot click the edit button to edit that file you just loaded.

We fix this by using the param `repository_ref` when present and
defaulting to the `default_branch` when absent.

See more at https://gitlab.com/gitlab-org/gitlab/-/issues/254932
parent 1036b045
......@@ -86,6 +86,11 @@ module SearchHelper
}).html_safe
end
def repository_ref(project)
# Always #to_s the repository_ref param in case the value is also a number
params[:repository_ref].to_s.presence || project.default_branch
end
# Overridden in EE
def search_blob_title(project, path)
path
......
......@@ -10,10 +10,9 @@
- if @project
- link_to_project = link_to(@project.full_name, @project, class: 'ml-md-1')
- if @scope == 'blobs'
- repository_ref = params[:repository_ref].to_s.presence || @project.default_branch
= s_("SearchCodeResults|in")
.mx-md-1
= render partial: "shared/ref_switcher", locals: { ref: repository_ref, form_path: request.fullpath, field_name: 'repository_ref' }
= render partial: "shared/ref_switcher", locals: { ref: repository_ref(@project), form_path: request.fullpath, field_name: 'repository_ref' }
= s_('SearchCodeResults|of %{link_to_project}').html_safe % { link_to_project: link_to_project }
- else
= _("in project %{link_to_project}").html_safe % { link_to_project: link_to_project }
......
- project = blob.project
- return unless project
- blob_link = project_blob_path(project, tree_join(blob.ref, blob.path))
- blob_link = project_blob_path(project, tree_join(repository_ref(project), blob.path))
= render partial: 'search/results/blob_data', locals: { blob: blob, project: project, path: blob.path, blob_link: blob_link }
---
title: Ensure code search results link to searched ref
merge_request: 43510
author:
type: fixed
......@@ -21,6 +21,7 @@ RSpec.describe 'User searches for code' do
expect(page).to have_selector('.results', text: 'application.js')
expect(page).to have_selector('.file-content .code')
expect(page).to have_selector("span.line[lang='javascript']")
expect(page).to have_link('application.js', href: /master\/files\/js\/application.js/)
end
context 'when on a project page', :js do
......
......@@ -399,4 +399,25 @@ RSpec.describe SearchHelper do
end
end
end
describe '#repository_ref' do
let_it_be(:project) { create(:project, :repository) }
let(:params) { { repository_ref: 'the-repository-ref-param' } }
subject { repository_ref(project) }
it { is_expected.to eq('the-repository-ref-param') }
context 'when the param :repository_ref is not set' do
let(:params) { { repository_ref: nil } }
it { is_expected.to eq(project.default_branch) }
end
context 'when the repository_ref param is a number' do
let(:params) { { repository_ref: 111111 } }
it { is_expected.to eq('111111') }
end
end
end
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