2018-08-18 11:19:57 +00:00
# frozen_string_literal: true
2013-10-20 05:05:20 +00:00
module SearchHelper
2022-07-01 12:08:08 +00:00
# params which should persist when a new tab is selected
2021-02-12 09:08:48 +00:00
SEARCH_GENERIC_PARAMS = [
:search ,
:scope ,
:project_id ,
:group_id ,
:repository_ref ,
:snippets ,
:sort ,
2021-06-15 12:10:11 +00:00
:force_search_results ,
:project_ids
2021-02-12 09:08:48 +00:00
] . freeze
2019-10-31 15:06:41 +00:00
2022-07-28 15:09:27 +00:00
def search_autocomplete_opts ( term , filter : nil )
2020-07-10 09:09:01 +00:00
return unless current_user
2022-07-28 15:09:27 +00:00
results = case filter & . to_sym
when :search
resource_results ( term )
when :generic
[
2022-08-02 09:11:05 +00:00
recent_items_autocomplete ( term ) ,
generic_results ( term )
2022-07-28 15:09:27 +00:00
]
else
[
2022-08-02 09:11:05 +00:00
recent_items_autocomplete ( term ) ,
2022-07-28 15:09:27 +00:00
resource_results ( term ) ,
2022-08-02 09:11:05 +00:00
generic_results ( term )
2022-07-28 15:09:27 +00:00
]
end
results . flatten { | item | item [ :label ] }
end
def resource_results ( term )
[
2020-07-10 09:09:01 +00:00
groups_autocomplete ( term ) ,
2020-10-22 12:08:41 +00:00
projects_autocomplete ( term ) ,
issue_autocomplete ( term )
2020-07-10 09:09:01 +00:00
] . flatten
2022-07-28 15:09:27 +00:00
end
2020-07-10 09:09:01 +00:00
2022-07-28 15:09:27 +00:00
def generic_results ( term )
2020-07-10 09:09:01 +00:00
search_pattern = Regexp . new ( Regexp . escape ( term ) , " i " )
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
generic_results . concat ( default_autocomplete_admin ) if current_user . admin?
2022-07-28 15:09:27 +00:00
generic_results . select { | result | result [ :label ] =~ search_pattern }
2020-07-10 09:09:01 +00:00
end
2020-10-05 15:08:56 +00:00
def recent_items_autocomplete ( term )
recent_merge_requests_autocomplete ( term ) + recent_issues_autocomplete ( term )
end
2016-04-15 16:00:53 +00:00
def search_entries_info ( collection , scope , term )
2018-12-21 11:44:37 +00:00
return if collection . to_a . empty?
2016-04-15 13:13:21 +00:00
2016-04-26 08:02:28 +00:00
from = collection . offset_value + 1
2018-12-21 11:44:37 +00:00
to = collection . offset_value + collection . to_a . size
2016-04-26 08:02:28 +00:00
count = collection . total_count
2019-12-05 18:07:51 +00:00
term_element = " <span> <code> #{ h ( term ) } </code> </span> " . html_safe
2016-04-26 08:02:28 +00:00
2019-08-30 09:49:14 +00:00
search_entries_info_template ( collection ) % {
from : from ,
to : to ,
count : count ,
2019-09-19 09:06:27 +00:00
scope : search_entries_scope_label ( scope , count ) ,
2019-12-05 18:07:51 +00:00
term_element : term_element
2019-08-30 09:49:14 +00:00
}
end
2019-09-19 09:06:27 +00:00
def search_entries_scope_label ( scope , count )
2019-08-30 09:49:14 +00:00
case scope
2019-09-19 09:06:27 +00:00
when 'blobs'
ns_ ( 'SearchResults|code result' , 'SearchResults|code results' , count )
2019-08-30 09:49:14 +00: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 )
when 'snippet_titles'
ns_ ( 'SearchResults|snippet' , 'SearchResults|snippets' , count )
when 'users'
ns_ ( 'SearchResults|user' , 'SearchResults|users' , count )
2019-09-19 09:06:27 +00:00
when 'wiki_blobs'
ns_ ( 'SearchResults|wiki result' , 'SearchResults|wiki results' , count )
2019-08-30 09:49:14 +00:00
else
raise " Unrecognized search scope ' #{ scope } ' "
end
end
def search_entries_info_template ( collection )
if collection . total_pages > 1
2021-09-29 12:11:22 +00:00
s_ ( " SearchResults|Showing %{from} - %{to} of %{count} %{scope} for %{term_element} " ) . html_safe
2019-08-30 09:49:14 +00:00
else
2021-09-29 12:11:22 +00:00
s_ ( " SearchResults|Showing %{count} %{scope} for %{term_element} " ) . html_safe
2019-08-30 09:49:14 +00:00
end
2016-04-15 13:13:21 +00:00
end
2020-11-03 18:09:22 +00:00
def search_entries_empty_message ( scope , term , group , project )
options = {
2019-09-19 09:06:27 +00:00
scope : search_entries_scope_label ( scope , 0 ) ,
2020-11-03 18:09:22 +00:00
term : " <code> #{ h ( term ) } </code> " . html_safe
}
# We check project first because we have 3 possible combinations here:
# - group && project
# - group
# - group: nil, project: nil
if project
html_escape ( _ ( " We couldn't find any %{scope} matching %{term} in project %{project} " ) ) % options . merge (
project : link_to ( project . full_name , project_path ( project ) , target : '_blank' , rel : 'noopener noreferrer' ) . html_safe
)
elsif group
html_escape ( _ ( " We couldn't find any %{scope} matching %{term} in group %{group} " ) ) % options . merge (
group : link_to ( group . full_name , group_path ( group ) , target : '_blank' , rel : 'noopener noreferrer' ) . html_safe
)
else
html_escape ( _ ( " We couldn't find any %{scope} matching %{term} " ) ) % options
end
2019-09-19 09:06:27 +00:00
end
2020-09-29 03:09:57 +00:00
def repository_ref ( project )
2022-05-20 12:08:50 +00:00
return project . default_branch unless params [ :project_id ]
2020-09-29 03:09:57 +00:00
# Always #to_s the repository_ref param in case the value is also a number
params [ :repository_ref ] . to_s . presence || project . default_branch
end
2020-07-08 06:09:13 +00:00
# Overridden in EE
2019-10-24 00:07:18 +00:00
def search_blob_title ( project , path )
path
2016-09-14 10:29:23 +00:00
end
2019-03-06 19:33:06 +00:00
def search_service
2022-07-01 12:08:08 +00:00
@search_service || = :: SearchService . new ( current_user , sanitized_search_params )
2019-03-06 19:33:06 +00:00
end
2021-02-02 18:09:42 +00:00
def search_sort_options
2021-07-14 12:09:23 +00:00
options = [
2021-02-10 12:09:45 +00:00
{
title : _ ( 'Created date' ) ,
sortable : true ,
sortParam : {
asc : 'created_asc' ,
desc : 'created_desc'
}
} ,
{
2022-01-18 21:16:02 +00:00
title : _ ( 'Updated date' ) ,
2021-02-10 12:09:45 +00:00
sortable : true ,
sortParam : {
asc : 'updated_asc' ,
desc : 'updated_desc'
}
2021-02-02 18:09:42 +00:00
}
2021-02-10 12:09:45 +00:00
]
2021-07-14 12:09:23 +00:00
2021-08-06 03:08:43 +00:00
if search_service . scope == 'issues'
2021-07-14 12:09:23 +00:00
options << {
title : _ ( 'Popularity' ) ,
sortable : true ,
sortParam : {
asc : 'popularity_asc' ,
desc : 'popularity_desc'
}
}
end
options
2021-02-02 18:09:42 +00:00
end
2022-10-25 00:09:22 +00:00
def search_group
# group gets derived from the Project in the project's scope
@group || @project & . group
end
def search_has_group?
search_group & . present? && search_group & . persisted?
end
2022-02-17 12:12:30 +00:00
2022-10-25 00:09:22 +00:00
def search_has_project?
@project & . present? && @project & . persisted?
end
2022-02-17 12:12:30 +00:00
2022-10-25 00:09:22 +00:00
def header_search_context
{ } . tap do | hash |
if search_has_group?
hash [ :group ] = { id : search_group . id , name : search_group . name , full_name : search_group . full_name }
hash [ :group_metadata ] = { issues_path : issues_group_path ( search_group ) , mr_path : merge_requests_group_path ( search_group ) }
end
if search_has_project?
hash [ :project ] = { id : @project . id , name : @project . name }
hash [ :project_metadata ] = { issues_path : project_issues_path ( @project ) , mr_path : project_merge_requests_path ( @project ) }
hash [ :code_search ] = search_scope . nil?
hash [ :ref ] = @ref if @ref && can? ( current_user , :download_code , @project )
end
hash [ :scope ] = search_scope if search_has_project? || search_has_group?
hash [ :for_snippets ] = @snippet & . present? || @snippets & . any?
end
end
2022-02-17 12:12:30 +00:00
2022-10-25 00:09:22 +00:00
def search_scope
if current_controller? ( :issues )
'issues'
elsif current_controller? ( :merge_requests )
'merge_requests'
elsif current_controller? ( :wikis )
'wiki_blobs'
elsif current_controller? ( :commits )
'commits'
elsif current_controller? ( :groups )
if %w( issues merge_requests ) . include? ( controller . action_name )
controller . action_name
end
2022-02-17 12:12:30 +00:00
end
end
2013-10-20 05:05:20 +00:00
private
2020-07-10 09:09:01 +00:00
# Autocomplete results for various settings pages
def default_autocomplete
[
{ category : " Settings " , label : _ ( " User settings " ) , url : profile_path } ,
{ category : " Settings " , label : _ ( " SSH Keys " ) , url : profile_keys_path } ,
{ category : " Settings " , label : _ ( " Dashboard " ) , url : root_path }
]
end
# Autocomplete results for settings pages, for admins
def default_autocomplete_admin
[
{ category : " Settings " , label : _ ( " Admin Section " ) , url : admin_root_path }
]
end
# Autocomplete results for internal help pages
def help_autocomplete
[
2021-06-28 15:08:03 +00:00
{ category : " Help " , label : _ ( " API Help " ) , url : help_page_path ( " api/index " ) } ,
2020-07-10 09:09:01 +00:00
{ category : " Help " , label : _ ( " Markdown Help " ) , url : help_page_path ( " user/markdown " ) } ,
{ category : " Help " , label : _ ( " Permissions Help " ) , url : help_page_path ( " user/permissions " ) } ,
2022-06-27 21:08:29 +00:00
{ category : " Help " , label : _ ( " Public Access Help " ) , url : help_page_path ( " user/public_access " ) } ,
2021-07-22 06:08:43 +00:00
{ category : " Help " , label : _ ( " Rake Tasks Help " ) , url : help_page_path ( " raketasks/index " ) } ,
2022-06-27 21:08:29 +00:00
{ category : " Help " , label : _ ( " SSH Keys Help " ) , url : help_page_path ( " user/ssh " ) } ,
{ category : " Help " , label : _ ( " System Hooks Help " ) , url : help_page_path ( " administration/system_hooks " ) } ,
2020-10-06 09:08:32 +00:00
{ category : " Help " , label : _ ( " Webhooks Help " ) , url : help_page_path ( " user/project/integrations/webhooks " ) }
2020-07-10 09:09:01 +00:00
]
end
# Autocomplete results for the current project, if it's defined
def project_autocomplete
if @project && @project . repository . root_ref
ref = @ref || @project . repository . root_ref
2021-12-07 03:12:22 +00:00
result = [ ]
if can? ( current_user , :download_code , @project )
result . concat ( [
2022-09-09 09:14:13 +00: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 ) }
] )
2021-12-07 03:12:22 +00:00
end
if can? ( current_user , :read_repository_graphs , @project )
result . concat ( [
2022-09-09 09:14:13 +00:00
{ category : " In this project " , label : _ ( " Network " ) , url : project_network_path ( @project , ref ) } ,
{ category : " In this project " , label : _ ( " Graph " ) , url : project_graph_path ( @project , ref ) }
] )
2021-12-07 03:12:22 +00:00
end
result . concat ( [
2022-09-09 09:14:13 +00:00
{ 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 ) }
] )
2020-10-30 09:08:39 +00:00
if can? ( current_user , :read_feature_flag , @project )
result << { category : " In this project " , label : _ ( " Feature Flags " ) , url : project_feature_flags_path ( @project ) }
end
result
2020-07-10 09:09:01 +00:00
else
[ ]
end
end
# Autocomplete results for the current user's groups
# rubocop: disable CodeReuse/ActiveRecord
def groups_autocomplete ( term , limit = 5 )
current_user . authorized_groups . order_id_desc . search ( term ) . limit ( limit ) . map do | group |
{
category : " Groups " ,
id : group . id ,
2022-04-14 18:08:29 +00:00
value : " #{ search_result_sanitize ( group . name ) } " ,
2020-07-10 09:09:01 +00:00
label : " #{ search_result_sanitize ( group . full_name ) } " ,
url : group_path ( group ) ,
avatar_url : group . avatar_url || ''
}
end
end
# rubocop: enable CodeReuse/ActiveRecord
2020-10-22 12:08:41 +00:00
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 )
[
2022-09-09 09:14:13 +00:00
{
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 || ''
}
2020-10-22 12:08:41 +00:00
]
end
2020-07-10 09:09:01 +00:00
# Autocomplete results for the current user's projects
# rubocop: disable CodeReuse/ActiveRecord
def projects_autocomplete ( term , limit = 5 )
current_user . authorized_projects . order_id_desc . search_by_title ( term )
. sorted_by_stars_desc . non_archived . limit ( limit ) . map do | p |
{
category : " Projects " ,
id : p . id ,
value : " #{ search_result_sanitize ( p . name ) } " ,
label : " #{ search_result_sanitize ( p . full_name ) } " ,
url : project_path ( p ) ,
avatar_url : p . avatar_url || ''
}
end
end
2020-09-02 12:10:35 +00:00
2020-10-05 15:08:56 +00:00
def recent_merge_requests_autocomplete ( term )
2020-09-18 06:09:31 +00:00
return [ ] unless current_user
2020-10-05 15:08:56 +00:00
:: Gitlab :: Search :: RecentMergeRequests . new ( user : current_user ) . search ( term ) . map do | mr |
2020-09-18 06:09:31 +00:00
{
category : " Recent merge requests " ,
id : mr . id ,
label : search_result_sanitize ( mr . title ) ,
url : merge_request_path ( mr ) ,
2022-04-14 18:08:29 +00:00
avatar_url : mr . project . avatar_url || '' ,
project_id : mr . target_project_id ,
project_name : mr . target_project . name
2020-09-18 06:09:31 +00:00
}
end
end
2020-10-05 15:08:56 +00:00
def recent_issues_autocomplete ( term )
2020-09-02 12:10:35 +00:00
return [ ] unless current_user
2020-10-05 15:08:56 +00:00
:: Gitlab :: Search :: RecentIssues . new ( user : current_user ) . search ( term ) . map do | i |
2020-09-02 12:10:35 +00:00
{
category : " Recent issues " ,
id : i . id ,
label : search_result_sanitize ( i . title ) ,
url : issue_path ( i ) ,
2022-04-14 18:08:29 +00:00
avatar_url : i . project . avatar_url || '' ,
project_id : i . project_id ,
project_name : i . project . name
2020-09-02 12:10:35 +00:00
}
end
end
2020-07-10 09:09:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2014-01-18 14:06:20 +00:00
def search_result_sanitize ( str )
2014-01-18 12:16:46 +00:00
Sanitize . clean ( str )
end
2014-08-27 06:57:50 +00:00
2021-02-12 09:08:48 +00:00
def search_filter_link ( scope , label , data : { } , search : { } )
search_params = params
. merge ( search )
. merge ( { scope : scope } )
. permit ( SEARCH_GENERIC_PARAMS )
2019-07-15 17:59:57 +00:00
2021-02-12 09:08:48 +00:00
if @scope == scope
li_class = 'active'
2021-07-09 12:08:17 +00:00
count = @timeout ? 0 : @search_results . formatted_count ( scope )
2021-02-12 09:08:48 +00:00
else
badge_class = 'js-search-count hidden'
badge_data = { url : search_count_path ( search_params ) }
end
2021-02-09 12:09:48 +00:00
2021-02-12 09:08:48 +00:00
content_tag :li , class : li_class , data : data do
link_to search_path ( search_params ) do
concat label
concat ' '
2022-01-10 06:13:42 +00:00
concat gl_badge_tag ( count , { size : :sm } , { class : badge_class , data : badge_data } )
2021-02-12 09:08:48 +00:00
end
end
2014-08-27 06:57:50 +00:00
end
2014-08-27 09:37:31 +00:00
2022-10-08 18:09:01 +00:00
def search_filter_link_json ( scope , label , data , search )
2022-10-14 21:09:20 +00:00
scope_name = scope . to_s
search_params = params . merge ( search ) . merge ( { scope : scope_name } ) . permit ( SEARCH_GENERIC_PARAMS )
active_scope = @scope == scope_name
2022-10-08 18:09:01 +00:00
2022-10-14 21:09:20 +00:00
result = { label : label , scope : scope_name , data : data , link : search_path ( search_params ) , active : active_scope }
2022-11-08 15:09:34 +00:00
if active_scope
result [ :count ] = ! @timeout ? @search_results . formatted_count ( scope_name ) : 0
end
2022-10-08 18:09:01 +00:00
result [ :count_link ] = search_count_path ( search_params ) unless active_scope
result
end
# search page scope navigation
def search_navigation
{
2022-11-08 15:09:34 +00:00
projects : { sort : 1 , label : _ ( " Projects " ) , data : { qa_selector : 'projects_tab' } , condition : @project . nil? } ,
2022-11-08 21:10:57 +00:00
blobs : { sort : 2 , label : _ ( " Code " ) , data : { qa_selector : 'code_tab' } , condition : project_search_tabs? ( :blobs ) || ( search_service . show_elasticsearch_tabs? && feature_flag_tab_enabled? ( :global_search_code_tab ) ) } ,
2022-11-08 15:09:34 +00:00
# sort: 3 is reserved for EE items
issues : { sort : 4 , label : _ ( " Issues " ) , condition : project_search_tabs? ( :issues ) || feature_flag_tab_enabled? ( :global_search_issues_tab ) } ,
merge_requests : { sort : 5 , label : _ ( " Merge requests " ) , condition : project_search_tabs? ( :merge_requests ) || feature_flag_tab_enabled? ( :global_search_merge_requests_tab ) } ,
wiki_blobs : { sort : 6 , label : _ ( " Wiki " ) , condition : project_search_tabs? ( :wiki ) || search_service . show_elasticsearch_tabs? } ,
commits : { sort : 7 , label : _ ( " Commits " ) , condition : project_search_tabs? ( :commits ) || ( search_service . show_elasticsearch_tabs? && feature_flag_tab_enabled? ( :global_search_commits_tab ) ) } ,
notes : { sort : 8 , label : _ ( " Comments " ) , condition : project_search_tabs? ( :notes ) || search_service . show_elasticsearch_tabs? } ,
milestones : { sort : 9 , label : _ ( " Milestones " ) , condition : project_search_tabs? ( :milestones ) || @project . nil? } ,
users : { sort : 10 , label : _ ( " Users " ) , condition : show_user_search_tab? } ,
snippet_titles : { sort : 11 , label : _ ( " Titles and Descriptions " ) , search : { snippets : true , group_id : nil , project_id : nil } , condition : @show_snippets . present? && @project . nil? }
2022-10-08 18:09:01 +00:00
}
end
def search_navigation_json
2022-11-08 15:09:34 +00:00
sorted_navigation = search_navigation . sort_by { | _ , h | h [ :sort ] }
2022-11-09 15:07:50 +00:00
sorted_navigation . each_with_object ( { } ) do | ( key , value ) , hash |
2022-10-14 21:09:20 +00:00
hash [ key ] = search_filter_link_json ( key , value [ :label ] , value [ :data ] , value [ :search ] ) if value [ :condition ]
2022-11-09 15:07:50 +00:00
end . to_json
2022-10-08 18:09:01 +00:00
end
2020-05-04 15:09:38 +00:00
def search_filter_input_options ( type , placeholder = _ ( 'Search or filter results...' ) )
2017-08-28 21:56:49 +00:00
opts =
{
id : " filtered-search- #{ type } " ,
2020-05-04 15:09:38 +00:00
placeholder : placeholder ,
2017-08-28 21:56:49 +00:00
data : {
2017-09-19 18:22:43 +00:00
'username-params' = > UserSerializer . new . represent ( @users )
2017-11-20 07:49:13 +00:00
} ,
autocomplete : 'off'
2017-06-15 14:01:12 +00:00
}
2017-08-02 09:27:24 +00:00
if @project . present?
opts [ :data ] [ 'project-id' ] = @project . id
2019-06-13 23:39:39 +00:00
opts [ :data ] [ 'labels-endpoint' ] = project_labels_path ( @project )
opts [ :data ] [ 'milestones-endpoint' ] = project_milestones_path ( @project )
2019-10-28 21:06:24 +00:00
opts [ :data ] [ 'releases-endpoint' ] = project_releases_path ( @project )
2020-10-19 06:09:08 +00:00
opts [ :data ] [ 'environments-endpoint' ] =
unfoldered_environment_names_project_path ( @project )
2018-10-27 17:24:30 +00:00
elsif @group . present?
2017-08-28 21:56:49 +00:00
opts [ :data ] [ 'group-id' ] = @group . id
2019-06-13 23:39:39 +00:00
opts [ :data ] [ 'labels-endpoint' ] = group_labels_path ( @group )
opts [ :data ] [ 'milestones-endpoint' ] = group_milestones_path ( @group )
2020-08-12 12:10:25 +00:00
opts [ :data ] [ 'releases-endpoint' ] = group_releases_path ( @group )
2020-10-19 06:09:08 +00:00
opts [ :data ] [ 'environments-endpoint' ] =
unfoldered_environment_names_group_path ( @group )
2018-10-27 17:24:30 +00:00
else
2019-06-13 23:39:39 +00:00
opts [ :data ] [ 'labels-endpoint' ] = dashboard_labels_path
opts [ :data ] [ 'milestones-endpoint' ] = dashboard_milestones_path
2017-08-02 09:27:24 +00:00
end
opts
2017-06-15 14:01:12 +00:00
end
2018-11-01 14:43:43 +00:00
def search_history_storage_prefix
if @project . present?
@project . full_path
elsif @group . present?
@group . full_path
else
2018-11-07 05:25:08 +00:00
'dashboard'
2018-11-01 14:43:43 +00:00
end
end
2021-03-15 09:09:11 +00:00
def search_md_sanitize ( source )
search_sanitize ( markdown ( search_truncate ( source ) ) )
end
def simple_search_highlight_and_truncate ( text , phrase , options = { } )
highlight ( search_sanitize ( search_truncate ( text ) ) , phrase . split , options )
end
2016-10-06 21:55:27 +00:00
# Sanitize a HTML field for search display. Most tags are stripped out and the
# maximum length is set to 200 characters.
2021-03-15 09:09:11 +00:00
def search_truncate ( source )
Truncato . truncate (
2020-08-25 06:10:18 +00:00
source ,
2016-10-06 21:55:27 +00:00
count_tags : false ,
count_tail : false ,
2021-03-15 09:09:11 +00:00
filtered_tags : %w( img ) ,
2016-10-06 21:55:27 +00:00
max_length : 200
)
2021-03-15 09:09:11 +00:00
end
2016-10-06 21:55:27 +00:00
2021-03-15 09:09:11 +00:00
def search_sanitize ( html )
2016-10-06 21:55:27 +00:00
# Truncato's filtered_tags and filtered_attributes are not quite the same
2014-08-28 08:06:19 +00:00
sanitize ( html , tags : %w( a p ol ul li pre code ) )
2014-08-27 09:37:31 +00:00
end
2018-01-23 11:03:15 +00:00
2020-09-23 03:09:30 +00:00
# _search_highlight is used in EE override
2020-11-07 00:08:58 +00:00
def highlight_and_truncate_issuable ( issuable , search_term , _search_highlight )
return unless issuable . description . present?
2020-10-08 15:08:17 +00:00
2020-12-03 03:09:27 +00:00
simple_search_highlight_and_truncate ( issuable . description , search_term , highlighter : '<span class="gl-text-gray-900 gl-font-weight-bold">\1</span>' )
2020-09-23 03:09:30 +00:00
end
2019-10-28 18:06:15 +00:00
def show_user_search_tab?
2022-04-14 15:08:59 +00:00
return project_search_tabs? ( :members ) if @project
return false unless can? ( current_user , :read_users_list )
return true if @group
2022-05-06 15:09:03 +00:00
Feature . enabled? ( :global_search_users_tab , current_user , type : :ops )
2018-12-18 08:35:54 +00:00
end
2020-11-07 00:08:58 +00:00
def issuable_state_to_badge_class ( issuable )
# Closed is considered "danger" for MR so we need to handle separately
if issuable . is_a? ( :: MergeRequest )
if issuable . merged?
2021-02-25 09:10:45 +00:00
:info
2020-11-07 00:08:58 +00:00
elsif issuable . closed?
:danger
else
:success
end
else
if issuable . closed?
:info
else
:success
end
end
end
def issuable_state_text ( issuable )
case issuable . state
when 'merged'
_ ( " Merged " )
when 'closed'
_ ( " Closed " )
else
_ ( " Open " )
end
end
2021-08-26 21:11:25 +00:00
def feature_flag_tab_enabled? ( flag )
2022-05-11 00:08:02 +00:00
@group || Feature . enabled? ( flag , current_user , type : :ops )
2021-08-26 21:11:25 +00:00
end
2022-07-01 12:08:08 +00:00
def sanitized_search_params
sanitized_params = params . dup
sanitized_params [ :confidential ] = Gitlab :: Utils . to_boolean ( sanitized_params [ :confidential ] ) if sanitized_params . key? ( :confidential )
sanitized_params
end
2013-10-20 05:05:20 +00:00
end
2019-09-13 13:26:31 +00:00
2021-05-11 21:10:21 +00:00
SearchHelper . prepend_mod_with ( 'SearchHelper' )