Commit fca9fa2e authored by Terri Chu's avatar Terri Chu Committed by Thong Kuah

Add GFM issue support to search autocomplete

Enable GitHub Flavored Markdown support for issues to the
search autocomplete field. If the user is in the project
dashboard and enters GFM for an issue id, a link to that
issue will show up.
parent a1ae6803
......@@ -9,7 +9,8 @@ module SearchHelper
resources_results = [
recent_items_autocomplete(term),
groups_autocomplete(term),
projects_autocomplete(term)
projects_autocomplete(term),
issue_autocomplete(term)
].flatten
search_pattern = Regexp.new(Regexp.escape(term), "i")
......@@ -172,6 +173,24 @@ module SearchHelper
end
# rubocop: enable CodeReuse/ActiveRecord
def issue_autocomplete(term)
return [] unless @project.present? && current_user && term =~ /\A#{Issue.reference_prefix}\d+\z/
iid = term.sub(Issue.reference_prefix, '').to_i
issue = @project.issues.find_by_iid(iid)
return [] unless issue && Ability.allowed?(current_user, :read_issue, issue)
[
{
category: 'In this project',
id: issue.id,
label: search_result_sanitize("#{issue.title} (#{issue.to_reference})"),
url: issue_path(issue),
avatar_url: issue.project.avatar_url || ''
}
]
end
# Autocomplete results for the current user's projects
# rubocop: disable CodeReuse/ActiveRecord
def projects_autocomplete(term, limit = 5)
......
---
title: Search Autocomplete add GFM support for issues
merge_request: 44930
author:
type: changed
......@@ -211,6 +211,7 @@ You can also type in this search bar to see autocomplete suggestions for:
- 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 viewed merge request)
- Recently viewed epics (try and type some word from the title of a recently viewed epic)
- [GitLab Flavored Markdown](../markdown.md#special-gitlab-references) (GFM) for issues within a project (try and type a GFM reference for an issue)
## Basic search
......
......@@ -73,7 +73,7 @@ RSpec.describe SearchHelper do
expect(result.keys).to match_array(%i[category id label url avatar_url])
end
it 'includes the users recently viewed issues' do
it 'includes the users recently viewed issues', :aggregate_failures do
recent_issues = instance_double(::Gitlab::Search::RecentIssues)
expect(::Gitlab::Search::RecentIssues).to receive(:new).with(user: user).and_return(recent_issues)
project1 = create(:project, :with_avatar, namespace: user.namespace)
......@@ -104,7 +104,7 @@ RSpec.describe SearchHelper do
})
end
it 'includes the users recently viewed merge requests' do
it 'includes the users recently viewed merge requests', :aggregate_failures 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)
......@@ -145,10 +145,40 @@ RSpec.describe SearchHelper do
@project = create(:project, :repository)
end
it "includes project-specific sections" do
it "includes project-specific sections", :aggregate_failures do
expect(search_autocomplete_opts("Files").size).to eq(1)
expect(search_autocomplete_opts("Commits").size).to eq(1)
end
context 'when user does not have access to project' do
it 'does not include issues by iid' do
issue = create(:issue, project: @project)
results = search_autocomplete_opts("\##{issue.iid}")
expect(results.count).to eq(0)
end
end
context 'when user has project access' do
before do
@project = create(:project, :repository, namespace: user.namespace)
end
it 'includes issues by iid', :aggregate_failures do
issue = create(:issue, project: @project, title: 'test title')
results = search_autocomplete_opts("\##{issue.iid}")
expect(results.count).to eq(1)
expect(results.first).to include({
category: 'In this project',
id: issue.id,
label: 'test title (#1)',
url: ::Gitlab::Routing.url_helpers.project_issue_path(issue.project, issue),
avatar_url: '' # project has no avatar
})
end
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