diff options
-rw-r--r-- | app/controllers/search_controller.rb | 14 | ||||
-rw-r--r-- | changelogs/unreleased/redirect-to-commit-when-only-commit-found.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/project_search_results.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/search_results.rb | 4 | ||||
-rw-r--r-- | spec/features/search_spec.rb | 25 |
5 files changed, 57 insertions, 1 deletions
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index b666aa01d6b..6576ebd5235 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -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 diff --git a/changelogs/unreleased/redirect-to-commit-when-only-commit-found.yml b/changelogs/unreleased/redirect-to-commit-when-only-commit-found.yml new file mode 100644 index 00000000000..e0f7e11b6d1 --- /dev/null +++ b/changelogs/unreleased/redirect-to-commit-when-only-commit-found.yml @@ -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 diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb index a66719706be..db325c00705 100644 --- a/lib/gitlab/project_search_results.rb +++ b/lib/gitlab/project_search_results.rb @@ -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 diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb index 35212992698..c9c65f76f4b 100644 --- a/lib/gitlab/search_results.rb +++ b/lib/gitlab/search_results.rb @@ -43,6 +43,10 @@ module Gitlab @milestones_count ||= milestones.count end + def single_commit_result? + false + end + private def projects diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rb index c64aeea0612..0fe5a897565 100644 --- a/spec/features/search_spec.rb +++ b/spec/features/search_spec.rb @@ -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' |