summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/bulk_imports/entities_finder.rb35
-rw-r--r--app/finders/bulk_imports/imports_finder.rb24
-rw-r--r--app/finders/ci/pipelines_finder.rb11
-rw-r--r--app/finders/ci/pipelines_for_merge_request_finder.rb3
-rw-r--r--app/finders/ci/runners_finder.rb7
-rw-r--r--app/finders/concerns/merged_at_filter.rb20
-rw-r--r--app/finders/container_repositories_finder.rb2
-rw-r--r--app/finders/environments/environments_finder.rb9
-rw-r--r--app/finders/members_finder.rb5
-rw-r--r--app/finders/merge_requests_finder.rb7
-rw-r--r--app/finders/milestones_finder.rb15
-rw-r--r--app/finders/packages/helm/package_files_finder.rb11
-rw-r--r--app/finders/projects_finder.rb2
-rw-r--r--app/finders/security/jobs_finder.rb4
-rw-r--r--app/finders/security/security_jobs_finder.rb2
15 files changed, 120 insertions, 37 deletions
diff --git a/app/finders/bulk_imports/entities_finder.rb b/app/finders/bulk_imports/entities_finder.rb
new file mode 100644
index 00000000000..2947d155668
--- /dev/null
+++ b/app/finders/bulk_imports/entities_finder.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+module BulkImports
+ class EntitiesFinder
+ def initialize(user:, bulk_import: nil, status: nil)
+ @user = user
+ @bulk_import = bulk_import
+ @status = status
+ end
+
+ def execute
+ ::BulkImports::Entity
+ .preload(:failures) # rubocop: disable CodeReuse/ActiveRecord
+ .by_user_id(user.id)
+ .then(&method(:filter_by_bulk_import))
+ .then(&method(:filter_by_status))
+ end
+
+ private
+
+ attr_reader :user, :bulk_import, :status
+
+ def filter_by_bulk_import(entities)
+ return entities unless bulk_import
+
+ entities.where(bulk_import_id: bulk_import.id) # rubocop: disable CodeReuse/ActiveRecord
+ end
+
+ def filter_by_status(entities)
+ return entities unless ::BulkImports::Entity.all_human_statuses.include?(status)
+
+ entities.with_status(status)
+ end
+ end
+end
diff --git a/app/finders/bulk_imports/imports_finder.rb b/app/finders/bulk_imports/imports_finder.rb
new file mode 100644
index 00000000000..b554bbfa5e7
--- /dev/null
+++ b/app/finders/bulk_imports/imports_finder.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module BulkImports
+ class ImportsFinder
+ def initialize(user:, status: nil)
+ @user = user
+ @status = status
+ end
+
+ def execute
+ filter_by_status(user.bulk_imports)
+ end
+
+ private
+
+ attr_reader :user, :status
+
+ def filter_by_status(imports)
+ return imports unless BulkImport.all_human_statuses.include?(status)
+
+ imports.with_status(status)
+ end
+ end
+end
diff --git a/app/finders/ci/pipelines_finder.rb b/app/finders/ci/pipelines_finder.rb
index af7b23278a4..d9fe5c23a7e 100644
--- a/app/finders/ci/pipelines_finder.rb
+++ b/app/finders/ci/pipelines_finder.rb
@@ -119,11 +119,12 @@ module Ci
# rubocop: disable CodeReuse/ActiveRecord
def by_username(items)
- if params[:username].present?
- items.joins(:user).where(users: { username: params[:username] })
- else
- items
- end
+ return items unless params[:username].present?
+
+ user_id = User.by_username(params[:username]).pluck_primary_key.first
+ return Ci::Pipeline.none unless user_id
+
+ items.where(user_id: user_id)
end
# rubocop: enable CodeReuse/ActiveRecord
diff --git a/app/finders/ci/pipelines_for_merge_request_finder.rb b/app/finders/ci/pipelines_for_merge_request_finder.rb
index 6c5038128f8..f769da03738 100644
--- a/app/finders/ci/pipelines_for_merge_request_finder.rb
+++ b/app/finders/ci/pipelines_for_merge_request_finder.rb
@@ -47,8 +47,7 @@ module Ci
# rubocop: disable CodeReuse/ActiveRecord
def pipelines_using_cte
- sha_relation = merge_request.all_commits.select(:sha)
- sha_relation = sha_relation.distinct if Feature.enabled?(:use_distinct_in_shas_cte, default_enabled: :yaml)
+ sha_relation = merge_request.all_commits.select(:sha).distinct
cte = Gitlab::SQL::CTE.new(:shas, sha_relation)
diff --git a/app/finders/ci/runners_finder.rb b/app/finders/ci/runners_finder.rb
index 7ad51361efd..d34b3202433 100644
--- a/app/finders/ci/runners_finder.rb
+++ b/app/finders/ci/runners_finder.rb
@@ -19,8 +19,9 @@ module Ci
filter_by_runner_type!
filter_by_tag_list!
sort!
+ request_tag_list!
- @runners.with_tags
+ @runners
rescue Gitlab::Access::AccessDeniedError
Ci::Runner.none
@@ -73,6 +74,10 @@ module Ci
@runners = @runners.order_by(sort_key)
end
+ def request_tag_list!
+ @runners = @runners.with_tags if !@params[:preload].present? || @params.dig(:preload, :tag_name)
+ end
+
def filter_by!(scope_name, available_scopes)
scope = @params[scope_name]
diff --git a/app/finders/concerns/merged_at_filter.rb b/app/finders/concerns/merged_at_filter.rb
index e44354f36d1..581bcca3c25 100644
--- a/app/finders/concerns/merged_at_filter.rb
+++ b/app/finders/concerns/merged_at_filter.rb
@@ -10,7 +10,7 @@ module MergedAtFilter
mr_metrics_scope = mr_metrics_scope.merged_after(merged_after) if merged_after.present?
mr_metrics_scope = mr_metrics_scope.merged_before(merged_before) if merged_before.present?
- join_metrics(items, mr_metrics_scope)
+ items.join_metrics.merge(mr_metrics_scope)
end
def merged_after
@@ -20,22 +20,4 @@ module MergedAtFilter
def merged_before
params[:merged_before]
end
-
- # rubocop: disable CodeReuse/ActiveRecord
- #
- # This join optimizes merged_at queries when the finder is invoked for a project by moving
- # the target_project_id condition from merge_requests table to merge_request_metrics table.
- def join_metrics(items, mr_metrics_scope)
- scope = if project_id = items.where_values_hash["target_project_id"]
- # removing the original merge_requests.target_project_id condition
- items = items.unscope(where: :target_project_id)
- # adding the target_project_id condition to merge_request_metrics
- items.join_metrics(project_id)
- else
- items.join_metrics
- end
-
- scope.merge(mr_metrics_scope)
- end
- # rubocop: enable CodeReuse/ActiveRecord
end
diff --git a/app/finders/container_repositories_finder.rb b/app/finders/container_repositories_finder.rb
index 14e4d6799d8..1f6fa9aa1cc 100644
--- a/app/finders/container_repositories_finder.rb
+++ b/app/finders/container_repositories_finder.rb
@@ -25,8 +25,6 @@ class ContainerRepositoriesFinder
end
def project_repositories
- return unless @subject.container_registry_enabled
-
@subject.container_repositories
end
diff --git a/app/finders/environments/environments_finder.rb b/app/finders/environments/environments_finder.rb
index 190cdb3dec3..46c49f096c6 100644
--- a/app/finders/environments/environments_finder.rb
+++ b/app/finders/environments/environments_finder.rb
@@ -16,6 +16,7 @@ module Environments
environments = project.environments
environments = by_name(environments)
environments = by_search(environments)
+ environments = by_ids(environments)
# Raises InvalidStatesError if params[:states] contains invalid states.
by_states(environments)
@@ -47,6 +48,14 @@ module Environments
end
end
+ def by_ids(environments)
+ if params[:environment_ids].present?
+ environments.for_id(params[:environment_ids])
+ else
+ environments
+ end
+ end
+
def environments_with_states(environments)
# Convert to array of strings
states = Array(params[:states]).map(&:to_s)
diff --git a/app/finders/members_finder.rb b/app/finders/members_finder.rb
index 1ff2ad01b63..ea101cf1dcd 100644
--- a/app/finders/members_finder.rb
+++ b/app/finders/members_finder.rb
@@ -83,7 +83,10 @@ class MembersFinder
union = Gitlab::SQL::Union.new(union_members, remove_duplicates: false) # rubocop: disable Gitlab/Union
sql = distinct_on(union)
- Member.includes(:user).from([Arel.sql("(#{sql}) AS #{Member.table_name}")]) # rubocop: disable CodeReuse/ActiveRecord
+ # enumerate the columns here since we are enumerating them in the union and want to be immune to
+ # column caching issues when adding/removing columns
+ Member.select(*Member.column_names)
+ .includes(:user).from([Arel.sql("(#{sql}) AS #{Member.table_name}")]) # rubocop: disable CodeReuse/ActiveRecord
end
def distinct_on(union)
diff --git a/app/finders/merge_requests_finder.rb b/app/finders/merge_requests_finder.rb
index 19fcd91a5b8..e23fa3f7f68 100644
--- a/app/finders/merge_requests_finder.rb
+++ b/app/finders/merge_requests_finder.rb
@@ -76,6 +76,7 @@ class MergeRequestsFinder < IssuableFinder
def filter_negated_items(items)
items = super(items)
items = by_negated_reviewer(items)
+ items = by_negated_approved_by(items)
by_negated_target_branch(items)
end
@@ -119,6 +120,12 @@ class MergeRequestsFinder < IssuableFinder
end
# rubocop: enable CodeReuse/ActiveRecord
+ def by_negated_approved_by(items)
+ return items unless not_params[:approved_by_usernames]
+
+ items.not_approved_by_users_with_usernames(not_params[:approved_by_usernames])
+ end
+
def source_project_id
@source_project_id ||= params[:source_project_id].presence
end
diff --git a/app/finders/milestones_finder.rb b/app/finders/milestones_finder.rb
index 5d2a54ac979..5fe55e88086 100644
--- a/app/finders/milestones_finder.rb
+++ b/app/finders/milestones_finder.rb
@@ -18,6 +18,8 @@ class MilestonesFinder
attr_reader :params
+ EXPIRED_LAST_SORTS = %i[expired_last_due_date_asc expired_last_due_date_desc].freeze
+
def initialize(params = {})
@params = params
end
@@ -70,7 +72,16 @@ class MilestonesFinder
end
def order(items)
- sort_by = params[:sort].presence || 'due_date_asc'
- items.sort_by_attribute(sort_by)
+ sort_by = params[:sort].presence || :due_date_asc
+
+ if sort_by_expired_last?(sort_by)
+ items.sort_with_expired_last(sort_by)
+ else
+ items.sort_by_attribute(sort_by)
+ end
+ end
+
+ def sort_by_expired_last?(sort_by)
+ EXPIRED_LAST_SORTS.include?(sort_by)
end
end
diff --git a/app/finders/packages/helm/package_files_finder.rb b/app/finders/packages/helm/package_files_finder.rb
index 74f9eaaca82..ba400b27554 100644
--- a/app/finders/packages/helm/package_files_finder.rb
+++ b/app/finders/packages/helm/package_files_finder.rb
@@ -3,6 +3,9 @@
module Packages
module Helm
class PackageFilesFinder
+ DEFAULT_PACKAGE_FILES_COUNT = 20
+ MAX_PACKAGE_FILES_COUNT = 1000
+
def initialize(project, channel, params = {})
@project = project
@channel = channel
@@ -10,12 +13,18 @@ module Packages
end
def execute
- package_files = Packages::PackageFile.for_helm_with_channel(@project, @channel).preload_helm_file_metadata
+ package_files = Packages::PackageFile.for_helm_with_channel(@project, @channel)
+ .limit_recent(limit)
by_file_name(package_files)
end
private
+ def limit
+ limit_param = @params[:limit] || DEFAULT_PACKAGE_FILES_COUNT
+ [limit_param, MAX_PACKAGE_FILES_COUNT].min
+ end
+
def by_file_name(files)
return files unless @params[:file_name]
diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb
index 582075efc4e..dca3d12f3c9 100644
--- a/app/finders/projects_finder.rb
+++ b/app/finders/projects_finder.rb
@@ -55,7 +55,7 @@ class ProjectsFinder < UnionFinder
collection = Project.wrap_with_cte(collection) if use_cte
collection = filter_projects(collection)
- if params[:sort] == 'similarity' && params[:search] && Feature.enabled?(:project_finder_similarity_sort, current_user)
+ if params[:sort] == 'similarity' && params[:search]
collection.sorted_by_similarity_desc(params[:search])
else
sort(collection)
diff --git a/app/finders/security/jobs_finder.rb b/app/finders/security/jobs_finder.rb
index b8649520c20..99bcf97f43c 100644
--- a/app/finders/security/jobs_finder.rb
+++ b/app/finders/security/jobs_finder.rb
@@ -20,7 +20,7 @@ module Security
end
def initialize(pipeline:, job_types: [])
- if self.class == Security::JobsFinder
+ if self.instance_of?(Security::JobsFinder)
raise NotImplementedError, 'This is an abstract class, please instantiate its descendants'
end
@@ -38,7 +38,7 @@ module Security
def execute
return [] if @job_types.empty?
- if Feature.enabled?(:ci_build_metadata_config)
+ if Feature.enabled?(:ci_build_metadata_config, pipeline.project, default_enabled: :yaml)
find_jobs
else
find_jobs_legacy
diff --git a/app/finders/security/security_jobs_finder.rb b/app/finders/security/security_jobs_finder.rb
index 2352e19c7da..008d4e29b13 100644
--- a/app/finders/security/security_jobs_finder.rb
+++ b/app/finders/security/security_jobs_finder.rb
@@ -13,7 +13,7 @@
module Security
class SecurityJobsFinder < JobsFinder
def self.allowed_job_types
- [:sast, :dast, :dependency_scanning, :container_scanning, :secret_detection, :coverage_fuzzing, :api_fuzzing]
+ [:sast, :dast, :dependency_scanning, :container_scanning, :secret_detection, :coverage_fuzzing, :api_fuzzing, :cluster_image_scanning]
end
end
end