diff options
Diffstat (limited to 'app/finders')
21 files changed, 142 insertions, 93 deletions
diff --git a/app/finders/autocomplete/routes_finder.rb b/app/finders/autocomplete/routes_finder.rb index 858a4b69376..ecede0c1c1c 100644 --- a/app/finders/autocomplete/routes_finder.rb +++ b/app/finders/autocomplete/routes_finder.rb @@ -13,7 +13,7 @@ module Autocomplete end def execute - return [] if @search.blank? + return Route.none if @search.blank? Route .for_routable(routables) @@ -30,7 +30,7 @@ module Autocomplete class NamespacesOnly < self def routables - return Namespace.without_project_namespaces if current_user.admin? + return Namespace.without_project_namespaces if current_user.can_admin_all_resources? current_user.namespaces end @@ -38,7 +38,7 @@ module Autocomplete class ProjectsOnly < self def routables - return Project.all if current_user.admin? + return Project.all if current_user.can_admin_all_resources? current_user.projects end diff --git a/app/finders/ci/freeze_periods_finder.rb b/app/finders/ci/freeze_periods_finder.rb new file mode 100644 index 00000000000..91df776abe6 --- /dev/null +++ b/app/finders/ci/freeze_periods_finder.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Ci + class FreezePeriodsFinder + def initialize(project, current_user = nil) + @project = project + @current_user = current_user + end + + def execute + return Ci::FreezePeriod.none unless Ability.allowed?(@current_user, :read_freeze_period, @project) + + @project.freeze_periods + end + end +end diff --git a/app/finders/ci/jobs_finder.rb b/app/finders/ci/jobs_finder.rb index 152eb271694..1627e41a02d 100644 --- a/app/finders/ci/jobs_finder.rb +++ b/app/finders/ci/jobs_finder.rb @@ -16,6 +16,7 @@ module Ci def execute builds = init_collection.order_id_desc + builds = filter_by_with_artifacts(builds) filter_by_scope(builds) rescue Gitlab::Access::AccessDeniedError type.none @@ -30,7 +31,7 @@ module Ci end def all_jobs - raise Gitlab::Access::AccessDeniedError unless current_user&.admin? + raise Gitlab::Access::AccessDeniedError unless current_user&.can_admin_all_resources? type.all end @@ -72,6 +73,14 @@ module Ci end end + def filter_by_with_artifacts(builds) + if params[:with_artifacts] + builds.with_erasable_artifacts + else + builds + end + end + def filter_by_statuses!(builds) unknown_statuses = params[:scope] - ::CommitStatus::AVAILABLE_STATUSES raise ArgumentError, 'Scope contains invalid value(s)' unless unknown_statuses.empty? diff --git a/app/finders/ci/pipelines_finder.rb b/app/finders/ci/pipelines_finder.rb index 712d5f8c6fb..4c47517299a 100644 --- a/app/finders/ci/pipelines_finder.rb +++ b/app/finders/ci/pipelines_finder.rb @@ -36,6 +36,7 @@ module Ci items = by_yaml_errors(items) items = by_updated_at(items) items = by_source(items) + items = by_name(items) sort_items(items) end @@ -152,6 +153,15 @@ module Ci items end + def by_name(items) + return items unless + Feature.enabled?(:pipeline_name, project) && + Feature.enabled?(:pipeline_name_search, project) && + params[:name].present? + + items.for_name(params[:name]) + end + # rubocop: disable CodeReuse/ActiveRecord def sort_items(items) order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) diff --git a/app/finders/ci/runners_finder.rb b/app/finders/ci/runners_finder.rb index d0d98a59677..136d23939e2 100644 --- a/app/finders/ci/runners_finder.rb +++ b/app/finders/ci/runners_finder.rb @@ -10,6 +10,7 @@ module Ci def initialize(current_user:, params:) @params = params @group = params.delete(:group) + @project = params.delete(:project) @current_user = current_user end @@ -36,13 +37,19 @@ module Ci private def search! - @group ? group_runners : all_runners + if @project && Feature.enabled?(:on_demand_scans_runner_tags, @project) + project_runners + elsif @group + group_runners + else + all_runners + end @runners = @runners.search(@params[:search]) if @params[:search].present? end def all_runners - raise Gitlab::Access::AccessDeniedError unless @current_user&.admin? + raise Gitlab::Access::AccessDeniedError unless @current_user&.can_admin_all_resources? @runners = Ci::Runner.all end @@ -66,6 +73,12 @@ module Ci end end + def project_runners + raise Gitlab::Access::AccessDeniedError unless can?(@current_user, :admin_project, @project) + + @runners = ::Ci::Runner.owned_or_instance_wide(@project.id) + end + def filter_by_active! @runners = @runners.active(@params[:active]) if @params.include?(:active) end diff --git a/app/finders/clusters/agent_tokens_finder.rb b/app/finders/clusters/agent_tokens_finder.rb index e241836e1dc..72692777bc6 100644 --- a/app/finders/clusters/agent_tokens_finder.rb +++ b/app/finders/clusters/agent_tokens_finder.rb @@ -2,24 +2,30 @@ module Clusters class AgentTokensFinder - def initialize(object, current_user, agent_id) - @object = object + include FinderMethods + + def initialize(agent, current_user, params = {}) + @agent = agent @current_user = current_user - @agent_id = agent_id + @params = params end def execute - raise_not_found_unless_can_read_cluster + return ::Clusters::AgentToken.none unless can_read_cluster_agents? - object.cluster_agents.find(agent_id).agent_tokens + agent.agent_tokens.then { |agent_tokens| by_status(agent_tokens) } end private - attr_reader :object, :current_user, :agent_id + attr_reader :agent, :current_user, :params + + def by_status(agent_tokens) + params[:status].present? ? agent_tokens.with_status(params[:status]) : agent_tokens + end - def raise_not_found_unless_can_read_cluster - raise ActiveRecord::RecordNotFound unless current_user&.can?(:read_cluster, object) + def can_read_cluster_agents? + current_user&.can?(:read_cluster, agent&.project) end end end diff --git a/app/finders/deployments_finder.rb b/app/finders/deployments_finder.rb index 5b2139cb941..21869f6f31d 100644 --- a/app/finders/deployments_finder.rb +++ b/app/finders/deployments_finder.rb @@ -212,6 +212,7 @@ class DeploymentsFinder deployable: { job_artifacts: [], user: [], + metadata: [], pipeline: { project: { route: [], diff --git a/app/finders/environments/environments_finder.rb b/app/finders/environments/environments_finder.rb index f2dcba04349..85cd37c267e 100644 --- a/app/finders/environments/environments_finder.rb +++ b/app/finders/environments/environments_finder.rb @@ -41,7 +41,13 @@ module Environments def by_search(environments) if params[:search].present? - environments.for_name_like(params[:search], limit: nil) + if Feature.enabled?(:enable_environments_search_within_folder, project) + Environment.from_union( + environments.for_name_like(params[:search], limit: nil), + environments.for_name_like_within_folder(params[:search], limit: nil)) + else + environments.for_name_like(params[:search], limit: nil) + end else environments end @@ -57,7 +63,7 @@ module Environments def by_ids(environments) if params[:environment_ids].present? - environments.for_id(params[:environment_ids]) + environments.id_in(params[:environment_ids]) else environments end diff --git a/app/finders/freeze_periods_finder.rb b/app/finders/freeze_periods_finder.rb deleted file mode 100644 index 2a9bfbe12ba..00000000000 --- a/app/finders/freeze_periods_finder.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class FreezePeriodsFinder - def initialize(project, current_user = nil) - @project = project - @current_user = current_user - end - - def execute - return Ci::FreezePeriod.none unless Ability.allowed?(@current_user, :read_freeze_period, @project) - - @project.freeze_periods - end -end diff --git a/app/finders/git_refs_finder.rb b/app/finders/git_refs_finder.rb index dbe0060d8ae..0492dd9934f 100644 --- a/app/finders/git_refs_finder.rb +++ b/app/finders/git_refs_finder.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class GitRefsFinder + include Gitlab::Utils::StrongMemoize + def initialize(repository, params = {}) @repository = repository @params = params @@ -10,44 +12,28 @@ class GitRefsFinder attr_reader :repository, :params - def search - @params[:search].to_s.presence - end - - def sort - @params[:sort].to_s.presence || 'name' - end - def by_search(refs) return refs unless search - case search - when ->(v) { v.starts_with?('^') } - filter_refs_with_prefix(refs, search.slice(1..-1)) - when ->(v) { v.ends_with?('$') } - filter_refs_with_suffix(refs, search.chop) - else - matches = filter_refs_by_name(refs, search) - set_exact_match_as_first_result(matches, search) - end - end - - def filter_refs_with_prefix(refs, prefix) - prefix = prefix.downcase + matches = filter_refs(refs, search) + return matches if regex_search? - refs.select { |ref| ref.name.downcase.starts_with?(prefix) } + set_exact_match_as_first_result(matches, search) end - def filter_refs_with_suffix(refs, suffix) - suffix = suffix.downcase - - refs.select { |ref| ref.name.downcase.ends_with?(suffix) } + def search + @params[:search].to_s.presence end + strong_memoize_attr :search - def filter_refs_by_name(refs, term) - term = term.downcase + def sort + @params[:sort].to_s.presence || 'name' + end - refs.select { |ref| ref.name.downcase.include?(term) } + def filter_refs(refs, term) + regex_string = Regexp.quote(term.downcase) + regex_string = unescape_regex_operators(regex_string) if regex_search? + refs.select { |ref| /#{regex_string}/ === ref.name.downcase } end def set_exact_match_as_first_result(matches, term) @@ -59,4 +45,13 @@ class GitRefsFinder def find_exact_match_index(matches, term) matches.index { |ref| ref.name.casecmp(term) == 0 } end + + def regex_search? + Regexp.union('^', '$', '*') === search + end + strong_memoize_attr :regex_search?, :regex_search + + def unescape_regex_operators(regex_string) + regex_string.sub('\^', '^').gsub('\*', '.*?').sub('\$', '$') + end end diff --git a/app/finders/group_descendants_finder.rb b/app/finders/group_descendants_finder.rb index 42cd06c8066..033af0f42a6 100644 --- a/app/finders/group_descendants_finder.rb +++ b/app/finders/group_descendants_finder.rb @@ -22,7 +22,7 @@ class GroupDescendantsFinder attr_reader :current_user, :parent_group, :params - def initialize(current_user: nil, parent_group:, params: {}) + def initialize(parent_group:, current_user: nil, params: {}) @current_user = current_user @parent_group = parent_group @params = params.reverse_merge(non_archived: params[:archived].blank?, not_aimed_for_deletion: true) diff --git a/app/finders/group_members_finder.rb b/app/finders/group_members_finder.rb index 4688d561897..47ed623b252 100644 --- a/app/finders/group_members_finder.rb +++ b/app/finders/group_members_finder.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true class GroupMembersFinder < UnionFinder - RELATIONS = %i(direct inherited descendants shared_from_groups).freeze - DEFAULT_RELATIONS = %i(direct inherited).freeze + RELATIONS = %i[direct inherited descendants shared_from_groups].freeze + DEFAULT_RELATIONS = %i[direct inherited].freeze INVALID_RELATION_TYPE_ERROR_MSG = "is not a valid relation type. Valid relation types are #{RELATIONS.join(', ')}." RELATIONS_DESCRIPTIONS = { diff --git a/app/finders/members_finder.rb b/app/finders/members_finder.rb index e68a0c8fca9..de6eacbb1e0 100644 --- a/app/finders/members_finder.rb +++ b/app/finders/members_finder.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true class MembersFinder - RELATIONS = %i(direct inherited descendants invited_groups).freeze - DEFAULT_RELATIONS = %i(direct inherited).freeze + RELATIONS = %i[direct inherited descendants invited_groups].freeze + DEFAULT_RELATIONS = %i[direct inherited].freeze # Params can be any of the following: # sort: string diff --git a/app/finders/merge_request_target_project_finder.rb b/app/finders/merge_request_target_project_finder.rb index dc9b28ab0a0..fdb3bac8935 100644 --- a/app/finders/merge_request_target_project_finder.rb +++ b/app/finders/merge_request_target_project_finder.rb @@ -5,7 +5,7 @@ class MergeRequestTargetProjectFinder attr_reader :current_user, :source_project - def initialize(current_user: nil, source_project:, project_feature: :merge_requests) + def initialize(source_project:, current_user: nil, project_feature: :merge_requests) @current_user = current_user @source_project = source_project @project_feature = project_feature diff --git a/app/finders/notes_finder.rb b/app/finders/notes_finder.rb index 42bd7a24888..7890502cf0e 100644 --- a/app/finders/notes_finder.rb +++ b/app/finders/notes_finder.rb @@ -65,7 +65,7 @@ class NotesFinder @target = if target_type == "commit" - if Ability.allowed?(@current_user, :download_code, @project) + if Ability.allowed?(@current_user, :read_code, @project) @project.commit(target_id) end else @@ -101,7 +101,7 @@ class NotesFinder # rubocop: disable CodeReuse/ActiveRecord def notes_of_any_type - types = %w(commit issue merge_request snippet) + types = %w[commit issue merge_request snippet] note_relations = types.map { |t| notes_for_type(t) } note_relations.map! { |notes| search(notes) } UnionFinder.new.find_union(note_relations, Note.includes(:author)) # rubocop: disable CodeReuse/Finder @@ -126,7 +126,7 @@ class NotesFinder # rubocop: disable CodeReuse/ActiveRecord def notes_for_type(noteable_type) if noteable_type == "commit" - if Ability.allowed?(@current_user, :download_code, @project) + if Ability.allowed?(@current_user, :read_code, @project) @project.notes.where(noteable_type: 'Commit') else Note.none diff --git a/app/finders/personal_access_tokens_finder.rb b/app/finders/personal_access_tokens_finder.rb index 8403c531945..5af08cf0660 100644 --- a/app/finders/personal_access_tokens_finder.rb +++ b/app/finders/personal_access_tokens_finder.rb @@ -33,7 +33,7 @@ class PersonalAccessTokensFinder attr_reader :current_user def by_current_user(tokens) - return tokens if current_user.nil? || current_user.admin? + return tokens if current_user.nil? || current_user.can_admin_all_resources? return PersonalAccessToken.none unless Ability.allowed?(current_user, :read_user_personal_access_tokens, params[:user]) tokens diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index 126687ae41f..1afd5adeada 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -89,6 +89,7 @@ class ProjectsFinder < UnionFinder collection = by_not_aimed_for_deletion(collection) collection = by_last_activity_after(collection) collection = by_last_activity_before(collection) + collection = by_language(collection) by_repository_storage(collection) end @@ -97,12 +98,10 @@ class ProjectsFinder < UnionFinder current_user.owned_projects elsif min_access_level? current_user.authorized_projects(params[:min_access_level]) + elsif private_only? || impossible_visibility_level? + current_user.authorized_projects else - if private_only? || impossible_visibility_level? - current_user.authorized_projects - else - Project.public_or_visible_to_user(current_user) - end + Project.public_or_visible_to_user(current_user) end end @@ -239,6 +238,14 @@ class ProjectsFinder < UnionFinder end end + def by_language(items) + if Feature.enabled?(:project_language_search, current_user) && params[:language].present? + items.with_programming_language_id(params[:language]) + else + items + end + end + def sort(items) if params[:sort].present? items.sort_by_attribute(params[:sort]) diff --git a/app/finders/releases/group_releases_finder.rb b/app/finders/releases/group_releases_finder.rb index 08530f63ea6..67784d6579c 100644 --- a/app/finders/releases/group_releases_finder.rb +++ b/app/finders/releases/group_releases_finder.rb @@ -33,8 +33,8 @@ module Releases Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder.new( scope: releases_scope, array_scope: Project.for_group_and_its_subgroups(parent).select(:id), - array_mapping_scope: -> (project_id_expression) { Release.where(Release.arel_table[:project_id].eq(project_id_expression)) }, - finder_query: -> (order_by, id_expression) { Release.where(Release.arel_table[:id].eq(id_expression)) } + array_mapping_scope: ->(project_id_expression) { Release.where(Release.arel_table[:project_id].eq(project_id_expression)) }, + finder_query: ->(order_by, id_expression) { Release.where(Release.arel_table[:id].eq(id_expression)) } ) .execute end diff --git a/app/finders/repositories/tree_finder.rb b/app/finders/repositories/tree_finder.rb index 2ea5a8856ec..231c1de1513 100644 --- a/app/finders/repositories/tree_finder.rb +++ b/app/finders/repositories/tree_finder.rb @@ -1,15 +1,13 @@ # frozen_string_literal: true module Repositories - class TreeFinder < GitRefsFinder - attr_reader :user_project - + class TreeFinder CommitMissingError = Class.new(StandardError) - def initialize(user_project, params = {}) - super(user_project.repository, params) - - @user_project = user_project + def initialize(project, params = {}) + @project = project + @repository = project.repository + @params = params end def execute(gitaly_pagination: false) @@ -17,15 +15,15 @@ module Repositories request_params = { recursive: recursive } request_params[:pagination_params] = pagination_params if gitaly_pagination - tree = user_project.repository.tree(commit.id, path, **request_params) - tree.sorted_entries + repository.tree(commit.id, path, **request_params).sorted_entries end def total # This is inefficient and we'll look at replacing this implementation - Gitlab::Cache.fetch_once([user_project, repository.commit, :tree_size, commit.id, path, recursive]) do - user_project.repository.tree(commit.id, path, recursive: recursive).entries.size + cache_key = [project, repository.commit, :tree_size, commit.id, path, recursive] + Gitlab::Cache.fetch_once(cache_key) do + repository.tree(commit.id, path, recursive: recursive).entries.size end end @@ -35,12 +33,14 @@ module Repositories private + attr_reader :project, :repository, :params + def commit - @commit ||= user_project.commit(ref) + @commit ||= project.commit(ref) end def ref - params[:ref] || user_project.default_branch + params[:ref] || project.default_branch end def path diff --git a/app/finders/todos_finder.rb b/app/finders/todos_finder.rb index e83018ed24c..0bf31ea33dd 100644 --- a/app/finders/todos_finder.rb +++ b/app/finders/todos_finder.rb @@ -24,7 +24,7 @@ class TodosFinder NONE = '0' - TODO_TYPES = Set.new(%w(Issue MergeRequest DesignManagement::Design AlertManagement::Alert)).freeze + TODO_TYPES = Set.new(%w[Issue WorkItem MergeRequest DesignManagement::Design AlertManagement::Alert]).freeze attr_accessor :current_user, :params diff --git a/app/finders/users_finder.rb b/app/finders/users_finder.rb index 9c2462b42a6..11e3c341c1f 100644 --- a/app/finders/users_finder.rb +++ b/app/finders/users_finder.rb @@ -55,7 +55,7 @@ class UsersFinder private def base_scope - scope = current_user&.admin? ? User.all : User.without_forbidden_states + scope = current_user&.can_admin_all_resources? ? User.all : User.without_forbidden_states scope.order_id_desc end @@ -80,7 +80,7 @@ class UsersFinder def by_search(users) return users unless params[:search].present? - users.search(params[:search], with_private_emails: current_user&.admin?) + users.search(params[:search], with_private_emails: current_user&.can_admin_all_resources?) end def by_blocked(users) @@ -97,7 +97,7 @@ class UsersFinder # rubocop: disable CodeReuse/ActiveRecord def by_external_identity(users) - return users unless current_user&.admin? && params[:extern_uid] && params[:provider] + return users unless current_user&.can_admin_all_resources? && params[:extern_uid] && params[:provider] users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid])) end |