Commit acd09bbd authored by Dylan Griffith's avatar Dylan Griffith

Add autocomplete search suggestions for recent merge requests

This implements the same behaviour for merge requests that was added
for issues in
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40669.
parent 22b0aa90
...@@ -50,6 +50,8 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -50,6 +50,8 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
around_action :allow_gitaly_ref_name_caching, only: [:index, :show, :discussions] around_action :allow_gitaly_ref_name_caching, only: [:index, :show, :discussions]
after_action :log_merge_request_show, only: [:show]
feature_category :source_code_management, feature_category :source_code_management,
unless: -> (action) { action.ends_with?("_reports") } unless: -> (action) { action.ends_with?("_reports") }
feature_category :code_testing, feature_category :code_testing,
...@@ -450,6 +452,12 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -450,6 +452,12 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
end end
end end
def log_merge_request_show
return unless current_user && @merge_request
::Gitlab::Search::RecentMergeRequests.new(user: current_user).log_view(@merge_request)
end
def authorize_read_actual_head_pipeline! def authorize_read_actual_head_pipeline!
return render_404 unless can?(current_user, :read_build, merge_request.actual_head_pipeline) return render_404 unless can?(current_user, :read_build, merge_request.actual_head_pipeline)
end end
......
...@@ -7,6 +7,7 @@ module SearchHelper ...@@ -7,6 +7,7 @@ module SearchHelper
return unless current_user return unless current_user
resources_results = [ resources_results = [
recent_merge_requests_autocomplete(term),
recent_issues_autocomplete(term), recent_issues_autocomplete(term),
groups_autocomplete(term), groups_autocomplete(term),
projects_autocomplete(term) projects_autocomplete(term)
...@@ -180,6 +181,20 @@ module SearchHelper ...@@ -180,6 +181,20 @@ module SearchHelper
end end
end end
def recent_merge_requests_autocomplete(term, limit = 5)
return [] unless current_user
::Gitlab::Search::RecentMergeRequests.new(user: current_user).search(term).limit(limit).map do |mr|
{
category: "Recent merge requests",
id: mr.id,
label: search_result_sanitize(mr.title),
url: merge_request_path(mr),
avatar_url: mr.project.avatar_url || ''
}
end
end
def recent_issues_autocomplete(term, limit = 5) def recent_issues_autocomplete(term, limit = 5)
return [] unless current_user return [] unless current_user
......
...@@ -21,6 +21,7 @@ class MergeRequest < ApplicationRecord ...@@ -21,6 +21,7 @@ class MergeRequest < ApplicationRecord
include MilestoneEventable include MilestoneEventable
include StateEventable include StateEventable
include ApprovableBase include ApprovableBase
include IdInOrdered
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
......
---
title: Add autocomplete search suggestions for recent merge requests
merge_request: 42560
author:
type: added
...@@ -171,6 +171,7 @@ You can also type in this search bar to see autocomplete suggestions for: ...@@ -171,6 +171,7 @@ You can also type in this search bar to see autocomplete suggestions for:
- Project feature pages (try and type **milestones**) - Project feature pages (try and type **milestones**)
- Various settings pages (try and type **user settings**) - Various settings pages (try and type **user settings**)
- Recently viewed issues (try and type some word from the title of a recently viewed issue) - Recently viewed issues (try and type some word from the title of a recently viewed issue)
- Recently viewed merge requests (try and type some word from the title of a recently merge request)
## To-Do List ## To-Do List
......
# frozen_string_literal: true
module Gitlab
module Search
class RecentMergeRequests < RecentItems
private
def type
MergeRequest
end
def finder
MergeRequestsFinder
end
end
end
end
...@@ -1029,7 +1029,8 @@ RSpec.describe Projects::IssuesController do ...@@ -1029,7 +1029,8 @@ RSpec.describe Projects::IssuesController do
go(id: issue.to_param) go(id: issue.to_param)
expect(recent_issues_double).to have_received(:log_view) expect(response).to be_successful
expect(recent_issues_double).to have_received(:log_view).with(issue)
end end
context 'when not logged in' do context 'when not logged in' do
......
...@@ -123,6 +123,16 @@ RSpec.describe Projects::MergeRequestsController do ...@@ -123,6 +123,16 @@ RSpec.describe Projects::MergeRequestsController do
expect(response).to be_successful expect(response).to be_successful
end end
it 'logs the view with Gitlab::Search::RecentMergeRequests' do
recent_merge_requests_double = instance_double(::Gitlab::Search::RecentMergeRequests, log_view: nil)
expect(::Gitlab::Search::RecentMergeRequests).to receive(:new).with(user: user).and_return(recent_merge_requests_double)
go(format: :html)
expect(response).to be_successful
expect(recent_merge_requests_double).to have_received(:log_view).with(merge_request)
end
context "that is invalid" do context "that is invalid" do
let(:merge_request) { create(:invalid_merge_request, target_project: project, source_project: project) } let(:merge_request) { create(:invalid_merge_request, target_project: project, source_project: project) }
......
...@@ -106,6 +106,39 @@ RSpec.describe SearchHelper do ...@@ -106,6 +106,39 @@ RSpec.describe SearchHelper do
}) })
end end
it 'includes the first 5 of the users recent merge requests' do
recent_merge_requests = instance_double(::Gitlab::Search::RecentMergeRequests)
expect(::Gitlab::Search::RecentMergeRequests).to receive(:new).with(user: user).and_return(recent_merge_requests)
project1 = create(:project, :with_avatar, namespace: user.namespace)
project2 = create(:project, namespace: user.namespace)
merge_request1 = create(:merge_request, :unique_branches, title: 'Merge request 1', target_project: project1, source_project: project1)
merge_request2 = create(:merge_request, :unique_branches, title: 'Merge request 2', target_project: project2, source_project: project2)
other_merge_requests = create_list(:merge_request, 5)
expect(recent_merge_requests).to receive(:search).with('the search term').and_return(MergeRequest.id_in_ordered([merge_request1.id, merge_request2.id, *other_merge_requests.map(&:id)]))
results = search_autocomplete_opts("the search term")
expect(results.count).to eq(5)
expect(results[0]).to include({
category: 'Recent merge requests',
id: merge_request1.id,
label: 'Merge request 1',
url: Gitlab::Routing.url_helpers.project_merge_request_path(merge_request1.project, merge_request1),
avatar_url: project1.avatar_url
})
expect(results[1]).to include({
category: 'Recent merge requests',
id: merge_request2.id,
label: 'Merge request 2',
url: Gitlab::Routing.url_helpers.project_merge_request_path(merge_request2.project, merge_request2),
avatar_url: '' # This project didn't have an avatar so set this to ''
})
end
it "does not include the public group" do it "does not include the public group" do
group = create(:group) group = create(:group)
expect(search_autocomplete_opts(group.name).size).to eq(0) expect(search_autocomplete_opts(group.name).size).to eq(0)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Gitlab::Search::RecentMergeRequests do
def create_item(content:, project:)
create(:merge_request, :unique_branches, title: content, target_project: project, source_project: project)
end
it_behaves_like 'search recent items'
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