BigW Consortium Gitlab

search_helper.rb 5.55 KB
Newer Older
1
module SearchHelper
2
  def search_autocomplete_opts(term)
3
    return unless current_user
4 5 6

    resources_results = [
      groups_autocomplete(term),
7
      projects_autocomplete(term)
8 9
    ].flatten

10 11
    search_pattern = Regexp.new(Regexp.escape(term), "i")

12
    generic_results = project_autocomplete + default_autocomplete + help_autocomplete
13
    generic_results.select! { |result| result[:label] =~ search_pattern }
14

15
    [
16 17
      resources_results,
      generic_results
18 19
    ].flatten.uniq do |item|
      item[:label]
20
    end
21 22
  end

Phil Hughes committed
23
  def search_entries_info(collection, scope, term)
24
    return unless collection.count > 0
25

26 27 28 29 30
    from = collection.offset_value + 1
    to = collection.offset_value + collection.length
    count = collection.total_count

    "Showing #{from} - #{to} of #{count} #{scope.humanize(capitalize: false)} for \"#{term}\""
31 32
  end

33
  def parse_search_result(result)
34
    Gitlab::ProjectSearchResults.parse_search_result(result)
35 36
  end

37 38 39 40 41
  private

  # Autocomplete results for various settings pages
  def default_autocomplete
    [
42
      { category: "Settings", label: "User settings",    url: profile_path },
43 44 45
      { category: "Settings", label: "SSH Keys",         url: profile_keys_path },
      { category: "Settings", label: "Dashboard",        url: root_path },
      { category: "Settings", label: "Admin Section",    url: admin_root_path },
46 47 48 49 50 51
    ]
  end

  # Autocomplete results for internal help pages
  def help_autocomplete
    [
52
      { category: "Help", label: "API Help",           url: help_page_path("api/README") },
53
      { category: "Help", label: "Markdown Help",      url: help_page_path("user/markdown") },
54
      { category: "Help", label: "Permissions Help",   url: help_page_path("user/permissions") },
55 56 57 58
      { 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") },
59
      { category: "Help", label: "Webhooks Help",      url: help_page_path("user/project/integrations/webhooks") },
60
      { category: "Help", label: "Workflow Help",      url: help_page_path("workflow/README") },
61 62 63 64 65 66
    ]
  end

  # Autocomplete results for the current project, if it's defined
  def project_autocomplete
    if @project && @project.repository.exists? && @project.repository.root_ref
67
      ref = @ref || @project.repository.root_ref
68 69

      [
70 71 72 73 74 75 76 77
        { category: "Current Project", label: "Files",          url: namespace_project_tree_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Commits",        url: namespace_project_commits_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Network",        url: namespace_project_network_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Graph",          url: namespace_project_graph_path(@project.namespace, @project, ref) },
        { category: "Current Project", label: "Issues",         url: namespace_project_issues_path(@project.namespace, @project) },
        { category: "Current Project", label: "Merge Requests", url: namespace_project_merge_requests_path(@project.namespace, @project) },
        { category: "Current Project", label: "Milestones",     url: namespace_project_milestones_path(@project.namespace, @project) },
        { category: "Current Project", label: "Snippets",       url: namespace_project_snippets_path(@project.namespace, @project) },
78
        { category: "Current Project", label: "Members",        url: namespace_project_settings_members_path(@project.namespace, @project) },
79
        { category: "Current Project", label: "Wiki",           url: namespace_project_wikis_path(@project.namespace, @project) },
80 81 82 83 84 85 86
      ]
    else
      []
    end
  end

  # Autocomplete results for the current user's groups
87
  def groups_autocomplete(term, limit = 5)
88
    current_user.authorized_groups.search(term).limit(limit).map do |group|
89
      {
90
        category: "Groups",
91
        id: group.id,
92
        label: "#{search_result_sanitize(group.full_name)}",
93 94
        url: group_path(group)
      }
95 96 97 98
    end
  end

  # Autocomplete results for the current user's projects
99
  def projects_autocomplete(term, limit = 5)
100
    current_user.authorized_projects.search_by_title(term).
101
      sorted_by_stars.non_archived.limit(limit).map do |p|
102
      {
103
        category: "Projects",
104 105 106
        id: p.id,
        value: "#{search_result_sanitize(p.name)}",
        label: "#{search_result_sanitize(p.name_with_namespace)}",
Vinnie Okada committed
107
        url: namespace_project_path(p.namespace, p)
108
      }
109 110
    end
  end
111

112
  def search_result_sanitize(str)
113 114
    Sanitize.clean(str)
  end
115

116
  def search_filter_path(options = {})
117 118 119 120
    exist_opts = {
      search: params[:search],
      project_id: params[:project_id],
      group_id: params[:group_id],
barthc committed
121 122
      scope: params[:scope],
      repository_ref: params[:repository_ref]
123 124 125
    }

    options = exist_opts.merge(options)
126
    search_path(options)
127
  end
128

129 130 131 132 133 134 135 136 137 138 139 140
  # 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
141
    sanitize(html, tags: %w(a p ol ul li pre code))
142
  end
143
end