Commit 7e6fd918 authored by Stan Hu's avatar Stan Hu

Merge branch '254932-fix-search-results-blob-link-to-branch' into 'master'

Ensure code search results link to searched ref

See merge request gitlab-org/gitlab!43510
parents da208269 0b49e64b
......@@ -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