summaryrefslogtreecommitdiff
path: root/app/finders/issuable_finder/params.rb
blob: 359a56bd39b71a6a444ccbe75bb0829e1793dff4 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true

class IssuableFinder
  class Params < SimpleDelegator
    include Gitlab::Utils::StrongMemoize

    # This is used as a common filter for None / Any / Upcoming / Started
    FILTER_NONE = 'none'
    FILTER_ANY = 'any'
    FILTER_STARTED = 'started'
    FILTER_UPCOMING = 'upcoming'

    # This is used in unassigning users
    NONE = '0'

    alias_method :params, :__getobj__

    attr_accessor :current_user, :klass

    def initialize(params, current_user, klass)
      @current_user = current_user
      @klass = klass
      # We turn the params into a HashWithIndifferentAccess. We must use #to_h first because sometimes
      # we get ActionController::Params and IssuableFinder::Params objects here.
      super(params.to_h.with_indifferent_access)
    end

    def present?
      params.present?
    end

    def milestones?
      params[:milestone_title].present? || params[:milestone_wildcard_id].present?
    end

    def filter_by_no_milestone?
      # Usage of `No Milestone` and `none`/`None` in milestone_title to be deprecated
      # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
      params[:milestone_title].to_s.downcase == FILTER_NONE ||
        params[:milestone_title] == Milestone::None.title ||
        params[:milestone_wildcard_id].to_s.downcase == FILTER_NONE
    end

    def filter_by_any_milestone?
      # Usage of `Any Milestone` and `any`/`Any` in milestone_title to be deprecated
      # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
      params[:milestone_title].to_s.downcase == FILTER_ANY ||
        params[:milestone_title] == Milestone::Any.title ||
        params[:milestone_wildcard_id].to_s.downcase == FILTER_ANY
    end

    def filter_by_upcoming_milestone?
      # Usage of `#upcoming` in milestone_title to be deprecated
      # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
      params[:milestone_title] == Milestone::Upcoming.name || params[:milestone_wildcard_id].to_s.downcase == FILTER_UPCOMING
    end

    def filter_by_started_milestone?
      # Usage of `#started` in milestone_title to be deprecated
      # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
      params[:milestone_title] == Milestone::Started.name || params[:milestone_wildcard_id].to_s.downcase == FILTER_STARTED
    end

    def filter_by_no_release?
      params[:release_tag].to_s.downcase == FILTER_NONE
    end

    def filter_by_any_release?
      params[:release_tag].to_s.downcase == FILTER_ANY
    end

    def filter_by_no_reaction?
      params[:my_reaction_emoji].to_s.downcase == FILTER_NONE
    end

    def filter_by_any_reaction?
      params[:my_reaction_emoji].to_s.downcase == FILTER_ANY
    end

    def releases?
      params[:release_tag].present?
    end

    def project?
      project_id.present?
    end

    def group?
      group_id.present?
    end

    def related_groups
      if project? && project&.group && Ability.allowed?(current_user, :read_group, project.group)
        project.group.self_and_ancestors
      elsif group
        [group]
      elsif current_user
        Gitlab::ObjectHierarchy.new(current_user.authorized_groups, current_user.groups).all_objects
      else
        []
      end
    end

    def project
      strong_memoize(:project) do
        next nil unless project?

        project = project_id.is_a?(Project) ? project_id : Project.find(project_id)
        project = nil unless Ability.allowed?(current_user, :"read_#{klass.to_ability_name}", project)

        project
      end
    end

    def group
      strong_memoize(:group) do
        next nil unless group?

        group = group_id.is_a?(Group) ? group_id : Group.find(group_id)
        group = nil unless Ability.allowed?(current_user, :read_group, group)

        group
      end
    end

    def project_id
      params[:project_id]
    end

    def group_id
      params[:group_id]
    end

    def projects
      strong_memoize(:projects) do
        next [project] if project?

        projects =
          if current_user && params[:authorized_only].presence && !current_user_related?
            current_user.authorized_projects(min_access_level)
          else
            projects_public_or_visible_to_user
          end

        projects.with_feature_available_for_user(klass, current_user).reorder(nil) # rubocop: disable CodeReuse/ActiveRecord
      end
    end

    def milestones
      strong_memoize(:milestones) do
        if milestones?
          if project?
            project_group_id = project.group&.id
            project_id = project.id
          end

          project_group_id = group.id if group

          search_params =
            { title: params[:milestone_title], project_ids: project_id, group_ids: project_group_id }

          MilestonesFinder.new(search_params).execute # rubocop: disable CodeReuse/Finder
        else
          Milestone.none
        end
      end
    end

    def current_user_related?
      scope = params[:scope]
      scope == 'created_by_me' || scope == 'authored' || scope == 'assigned_to_me'
    end

    def find_group_projects
      return Project.none unless group

      if params[:include_subgroups]
        Project.where(namespace_id: group.self_and_descendants) # rubocop: disable CodeReuse/ActiveRecord
      else
        group.projects
      end
    end

    # We use Hash#merge in a few places, so let's support it
    def merge(other)
      self.class.new(params.merge(other), current_user, klass)
    end

    # Just for symmetry, and in case someone tries to use it
    def merge!(other)
      params.merge!(other)
    end

    def parent
      project || group
    end

    private

    def projects_public_or_visible_to_user
      projects =
        if group
          if params[:projects]
            find_group_projects.id_in(params[:projects])
          else
            find_group_projects
          end
        elsif params[:projects]
          Project.id_in(params[:projects])
        else
          Project
        end

      projects.public_or_visible_to_user(current_user, min_access_level)
    end

    def min_access_level
      ProjectFeature.required_minimum_access_level(klass)
    end

    def method_missing(method_name, *args, &block)
      if method_name[-1] == '?'
        params[method_name[0..-2].to_sym].present?
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      method_name[-1] == '?'
    end
  end
end