diff options
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/ci/auth_job_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/ci/runners_finder.rb | 8 | ||||
-rw-r--r-- | app/finders/clusters/agents_finder.rb | 19 | ||||
-rw-r--r-- | app/finders/contributed_projects_finder.rb | 11 | ||||
-rw-r--r-- | app/finders/groups/user_groups_finder.rb | 8 | ||||
-rw-r--r-- | app/finders/issuable_finder.rb | 9 | ||||
-rw-r--r-- | app/finders/projects_finder.rb | 13 | ||||
-rw-r--r-- | app/finders/snippets_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/user_recent_events_finder.rb | 50 |
9 files changed, 70 insertions, 52 deletions
diff --git a/app/finders/ci/auth_job_finder.rb b/app/finders/ci/auth_job_finder.rb index 2dbdcb3c472..360afe5a7ab 100644 --- a/app/finders/ci/auth_job_finder.rb +++ b/app/finders/ci/auth_job_finder.rb @@ -29,7 +29,7 @@ module Ci private - attr_reader :token, :require_running, :raise_on_missing + attr_reader :token def find_job_by_token ::Ci::Build.find_by_token(token) diff --git a/app/finders/ci/runners_finder.rb b/app/finders/ci/runners_finder.rb index 356915722fe..4f9244d9825 100644 --- a/app/finders/ci/runners_finder.rb +++ b/app/finders/ci/runners_finder.rb @@ -17,6 +17,7 @@ module Ci search! filter_by_active! filter_by_status! + filter_by_upgrade_status! filter_by_runner_type! filter_by_tag_list! sort! @@ -67,6 +68,13 @@ module Ci filter_by!(:status_status, Ci::Runner::AVAILABLE_STATUSES) end + def filter_by_upgrade_status! + return unless @params.key?(:upgrade_status) + return unless Ci::RunnerVersion.statuses.key?(@params[:upgrade_status]) + + @runners = @runners.with_upgrade_status(@params[:upgrade_status]) + end + def filter_by_runner_type! filter_by!(:type_type, Ci::Runner::AVAILABLE_TYPES) end diff --git a/app/finders/clusters/agents_finder.rb b/app/finders/clusters/agents_finder.rb index d0b1240157c..14277db3f85 100644 --- a/app/finders/clusters/agents_finder.rb +++ b/app/finders/clusters/agents_finder.rb @@ -4,8 +4,8 @@ module Clusters class AgentsFinder include FinderMethods - def initialize(project, current_user, params: {}) - @project = project + def initialize(object, current_user, params: {}) + @object = object @current_user = current_user @params = params end @@ -13,18 +13,25 @@ module Clusters def execute return ::Clusters::Agent.none unless can_read_cluster_agents? - agents = project.cluster_agents - agents = agents.with_name(params[:name]) if params[:name].present? + agents = filter_clusters(object.cluster_agents) agents.ordered_by_name end private - attr_reader :project, :current_user, :params + attr_reader :object, :current_user, :params + + def filter_clusters(agents) + agents = agents.with_name(params[:name]) if params[:name].present? + + agents + end def can_read_cluster_agents? - current_user.can?(:read_cluster, project) + current_user&.can?(:read_cluster, object) end end end + +Clusters::AgentsFinder.prepend_mod_with('Clusters::AgentsFinder') diff --git a/app/finders/contributed_projects_finder.rb b/app/finders/contributed_projects_finder.rb index a351d30229e..eccc7d3f2bb 100644 --- a/app/finders/contributed_projects_finder.rb +++ b/app/finders/contributed_projects_finder.rb @@ -11,12 +11,15 @@ class ContributedProjectsFinder < UnionFinder # current_user - When given the list of the projects is limited to those only # visible by this user. # + # ignore_visibility - When true the list of projects will include all contributed + # projects, regardless of their visibility to the current_user + # # Returns an ActiveRecord::Relation. - def execute(current_user = nil) + def execute(current_user = nil, ignore_visibility: false) # Do not show contributed projects if the user profile is private. return Project.none unless can_read_profile?(current_user) - segments = all_projects(current_user) + segments = all_projects(current_user, ignore_visibility) find_union(segments, Project).with_namespace.order_id_desc end @@ -27,7 +30,9 @@ class ContributedProjectsFinder < UnionFinder Ability.allowed?(current_user, :read_user_profile, @user) end - def all_projects(current_user) + def all_projects(current_user, ignore_visibility) + return [@user.contributed_projects] if ignore_visibility + projects = [] projects << @user.contributed_projects.visible_to_user(current_user) if current_user diff --git a/app/finders/groups/user_groups_finder.rb b/app/finders/groups/user_groups_finder.rb index f4aed413867..90367638dcf 100644 --- a/app/finders/groups/user_groups_finder.rb +++ b/app/finders/groups/user_groups_finder.rb @@ -35,7 +35,7 @@ module Groups attr_reader :current_user, :target_user, :params def sort(items) - items.order(path: :asc, id: :asc) # rubocop: disable CodeReuse/ActiveRecord + items.order(Group.arel_table[:path].asc, Group.arel_table[:id].asc) # rubocop: disable CodeReuse/ActiveRecord end def by_search(items) @@ -47,6 +47,8 @@ module Groups def by_permission_scope if permission_scope_create_projects? target_user.manageable_groups(include_groups_with_developer_maintainer_access: true) + elsif permission_scope_transfer_projects? + target_user.manageable_groups(include_groups_with_developer_maintainer_access: false) else target_user.groups end @@ -55,5 +57,9 @@ module Groups def permission_scope_create_projects? params[:permission_scope] == :create_projects end + + def permission_scope_transfer_projects? + params[:permission_scope] == :transfer_projects + end end end diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index 6bbbc237e62..8ecf0c158e0 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -316,7 +316,12 @@ class IssuableFinder # rubocop: disable CodeReuse/ActiveRecord def by_project(items) - if params.project? || params.projects + # When finding issues for multiple projects it's more efficient + # to use a JOIN instead of running a sub-query + # See https://gitlab.com/gitlab-org/gitlab/-/commit/8591cc02be6b12ed60f763a5e0147f2cbbca99e1 + if params.projects.is_a?(ActiveRecord::Relation) + items.merge(params.projects.reorder(nil)).join_project + elsif params.projects items.of_projects(params.projects).references_project else items.none @@ -431,7 +436,7 @@ class IssuableFinder elsif not_params.filter_by_started_milestone? items.joins(:milestone).merge(Milestone.not_started) else - items.without_particular_milestone(not_params[:milestone_title]) + items.without_particular_milestones(not_params[:milestone_title]) end end # rubocop: enable CodeReuse/ActiveRecord diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index f6db150c5d8..6b8dcd61d29 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -16,6 +16,7 @@ # visibility_level: int # tag: string[] - deprecated, use 'topic' instead # topic: string[] +# topic_id: int # personal: boolean # search: string # search_namespaces: boolean @@ -81,6 +82,7 @@ class ProjectsFinder < UnionFinder collection = by_trending(collection) collection = by_visibility_level(collection) collection = by_topics(collection) + collection = by_topic_id(collection) collection = by_search(collection) collection = by_archived(collection) collection = by_custom_attributes(collection) @@ -186,12 +188,21 @@ class ProjectsFinder < UnionFinder topics = params[:topic].instance_of?(String) ? params[:topic].split(',') : params[:topic] topics.map(&:strip).uniq.reject(&:empty?).each do |topic| - items = items.with_topic(topic) + items = items.with_topic_by_name(topic) end items end + def by_topic_id(items) + return items unless params[:topic_id].present? + + topic = Projects::Topic.find_by(id: params[:topic_id]) # rubocop: disable CodeReuse/ActiveRecord + return Project.none unless topic + + items.with_topic(topic) + end + def by_search(items) params[:search] ||= params[:name] diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb index b1e12721712..bf20a2c2c3d 100644 --- a/app/finders/snippets_finder.rb +++ b/app/finders/snippets_finder.rb @@ -41,6 +41,7 @@ class SnippetsFinder < UnionFinder include FinderMethods include Gitlab::Utils::StrongMemoize + include CreatedAtFilter attr_reader :current_user, :params @@ -69,6 +70,7 @@ class SnippetsFinder < UnionFinder items = init_collection items = by_ids(items) items = items.with_optional_visibility(visibility_from_scope) + items = by_created_at(items) items.order_by(sort_param) end diff --git a/app/finders/user_recent_events_finder.rb b/app/finders/user_recent_events_finder.rb index 0f7bf893bb2..3e06dbb2e2c 100644 --- a/app/finders/user_recent_events_finder.rb +++ b/app/finders/user_recent_events_finder.rb @@ -47,24 +47,6 @@ class UserRecentEventsFinder end # rubocop: disable CodeReuse/ActiveRecord - def execute_optimized_multi(users) - Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder.new( - scope: Event.reorder(id: :desc), - array_scope: User.select(:id).where(id: users), - # Event model has a default scope { reorder(nil) } - # When a relation is rordered and used as a target when merging scope, - # its order takes a precedence and _overwrites_ the original scope's order. - # Thus we have to explicitly provide `reorder` for array_mapping_scope here. - array_mapping_scope: -> (author_id_expression) { Event.where(Event.arel_table[:author_id].eq(author_id_expression)).reorder(id: :desc) }, - finder_query: -> (id_expression) { Event.where(Event.arel_table[:id].eq(id_expression)) } - ) - .execute - .limit(limit) - .offset(params[:offset] || 0) - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord def execute_multi users = [] @target_user.each do |user| @@ -73,26 +55,18 @@ class UserRecentEventsFinder return Event.none if users.empty? - if Feature.enabled?(:optimized_followed_users_queries, current_user) - array_data = { - scope_ids: users, - scope_model: User, - mapping_column: :author_id - } - query_builder_params = event_filter.in_operator_query_builder_params(array_data) - - Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder - .new(**query_builder_params) - .execute - .limit(limit) - .offset(params[:offset] || 0) - else - if event_filter.filter == EventFilter::ALL - execute_optimized_multi(users) - else - event_filter.apply_filter(Event.where(author: users).limit_recent(limit, params[:offset] || 0)) - end - end + array_data = { + scope_ids: users, + scope_model: User, + mapping_column: :author_id + } + query_builder_params = event_filter.in_operator_query_builder_params(array_data) + + Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder + .new(**query_builder_params) + .execute + .limit(limit) + .offset(params[:offset] || 0) end # rubocop: enable CodeReuse/ActiveRecord |