Welcome to mirror list, hosted at ThFree Co, Russian Federation.

navigation.rb « search « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3594ac0dc306ccbb99858e4e975acb3cdb201b1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# frozen_string_literal: true

module Search
  class Navigation
    include Gitlab::Allowable

    def initialize(user:, project: nil, group: nil, options: {})
      @user = user
      @project = project
      @group = group
      @options = options
    end

    def tab_enabled_for_project?(tab)
      return false unless project.present?

      abilities = Array(search_tab_ability_map[tab])
      Array.wrap(project).any? { |p| abilities.any? { |ability| can?(user, ability, p) } }
    end

    def tabs
      {
        projects: {
          sort: 1,
          label: _("Projects"),
          data: { qa_selector: 'projects_tab' },
          condition: project.nil?
        },
        blobs: {
          sort: 2,
          label: _("Code"),
          data: { qa_selector: 'code_tab' },
          condition: show_code_search_tab?
        },
        #  sort: 3 is reserved for EE items
        issues: {
          sort: 4,
          label: _("Issues"),
          condition: show_issues_search_tab?
        },
        merge_requests: {
          sort: 5,
          label: _("Merge requests"),
          condition: show_merge_requests_search_tab?
        },
        wiki_blobs: {
          sort: 6,
          label: _("Wiki"),
          condition: show_wiki_search_tab?
        },
        commits: {
          sort: 7,
          label: _("Commits"),
          condition: show_commits_search_tab?
        },
        notes: {
          sort: 8,
          label: _("Comments"),
          condition: show_comments_search_tab?
        },
        milestones: {
          sort: 9, label: _("Milestones"),
          condition: show_milestones_search_tab?
        },
        users: {
          sort: 10,
          label: _("Users"),
          condition: show_user_search_tab?
        },
        snippet_titles: {
          sort: 11,
          label: _("Snippets"),
          search: { snippets: true, group_id: nil, project_id: nil },
          condition: show_snippets_search_tab?
        }
      }
    end

    private

    attr_reader :user, :project, :group, :options

    def show_elasticsearch_tabs?
      !!options[:show_elasticsearch_tabs]
    end

    def search_tab_ability_map
      {
        milestones: :read_milestone,
        snippets: :read_snippet,
        issues: :read_issue,
        blobs: :read_code,
        commits: :read_code,
        merge_requests: :read_merge_request,
        notes: [:read_merge_request, :read_code, :read_issue, :read_snippet],
        users: :read_project_member,
        wiki_blobs: :read_wiki
      }
    end

    def show_user_search_tab?
      return true if tab_enabled_for_project?(:users)
      return false unless can?(user, :read_users_list)

      project.nil? && feature_flag_tab_enabled?(:global_search_users_tab)
    end

    def show_code_search_tab?
      return true if tab_enabled_for_project?(:blobs)

      project.nil? && show_elasticsearch_tabs? && feature_flag_tab_enabled?(:global_search_code_tab)
    end

    def show_wiki_search_tab?
      return true if tab_enabled_for_project?(:wiki_blobs)

      project.nil? && show_elasticsearch_tabs? && feature_flag_tab_enabled?(:global_search_wiki_tab)
    end

    def show_commits_search_tab?
      return true if tab_enabled_for_project?(:commits)

      project.nil? && show_elasticsearch_tabs? && feature_flag_tab_enabled?(:global_search_commits_tab)
    end

    def show_issues_search_tab?
      return true if tab_enabled_for_project?(:issues)

      project.nil? && feature_flag_tab_enabled?(:global_search_issues_tab)
    end

    def show_merge_requests_search_tab?
      return true if tab_enabled_for_project?(:merge_requests)

      project.nil? && feature_flag_tab_enabled?(:global_search_merge_requests_tab)
    end

    def show_comments_search_tab?
      return true if tab_enabled_for_project?(:notes)

      project.nil? && show_elasticsearch_tabs?
    end

    def show_snippets_search_tab?
      !!options[:show_snippets] && project.nil? && feature_flag_tab_enabled?(:global_search_snippet_titles_tab)
    end

    def show_milestones_search_tab?
      project.nil? || tab_enabled_for_project?(:milestones)
    end

    def feature_flag_tab_enabled?(flag)
      group.present? || Feature.enabled?(flag, user, type: :ops)
    end
  end
end

Search::Navigation.prepend_mod