Search feature: redirects to commit page if query is commit sha and only commit found

See !8028 and #24833
This commit is contained in:
YarNayar 2017-01-11 07:20:32 +03:00
parent dd3ddcd72b
commit 99404a5851
5 changed files with 57 additions and 1 deletions

View file

@ -45,6 +45,8 @@ class SearchController < ApplicationController
end
@search_objects = @search_results.objects(@scope, params[:page])
check_single_commit_result
end
def autocomplete
@ -59,4 +61,16 @@ class SearchController < ApplicationController
render json: search_autocomplete_opts(term).to_json
end
private
def check_single_commit_result
if @search_results.single_commit_result?
only_commit = @search_results.objects('commits').first
query = params[:search].strip.downcase
found_by_commit_sha = Commit.valid_hash?(query) && only_commit.sha.start_with?(query)
redirect_to namespace_project_commit_path(@project.namespace, @project, only_commit) if found_by_commit_sha
end
end
end

View file

@ -0,0 +1,5 @@
---
title: 'Search feature: redirects to commit page if query is commit sha and only commit
found'
merge_request: 8028
author: YarNayar

View file

@ -71,6 +71,14 @@ module Gitlab
)
end
def single_commit_result?
commits_count == 1 && total_result_count == 1
end
def total_result_count
issues_count + merge_requests_count + milestones_count + notes_count + blobs_count + wiki_blobs_count + commits_count
end
private
def blobs
@ -122,7 +130,7 @@ module Gitlab
commits = find_commits_by_message(query)
commit_by_sha = find_commit_by_sha(query)
commits << commit_by_sha if commit_by_sha && !commits.include?(commit_by_sha)
commits |= [commit_by_sha] if commit_by_sha
commits
end

View file

@ -43,6 +43,10 @@ module Gitlab
@milestones_count ||= milestones.count
end
def single_commit_result?
false
end
private
def projects

View file

@ -217,6 +217,31 @@ describe "Search", feature: true do
visit search_path(project_id: project.id)
end
it 'redirects to commit page when search by sha and only commit found' do
fill_in 'search', with: '6d394385cf567f80a8fd85055db1ab4c5295806f'
click_button 'Search'
expect(page).to have_current_path(namespace_project_commit_path(project.namespace, project, '6d394385cf567f80a8fd85055db1ab4c5295806f'))
end
it 'redirects to single commit regardless of query case' do
fill_in 'search', with: '6D394385cf'
click_button 'Search'
expect(page).to have_current_path(namespace_project_commit_path(project.namespace, project, '6d394385cf567f80a8fd85055db1ab4c5295806f'))
end
it 'holds on /search page when the only commit is found by message' do
create_commit('Message referencing another sha: "deadbeef" ', project, user, 'master')
fill_in 'search', with: 'deadbeef'
click_button 'Search'
expect(page).to have_current_path('/search', only_path: true)
end
it 'shows multiple matching commits' do
fill_in 'search', with: 'See merge request'