2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
module SearchHelper
|
2019-10-31 11:06:41 -04:00
|
|
|
SEARCH_PERMITTED_PARAMS = [:search, :scope, :project_id, :group_id, :repository_ref, :snippets].freeze
|
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
def search_autocomplete_opts(term)
|
2013-10-20 01:05:20 -04:00
|
|
|
return unless current_user
|
2014-01-18 07:16:46 -05:00
|
|
|
|
|
|
|
resources_results = [
|
|
|
|
groups_autocomplete(term),
|
2014-02-13 15:45:51 -05:00
|
|
|
projects_autocomplete(term)
|
2014-01-18 07:16:46 -05:00
|
|
|
].flatten
|
|
|
|
|
2016-09-07 09:16:22 -04:00
|
|
|
search_pattern = Regexp.new(Regexp.escape(term), "i")
|
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
|
2017-09-03 01:49:19 -04:00
|
|
|
generic_results.concat(default_autocomplete_admin) if current_user.admin?
|
2016-09-07 09:16:22 -04:00
|
|
|
generic_results.select! { |result| result[:label] =~ search_pattern }
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
[
|
2014-01-18 07:16:46 -05:00
|
|
|
resources_results,
|
|
|
|
generic_results
|
2013-12-09 09:26:43 -05:00
|
|
|
].flatten.uniq do |item|
|
|
|
|
item[:label]
|
2014-01-18 07:16:46 -05:00
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
|
2016-04-15 12:00:53 -04:00
|
|
|
def search_entries_info(collection, scope, term)
|
2018-12-21 06:44:37 -05:00
|
|
|
return if collection.to_a.empty?
|
2016-04-15 09:13:21 -04:00
|
|
|
|
2016-04-26 04:02:28 -04:00
|
|
|
from = collection.offset_value + 1
|
2018-12-21 06:44:37 -05:00
|
|
|
to = collection.offset_value + collection.to_a.size
|
2016-04-26 04:02:28 -04:00
|
|
|
count = collection.total_count
|
2019-12-05 13:07:51 -05:00
|
|
|
term_element = "<span> <code>#{h(term)}</code> </span>".html_safe
|
2016-04-26 04:02:28 -04:00
|
|
|
|
2019-08-30 05:49:14 -04:00
|
|
|
search_entries_info_template(collection) % {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
count: count,
|
2019-09-19 05:06:27 -04:00
|
|
|
scope: search_entries_scope_label(scope, count),
|
2019-12-05 13:07:51 -05:00
|
|
|
term_element: term_element
|
2019-08-30 05:49:14 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-19 05:06:27 -04:00
|
|
|
def search_entries_scope_label(scope, count)
|
2019-08-30 05:49:14 -04:00
|
|
|
case scope
|
2019-09-19 05:06:27 -04:00
|
|
|
when 'blobs'
|
|
|
|
ns_('SearchResults|code result', 'SearchResults|code results', count)
|
2019-08-30 05:49:14 -04:00
|
|
|
when 'commits'
|
|
|
|
ns_('SearchResults|commit', 'SearchResults|commits', count)
|
|
|
|
when 'issues'
|
|
|
|
ns_('SearchResults|issue', 'SearchResults|issues', count)
|
|
|
|
when 'merge_requests'
|
|
|
|
ns_('SearchResults|merge request', 'SearchResults|merge requests', count)
|
|
|
|
when 'milestones'
|
|
|
|
ns_('SearchResults|milestone', 'SearchResults|milestones', count)
|
|
|
|
when 'notes'
|
|
|
|
ns_('SearchResults|comment', 'SearchResults|comments', count)
|
|
|
|
when 'projects'
|
|
|
|
ns_('SearchResults|project', 'SearchResults|projects', count)
|
2019-09-19 05:06:27 -04:00
|
|
|
when 'snippet_blobs'
|
|
|
|
ns_('SearchResults|snippet result', 'SearchResults|snippet results', count)
|
2019-08-30 05:49:14 -04:00
|
|
|
when 'snippet_titles'
|
|
|
|
ns_('SearchResults|snippet', 'SearchResults|snippets', count)
|
|
|
|
when 'users'
|
|
|
|
ns_('SearchResults|user', 'SearchResults|users', count)
|
2019-09-19 05:06:27 -04:00
|
|
|
when 'wiki_blobs'
|
|
|
|
ns_('SearchResults|wiki result', 'SearchResults|wiki results', count)
|
2019-08-30 05:49:14 -04:00
|
|
|
else
|
|
|
|
raise "Unrecognized search scope '#{scope}'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_entries_info_template(collection)
|
|
|
|
if collection.total_pages > 1
|
2019-12-05 13:07:51 -05:00
|
|
|
s_("SearchResults|Showing %{from} - %{to} of %{count} %{scope} for%{term_element}").html_safe
|
2019-08-30 05:49:14 -04:00
|
|
|
else
|
2019-12-05 13:07:51 -05:00
|
|
|
s_("SearchResults|Showing %{count} %{scope} for%{term_element}").html_safe
|
2019-08-30 05:49:14 -04:00
|
|
|
end
|
2016-04-15 09:13:21 -04:00
|
|
|
end
|
|
|
|
|
2019-09-19 05:06:27 -04:00
|
|
|
def search_entries_empty_message(scope, term)
|
|
|
|
(s_("SearchResults|We couldn't find any %{scope} matching %{term}") % {
|
|
|
|
scope: search_entries_scope_label(scope, 0),
|
2019-10-09 08:06:13 -04:00
|
|
|
term: "<code>#{h(term)}</code>"
|
2019-09-19 05:06:27 -04:00
|
|
|
}).html_safe
|
|
|
|
end
|
|
|
|
|
2019-03-24 23:14:18 -04:00
|
|
|
def find_project_for_result_blob(projects, result)
|
2018-06-04 07:41:37 -04:00
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2019-03-24 23:14:18 -04:00
|
|
|
# Used in EE
|
|
|
|
def blob_projects(results)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2016-09-14 06:29:23 -04:00
|
|
|
def parse_search_result(result)
|
2018-06-04 07:41:37 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-10-23 20:07:18 -04:00
|
|
|
# Overriden in EE
|
|
|
|
def search_blob_title(project, path)
|
|
|
|
path
|
2016-09-14 06:29:23 -04:00
|
|
|
end
|
|
|
|
|
2019-03-06 14:33:06 -05:00
|
|
|
def search_service
|
|
|
|
@search_service ||= ::SearchService.new(current_user, params)
|
|
|
|
end
|
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Autocomplete results for various settings pages
|
|
|
|
def default_autocomplete
|
|
|
|
[
|
2019-04-04 13:05:25 -04:00
|
|
|
{ category: "Settings", label: _("User settings"), url: profile_path },
|
|
|
|
{ category: "Settings", label: _("SSH Keys"), url: profile_keys_path },
|
|
|
|
{ category: "Settings", label: _("Dashboard"), url: root_path }
|
2017-09-03 01:49:19 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for settings pages, for admins
|
|
|
|
def default_autocomplete_admin
|
|
|
|
[
|
2019-04-04 13:05:25 -04:00
|
|
|
{ category: "Settings", label: _("Admin Section"), url: admin_root_path }
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for internal help pages
|
|
|
|
def help_autocomplete
|
|
|
|
[
|
2019-04-04 13:05:25 -04:00
|
|
|
{ category: "Help", label: _("API Help"), url: help_page_path("api/README") },
|
|
|
|
{ category: "Help", label: _("Markdown Help"), url: help_page_path("user/markdown") },
|
|
|
|
{ category: "Help", label: _("Permissions Help"), url: help_page_path("user/permissions") },
|
|
|
|
{ category: "Help", label: _("Public Access Help"), url: help_page_path("public_access/public_access") },
|
|
|
|
{ category: "Help", label: _("Rake Tasks Help"), url: help_page_path("raketasks/README") },
|
|
|
|
{ category: "Help", label: _("SSH Keys Help"), url: help_page_path("ssh/README") },
|
|
|
|
{ category: "Help", label: _("System Hooks Help"), url: help_page_path("system_hooks/system_hooks") },
|
|
|
|
{ category: "Help", label: _("Webhooks Help"), url: help_page_path("user/project/integrations/webhooks") },
|
|
|
|
{ category: "Help", label: _("Workflow Help"), url: help_page_path("workflow/README") }
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current project, if it's defined
|
|
|
|
def project_autocomplete
|
|
|
|
if @project && @project.repository.exists? && @project.repository.root_ref
|
2016-05-10 22:58:06 -04:00
|
|
|
ref = @ref || @project.repository.root_ref
|
2013-10-20 01:05:20 -04:00
|
|
|
|
|
|
|
[
|
2019-04-04 13:05:25 -04:00
|
|
|
{ category: "In this project", label: _("Files"), url: project_tree_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: _("Commits"), url: project_commits_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: _("Network"), url: project_network_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: _("Graph"), url: project_graph_path(@project, ref) },
|
|
|
|
{ category: "In this project", label: _("Issues"), url: project_issues_path(@project) },
|
|
|
|
{ category: "In this project", label: _("Merge Requests"), url: project_merge_requests_path(@project) },
|
|
|
|
{ category: "In this project", label: _("Milestones"), url: project_milestones_path(@project) },
|
|
|
|
{ category: "In this project", label: _("Snippets"), url: project_snippets_path(@project) },
|
|
|
|
{ category: "In this project", label: _("Members"), url: project_project_members_path(@project) },
|
|
|
|
{ category: "In this project", label: _("Wiki"), url: project_wikis_path(@project) }
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's groups
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-01-18 07:16:46 -05:00
|
|
|
def groups_autocomplete(term, limit = 5)
|
2017-08-15 06:27:37 -04:00
|
|
|
current_user.authorized_groups.order_id_desc.search(term).limit(limit).map do |group|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
2016-03-24 16:19:43 -04:00
|
|
|
category: "Groups",
|
2016-03-08 02:56:43 -05:00
|
|
|
id: group.id,
|
2017-01-26 14:31:32 -05:00
|
|
|
label: "#{search_result_sanitize(group.full_name)}",
|
2018-07-17 01:48:39 -04:00
|
|
|
url: group_path(group),
|
|
|
|
avatar_url: group.avatar_url || ''
|
2014-01-18 07:16:46 -05:00
|
|
|
}
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2013-10-20 01:05:20 -04:00
|
|
|
|
|
|
|
# Autocomplete results for the current user's projects
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-01-18 07:16:46 -05:00
|
|
|
def projects_autocomplete(term, limit = 5)
|
2017-08-15 06:27:37 -04:00
|
|
|
current_user.authorized_projects.order_id_desc.search_by_title(term)
|
2019-04-19 04:36:16 -04:00
|
|
|
.sorted_by_stars_desc.non_archived.limit(limit).map do |p|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
2016-03-24 16:19:43 -04:00
|
|
|
category: "Projects",
|
2016-03-08 02:56:43 -05:00
|
|
|
id: p.id,
|
|
|
|
value: "#{search_result_sanitize(p.name)}",
|
2018-03-05 09:15:26 -05:00
|
|
|
label: "#{search_result_sanitize(p.full_name)}",
|
2018-07-17 01:48:39 -04:00
|
|
|
url: project_path(p),
|
|
|
|
avatar_url: p.avatar_url || ''
|
2014-01-18 07:16:46 -05:00
|
|
|
}
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2014-01-18 09:06:20 -05:00
|
|
|
def search_result_sanitize(str)
|
2014-01-18 07:16:46 -05:00
|
|
|
Sanitize.clean(str)
|
|
|
|
end
|
2014-08-27 02:57:50 -04:00
|
|
|
|
2019-07-15 13:59:57 -04:00
|
|
|
def search_filter_link(scope, label, data: {}, search: {})
|
|
|
|
search_params = params
|
|
|
|
.merge(search)
|
|
|
|
.merge({ scope: scope })
|
2019-10-31 11:06:41 -04:00
|
|
|
.permit(SEARCH_PERMITTED_PARAMS)
|
2019-07-15 13:59:57 -04:00
|
|
|
|
|
|
|
if @scope == scope
|
|
|
|
li_class = 'active'
|
|
|
|
count = @search_results.formatted_count(scope)
|
|
|
|
else
|
2019-08-05 03:20:52 -04:00
|
|
|
badge_class = 'js-search-count hidden'
|
|
|
|
badge_data = { url: search_count_path(search_params) }
|
2019-07-15 13:59:57 -04:00
|
|
|
end
|
2014-08-27 02:57:50 -04:00
|
|
|
|
2019-07-15 13:59:57 -04:00
|
|
|
content_tag :li, class: li_class, data: data do
|
|
|
|
link_to search_path(search_params) do
|
|
|
|
concat label
|
|
|
|
concat ' '
|
2019-08-05 03:20:52 -04:00
|
|
|
concat content_tag(:span, count, class: ['badge badge-pill', badge_class], data: badge_data)
|
2019-07-15 13:59:57 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-27 02:57:50 -04:00
|
|
|
end
|
2014-08-27 05:37:31 -04:00
|
|
|
|
2017-06-15 10:01:12 -04:00
|
|
|
def search_filter_input_options(type)
|
2017-08-28 17:56:49 -04:00
|
|
|
opts =
|
|
|
|
{
|
|
|
|
id: "filtered-search-#{type}",
|
2019-04-04 13:05:25 -04:00
|
|
|
placeholder: _('Search or filter results...'),
|
2017-08-28 17:56:49 -04:00
|
|
|
data: {
|
2017-09-19 14:22:43 -04:00
|
|
|
'username-params' => UserSerializer.new.represent(@users)
|
2017-11-20 02:49:13 -05:00
|
|
|
},
|
|
|
|
autocomplete: 'off'
|
2017-06-15 10:01:12 -04:00
|
|
|
}
|
2017-08-02 05:27:24 -04:00
|
|
|
|
2019-06-24 07:42:33 -04:00
|
|
|
opts[:data]['runner-tags-endpoint'] = tag_list_admin_runners_path
|
|
|
|
|
2017-08-02 05:27:24 -04:00
|
|
|
if @project.present?
|
|
|
|
opts[:data]['project-id'] = @project.id
|
2019-06-13 19:39:39 -04:00
|
|
|
opts[:data]['labels-endpoint'] = project_labels_path(@project)
|
|
|
|
opts[:data]['milestones-endpoint'] = project_milestones_path(@project)
|
2019-10-28 17:06:24 -04:00
|
|
|
opts[:data]['releases-endpoint'] = project_releases_path(@project)
|
2018-10-27 13:24:30 -04:00
|
|
|
elsif @group.present?
|
2017-08-28 17:56:49 -04:00
|
|
|
opts[:data]['group-id'] = @group.id
|
2019-06-13 19:39:39 -04:00
|
|
|
opts[:data]['labels-endpoint'] = group_labels_path(@group)
|
|
|
|
opts[:data]['milestones-endpoint'] = group_milestones_path(@group)
|
2018-10-27 13:24:30 -04:00
|
|
|
else
|
2019-06-13 19:39:39 -04:00
|
|
|
opts[:data]['labels-endpoint'] = dashboard_labels_path
|
|
|
|
opts[:data]['milestones-endpoint'] = dashboard_milestones_path
|
2017-08-02 05:27:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opts
|
2017-06-15 10:01:12 -04:00
|
|
|
end
|
|
|
|
|
2018-11-01 10:43:43 -04:00
|
|
|
def search_history_storage_prefix
|
|
|
|
if @project.present?
|
|
|
|
@project.full_path
|
|
|
|
elsif @group.present?
|
|
|
|
@group.full_path
|
|
|
|
else
|
2018-11-07 00:25:08 -05:00
|
|
|
'dashboard'
|
2018-11-01 10:43:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-06 17:55:27 -04:00
|
|
|
# Sanitize a HTML field for search display. Most tags are stripped out and the
|
|
|
|
# maximum length is set to 200 characters.
|
|
|
|
def search_md_sanitize(object, field)
|
|
|
|
html = markdown_field(object, field)
|
|
|
|
html = Truncato.truncate(
|
|
|
|
html,
|
|
|
|
count_tags: false,
|
|
|
|
count_tail: false,
|
|
|
|
max_length: 200
|
|
|
|
)
|
|
|
|
|
|
|
|
# Truncato's filtered_tags and filtered_attributes are not quite the same
|
2014-08-28 04:06:19 -04:00
|
|
|
sanitize(html, tags: %w(a p ol ul li pre code))
|
2014-08-27 05:37:31 -04:00
|
|
|
end
|
2018-01-23 06:03:15 -05:00
|
|
|
|
2019-10-28 14:06:15 -04:00
|
|
|
def show_user_search_tab?
|
2018-12-19 09:24:46 -05:00
|
|
|
return false if Feature.disabled?(:users_search, default_enabled: true)
|
|
|
|
|
2018-12-18 03:35:54 -05:00
|
|
|
if @project
|
|
|
|
project_search_tabs?(:members)
|
|
|
|
else
|
|
|
|
can?(current_user, :read_users_list)
|
|
|
|
end
|
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
SearchHelper.prepend_if_ee('EE::SearchHelper')
|