diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
commit | 0ea3fcec397b69815975647f5e2aa5fe944a8486 (patch) | |
tree | 7979381b89d26011bcf9bdc989a40fcc2f1ed4ff /app/finders | |
parent | 72123183a20411a36d607d70b12d57c484394c8e (diff) |
Add latest changes from gitlab-org/gitlab@15-1-stable-eev15.1.0-rc42
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/clusters/agents_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/crm/contacts_finder.rb | 39 | ||||
-rw-r--r-- | app/finders/crm/organizations_finder.rb | 76 | ||||
-rw-r--r-- | app/finders/issuable_finder.rb | 4 | ||||
-rw-r--r-- | app/finders/issuable_finder/params.rb | 6 | ||||
-rw-r--r-- | app/finders/issuables/label_filter.rb | 6 | ||||
-rw-r--r-- | app/finders/issues_finder.rb | 14 | ||||
-rw-r--r-- | app/finders/packages/pypi/packages_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/work_items/work_items_finder.rb | 19 |
9 files changed, 153 insertions, 15 deletions
diff --git a/app/finders/clusters/agents_finder.rb b/app/finders/clusters/agents_finder.rb index 136bbf16981..d0b1240157c 100644 --- a/app/finders/clusters/agents_finder.rb +++ b/app/finders/clusters/agents_finder.rb @@ -2,6 +2,8 @@ module Clusters class AgentsFinder + include FinderMethods + def initialize(project, current_user, params: {}) @project = project @current_user = current_user diff --git a/app/finders/crm/contacts_finder.rb b/app/finders/crm/contacts_finder.rb index c2d44bec27b..29f3d6f0f16 100644 --- a/app/finders/crm/contacts_finder.rb +++ b/app/finders/crm/contacts_finder.rb @@ -6,6 +6,9 @@ # current_user - user performing the action. Must have the correct permission level for the group. # params: # group: Group, required +# search: String, optional +# state: CustomerRelations::ContactStateEnum, optional +# ids: int[], optional module Crm class ContactsFinder include Gitlab::Allowable @@ -21,7 +24,11 @@ module Crm def execute return CustomerRelations::Contact.none unless root_group - root_group.contacts + contacts = root_group.contacts + contacts = by_ids(contacts) + contacts = by_state(contacts) + contacts = by_search(contacts) + contacts.sort_by_name end private @@ -35,5 +42,35 @@ module Crm group end end + + def by_search(contacts) + return contacts unless search? + + contacts.search(params[:search]) + end + + def by_state(contacts) + return contacts unless state? + + contacts.search_by_state(params[:state]) + end + + def by_ids(contacts) + return contacts unless ids? + + contacts.id_in(params[:ids]) + end + + def search? + params[:search].present? + end + + def state? + params[:state].present? + end + + def ids? + params[:ids].present? + end end end diff --git a/app/finders/crm/organizations_finder.rb b/app/finders/crm/organizations_finder.rb new file mode 100644 index 00000000000..5a8ab148ef3 --- /dev/null +++ b/app/finders/crm/organizations_finder.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +# Finder for retrieving organizations scoped to a group +# +# Arguments: +# current_user - user performing the action. Must have the correct permission level for the group. +# params: +# group: Group, required +# search: String, optional +# state: CustomerRelations::OrganizationStateEnum, optional +# ids: int[], optional +module Crm + class OrganizationsFinder + include Gitlab::Allowable + include Gitlab::Utils::StrongMemoize + + attr_reader :params, :current_user + + def initialize(current_user, params = {}) + @current_user = current_user + @params = params + end + + def execute + return CustomerRelations::Organization.none unless root_group + + organizations = root_group.organizations + organizations = by_ids(organizations) + organizations = by_search(organizations) + organizations = by_state(organizations) + organizations.sort_by_name + end + + private + + def root_group + strong_memoize(:root_group) do + group = params[:group]&.root_ancestor + + next unless can?(@current_user, :read_crm_organization, group) + + group + end + end + + def by_search(organizations) + return organizations unless search? + + organizations.search(params[:search]) + end + + def by_state(organizations) + return organizations unless state? + + organizations.search_by_state(params[:state]) + end + + def by_ids(organizations) + return organizations unless ids? + + organizations.id_in(params[:ids]) + end + + def search? + params[:search].present? + end + + def state? + params[:state].present? + end + + def ids? + params[:ids].present? + end + end +end diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index fe07a52cbf0..6bbbc237e62 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -316,10 +316,8 @@ class IssuableFinder # rubocop: disable CodeReuse/ActiveRecord def by_project(items) - if params.project? + if params.project? || params.projects items.of_projects(params.projects).references_project - elsif params.projects - items.merge(params.projects.reorder(nil)).join_project else items.none end diff --git a/app/finders/issuable_finder/params.rb b/app/finders/issuable_finder/params.rb index 359a56bd39b..32d50802537 100644 --- a/app/finders/issuable_finder/params.rb +++ b/app/finders/issuable_finder/params.rb @@ -142,7 +142,7 @@ class IssuableFinder projects_public_or_visible_to_user end - projects.with_feature_available_for_user(klass, current_user).reorder(nil) # rubocop: disable CodeReuse/ActiveRecord + projects.with_feature_available_for_user(klass.base_class, current_user).reorder(nil) # rubocop: disable CodeReuse/ActiveRecord end end @@ -175,7 +175,7 @@ class IssuableFinder return Project.none unless group if params[:include_subgroups] - Project.where(namespace_id: group.self_and_descendants) # rubocop: disable CodeReuse/ActiveRecord + Project.where(namespace_id: group.self_and_descendant_ids) # rubocop: disable CodeReuse/ActiveRecord else group.projects end @@ -215,7 +215,7 @@ class IssuableFinder end def min_access_level - ProjectFeature.required_minimum_access_level(klass) + ProjectFeature.required_minimum_access_level(klass.base_class) end def method_missing(method_name, *args, &block) diff --git a/app/finders/issuables/label_filter.rb b/app/finders/issuables/label_filter.rb index 9a6ca107b19..4e9c964e51c 100644 --- a/app/finders/issuables/label_filter.rb +++ b/app/finders/issuables/label_filter.rb @@ -27,7 +27,7 @@ module Issuables def by_label(issuables) return issuables unless label_names_from_params.present? - target_model = issuables.model + target_model = issuables.base_class if filter_by_no_label? issuables.where(label_link_query(target_model).arel.exists.not) @@ -55,7 +55,7 @@ module Issuables # rubocop: disable CodeReuse/ActiveRecord def issuables_with_selected_labels(issuables, label_names) - target_model = issuables.model + target_model = issuables.base_class if root_namespace all_label_ids = find_label_ids(label_names) @@ -77,7 +77,7 @@ module Issuables # rubocop: disable CodeReuse/ActiveRecord def issuables_without_selected_labels(issuables, label_names) - target_model = issuables.model + target_model = issuables.base_class if root_namespace label_ids = find_label_ids(label_names).flatten(1) diff --git a/app/finders/issues_finder.rb b/app/finders/issues_finder.rb index 7929c36906d..663dda73a6a 100644 --- a/app/finders/issues_finder.rb +++ b/app/finders/issues_finder.rb @@ -37,7 +37,7 @@ class IssuesFinder < IssuableFinder # rubocop: disable CodeReuse/ActiveRecord def klass - Issue.includes(:author) + model_class.includes(:author) end # rubocop: enable CodeReuse/ActiveRecord @@ -47,10 +47,10 @@ class IssuesFinder < IssuableFinder # rubocop: disable CodeReuse/ActiveRecord def with_confidentiality_access_check - return Issue.all if params.user_can_see_all_issues? + return model_class.all if params.user_can_see_all_issues? # Only admins can see hidden issues, so for non-admins, we filter out any hidden issues - issues = Issue.without_hidden + issues = model_class.without_hidden return issues.all if params.user_can_see_all_confidential_issues? @@ -77,7 +77,7 @@ class IssuesFinder < IssuableFinder def init_collection if params.public_only? - Issue.public_only + model_class.public_only else with_confidentiality_access_check end @@ -129,7 +129,7 @@ class IssuesFinder < IssuableFinder def by_issue_types(items) issue_type_params = Array(params[:issue_types]).map(&:to_s) return items if issue_type_params.blank? - return Issue.none unless (WorkItems::Type.base_types.keys & issue_type_params).sort == issue_type_params.sort + return model_class.none unless (WorkItems::Type.base_types.keys & issue_type_params).sort == issue_type_params.sort items.with_issue_type(params[:issue_types]) end @@ -140,6 +140,10 @@ class IssuesFinder < IssuableFinder items.without_issue_type(issue_type_params) end + + def model_class + Issue + end end IssuesFinder.prepend_mod_with('IssuesFinder') diff --git a/app/finders/packages/pypi/packages_finder.rb b/app/finders/packages/pypi/packages_finder.rb index 47cfb59944b..17138134eb3 100644 --- a/app/finders/packages/pypi/packages_finder.rb +++ b/app/finders/packages/pypi/packages_finder.rb @@ -4,6 +4,8 @@ module Packages module Pypi class PackagesFinder < ::Packages::GroupOrProjectPackageFinder def execute + return packages unless @params[:package_name] + packages.with_normalized_pypi_name(@params[:package_name]) end diff --git a/app/finders/work_items/work_items_finder.rb b/app/finders/work_items/work_items_finder.rb new file mode 100644 index 00000000000..960272fe47e --- /dev/null +++ b/app/finders/work_items/work_items_finder.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# WorkItem model inherits from Issue model. It's planned to be its extension +# with widgets support. Because WorkItems are internally Issues, WorkItemsFinder +# can be almost identical to IssuesFinder, except it should return instances of +# WorkItems instead of Issues +module WorkItems + class WorkItemsFinder < IssuesFinder + def params_class + ::IssuesFinder::Params + end + + private + + def model_class + WorkItem + end + end +end |