diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
commit | 585826cb22ecea5998a2c2a4675735c94bdeedac (patch) | |
tree | 5b05f0b30d33cef48963609e8a18a4dff260eab3 /app/finders | |
parent | df221d036e5d0c6c0ee4d55b9c97f481ee05dee8 (diff) |
Add latest changes from gitlab-org/gitlab@16-6-stable-eev16.6.0-rc42
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/ci/catalog/resources/versions_finder.rb | 58 | ||||
-rw-r--r-- | app/finders/ci/runners_finder.rb | 17 | ||||
-rw-r--r-- | app/finders/data_transfer/mocked_transfer_finder.rb | 27 | ||||
-rw-r--r-- | app/finders/merge_requests_finder.rb | 11 | ||||
-rw-r--r-- | app/finders/organizations/user_organizations_finder.rb | 26 | ||||
-rw-r--r-- | app/finders/packages/packages_finder.rb | 1 | ||||
-rw-r--r-- | app/finders/packages/pypi/packages_finder.rb | 11 | ||||
-rw-r--r-- | app/finders/projects/ml/model_finder.rb | 52 | ||||
-rw-r--r-- | app/finders/projects_finder.rb | 14 | ||||
-rw-r--r-- | app/finders/user_group_notification_settings_finder.rb | 17 |
10 files changed, 193 insertions, 41 deletions
diff --git a/app/finders/ci/catalog/resources/versions_finder.rb b/app/finders/ci/catalog/resources/versions_finder.rb new file mode 100644 index 00000000000..b37d4f0377a --- /dev/null +++ b/app/finders/ci/catalog/resources/versions_finder.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module Ci + module Catalog + module Resources + class VersionsFinder + include Gitlab::Utils::StrongMemoize + + def initialize(catalog_resources, current_user, params = {}) + # The catalog resources should already have their project association preloaded + @catalog_resources = Array.wrap(catalog_resources) + @current_user = current_user + @params = params + end + + def execute + return Ci::Catalog::Resources::Version.none if authorized_catalog_resources.empty? + + versions = params[:latest] ? get_latest_versions : get_versions + versions = versions.preloaded + sort(versions) + end + + private + + DEFAULT_SORT = :released_at_desc + + attr_reader :catalog_resources, :current_user, :params + + def get_versions + Ci::Catalog::Resources::Version.for_catalog_resources(authorized_catalog_resources) + end + + def get_latest_versions + Ci::Catalog::Resources::Version.latest_for_catalog_resources(authorized_catalog_resources) + end + + def authorized_catalog_resources + # Preload project authorizations to avoid N+1 queries + projects = catalog_resources.map(&:project) + ActiveRecord::Associations::Preloader.new(records: projects, associations: :project_feature).call + Preloaders::UserMaxAccessLevelInProjectsPreloader.new(projects, current_user).execute + + catalog_resources.select { |resource| authorized?(resource.project) } + end + strong_memoize_attr :authorized_catalog_resources + + def sort(versions) + versions.order_by(params[:sort] || DEFAULT_SORT) + end + + def authorized?(project) + Ability.allowed?(current_user, :read_release, project) + end + end + end + end +end diff --git a/app/finders/ci/runners_finder.rb b/app/finders/ci/runners_finder.rb index 331f732bff7..945d332ff47 100644 --- a/app/finders/ci/runners_finder.rb +++ b/app/finders/ci/runners_finder.rb @@ -20,6 +20,8 @@ module Ci filter_by_upgrade_status! filter_by_runner_type! filter_by_tag_list! + filter_by_creator_id! + filter_by_version_prefix! sort! request_tag_list! @@ -113,6 +115,21 @@ module Ci end end + def filter_by_creator_id! + creator_id = @params[:creator_id] + @runners = @runners.with_creator_id(creator_id) if creator_id.present? + end + + def filter_by_version_prefix! + return @runners unless @params[:version_prefix] + + sanitized_prefix = @params[:version_prefix][/^[\d+.]+/] + + return @runners unless sanitized_prefix + + @runners = @runners.with_version_prefix(sanitized_prefix) + end + def sort! @runners = @runners.order_by(sort_key) end diff --git a/app/finders/data_transfer/mocked_transfer_finder.rb b/app/finders/data_transfer/mocked_transfer_finder.rb deleted file mode 100644 index 9c5551005ea..00000000000 --- a/app/finders/data_transfer/mocked_transfer_finder.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -# Mocked data for data transfer -# Follow this epic for recent progress: https://gitlab.com/groups/gitlab-org/-/epics/9330 -module DataTransfer - class MockedTransferFinder - def execute - start_date = Date.new(2023, 0o1, 0o1) - date_for_index = ->(i) { (start_date + i.months).strftime('%Y-%m-%d') } - - 0.upto(11).map do |i| - { - date: date_for_index.call(i), - repository_egress: rand(70000..550000), - artifacts_egress: rand(70000..550000), - packages_egress: rand(70000..550000), - registry_egress: rand(70000..550000) - }.tap do |hash| - hash[:total_egress] = hash - .slice(:repository_egress, :artifacts_egress, :packages_egress, :registry_egress) - .values - .sum - end - end - end - end -end diff --git a/app/finders/merge_requests_finder.rb b/app/finders/merge_requests_finder.rb index 95b5b267089..b7de1c08f86 100644 --- a/app/finders/merge_requests_finder.rb +++ b/app/finders/merge_requests_finder.rb @@ -46,6 +46,7 @@ class MergeRequestsFinder < IssuableFinder :merged_before, :reviewer_id, :reviewer_username, + :source_branch, :target_branch, :wip ] @@ -73,7 +74,6 @@ class MergeRequestsFinder < IssuableFinder items = by_deployments(items) items = by_reviewer(items) items = by_source_project_id(items) - items = items.allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/417462") by_approved(items) end @@ -82,7 +82,8 @@ class MergeRequestsFinder < IssuableFinder items = super(items) items = by_negated_reviewer(items) items = by_negated_approved_by(items) - by_negated_target_branch(items) + items = by_negated_target_branch(items) + by_negated_source_branch(items) end private @@ -133,6 +134,12 @@ class MergeRequestsFinder < IssuableFinder items.where.not(target_branch: not_params[:target_branch]) end + + def by_negated_source_branch(items) + return items unless not_params[:source_branch] + + items.where.not(source_branch: not_params[:source_branch]) + end # rubocop: enable CodeReuse/ActiveRecord def by_negated_approved_by(items) diff --git a/app/finders/organizations/user_organizations_finder.rb b/app/finders/organizations/user_organizations_finder.rb new file mode 100644 index 00000000000..739940c44ca --- /dev/null +++ b/app/finders/organizations/user_organizations_finder.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Organizations + class UserOrganizationsFinder + def initialize(current_user, target_user, params = {}) + @current_user = current_user + @target_user = target_user + @params = params + end + + def execute + return Organizations::Organization.none unless can_read_user_organizations? + return Organizations::Organization.none if target_user.blank? + + target_user.organizations + end + + private + + attr_reader :current_user, :target_user, :params + + def can_read_user_organizations? + current_user&.can?(:read_user_organizations, target_user) + end + end +end diff --git a/app/finders/packages/packages_finder.rb b/app/finders/packages/packages_finder.rb index 31fbbfb7937..8fe1a73a030 100644 --- a/app/finders/packages/packages_finder.rb +++ b/app/finders/packages/packages_finder.rb @@ -22,6 +22,7 @@ module Packages packages = filter_by_package_type(packages) packages = filter_by_package_name(packages) packages = filter_by_status(packages) + packages = filter_by_package_version(packages) order_packages(packages) end diff --git a/app/finders/packages/pypi/packages_finder.rb b/app/finders/packages/pypi/packages_finder.rb index 17138134eb3..944824bee6e 100644 --- a/app/finders/packages/pypi/packages_finder.rb +++ b/app/finders/packages/pypi/packages_finder.rb @@ -3,6 +3,8 @@ module Packages module Pypi class PackagesFinder < ::Packages::GroupOrProjectPackageFinder + extend ::Gitlab::Utils::Override + def execute return packages unless @params[:package_name] @@ -14,6 +16,15 @@ module Packages def packages base.pypi.has_version end + + override :group_packages + def group_packages + packages_visible_to_user( + @current_user, + within_group: @project_or_group, + with_package_registry_enabled: true + ) + end end end end diff --git a/app/finders/projects/ml/model_finder.rb b/app/finders/projects/ml/model_finder.rb index 1e407ba4aa4..57e0620c7a7 100644 --- a/app/finders/projects/ml/model_finder.rb +++ b/app/finders/projects/ml/model_finder.rb @@ -3,16 +3,58 @@ module Projects module Ml class ModelFinder - def initialize(project) + include Gitlab::Utils::StrongMemoize + + VALID_ORDER_BY = %w[name created_at id].freeze + VALID_SORT = %w[asc desc].freeze + + def initialize(project, params = {}) @project = project + @params = params end def execute - ::Ml::Model - .by_project(@project) - .including_latest_version - .with_version_count + relation + end + + def count + relation.length + end + + private + + def relation + @models = ::Ml::Model + .by_project(project) + .including_latest_version + .including_project + .with_version_count + + @models = by_name + ordered + end + strong_memoize_attr :relation + + def by_name + return models unless params[:name].present? + + models.by_name(params[:name]) + end + + def ordered + order_by = valid_or_default(params[:order_by]&.downcase, VALID_ORDER_BY, 'created_at') + sort = valid_or_default(params[:sort]&.downcase, VALID_SORT, 'desc') + + models.order_by("#{order_by}_#{sort}").with_order_id_desc end + + def valid_or_default(value, valid_values, default) + return value if valid_values.include?(value) + + default + end + + attr_reader :params, :project, :models end end end diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index 87edf36d1ce..1aa5245590e 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -28,6 +28,7 @@ # last_activity_before: datetime # repository_storage: string # not_aimed_for_deletion: boolean +# full_paths: string[] # class ProjectsFinder < UnionFinder include CustomAttributesFilter @@ -76,8 +77,9 @@ class ProjectsFinder < UnionFinder # EE would override this to add more filters def filter_projects(collection) - collection = collection.without_deleted + collection = by_deleted_status(collection) collection = by_ids(collection) + collection = by_full_paths(collection) collection = by_personal(collection) collection = by_starred(collection) collection = by_trending(collection) @@ -153,6 +155,12 @@ class ProjectsFinder < UnionFinder params[:min_access_level].present? end + def by_deleted_status(items) + return items.without_deleted unless current_user&.can?(:admin_all_resources) + + params[:include_pending_delete].present? ? items : items.without_deleted + end + # rubocop: disable CodeReuse/ActiveRecord def by_ids(items) items = items.where(id: project_ids_relation) if project_ids_relation @@ -162,6 +170,10 @@ class ProjectsFinder < UnionFinder end # rubocop: enable CodeReuse/ActiveRecord + def by_full_paths(items) + params[:full_paths].present? ? items.where_full_path_in(params[:full_paths], use_includes: false) : items + end + def union(items) find_union(items, Project).with_route end diff --git a/app/finders/user_group_notification_settings_finder.rb b/app/finders/user_group_notification_settings_finder.rb index c6a1a6b36d1..8d06d3d18ca 100644 --- a/app/finders/user_group_notification_settings_finder.rb +++ b/app/finders/user_group_notification_settings_finder.rb @@ -11,11 +11,16 @@ class UserGroupNotificationSettingsFinder @loaded_groups_with_ancestors = groups_with_ancestors.index_by(&:id) @loaded_notification_settings = user.notification_settings_for_groups(groups_with_ancestors).preload_source_route.index_by(&:source_id) - preload_emails_disabled + preload_emails_enabled - groups.map do |group| + group_notifications = groups.map do |group| find_notification_setting_for(group) end + + group_sources = group_notifications.map(&:source) + ActiveRecord::Associations::Preloader.new(records: group_sources, associations: :namespace_settings).call + + group_notifications end private @@ -45,18 +50,18 @@ class UserGroupNotificationSettingsFinder parent_setting.level != NotificationSetting.levels[:global] || parent_setting.notification_email.present? end - # This method preloads the `emails_disabled` strong memoized method for the given groups. + # This method preloads the `emails_enabled` strong memoized method for the given groups. # - # For each group, look up the ancestor hierarchy and look for any group where emails_disabled is true. + # For each group, look up the ancestor hierarchy and look for any group where emails_enabled is false. # The lookup is implemented with an EXISTS subquery, so we can look up the ancestor chain for each group individually. # The query will return groups where at least one ancestor has the `emails_disabled` set to true. # # After the query, we set the instance variable. - def preload_emails_disabled + def preload_emails_enabled group_ids_with_disabled_email = Group.ids_with_disabled_email(groups.to_a) groups.each do |group| - group.emails_disabled_memoized = group_ids_with_disabled_email.include?(group.id) if group.parent_id + group.emails_enabled_memoized = group_ids_with_disabled_email.exclude?(group.id) if group.parent_id end end end |