debian-mirror-gitlab/app/finders/packages/maven/package_finder.rb

85 lines
2.4 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
2021-03-08 18:12:59 +05:30
2020-07-28 23:09:34 +05:30
module Packages
module Maven
class PackageFinder
2021-04-29 21:17:54 +05:30
include ::Packages::FinderHelper
include Gitlab::Utils::StrongMemoize
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
def initialize(path, current_user, project: nil, group: nil, order_by_package_file: false)
2020-07-28 23:09:34 +05:30
@path = path
@current_user = current_user
@project = project
@group = group
2021-04-29 21:17:54 +05:30
@order_by_package_file = order_by_package_file
2020-07-28 23:09:34 +05:30
end
def execute
packages_with_path.last
end
def execute!
packages_with_path.last!
end
private
def base
2021-04-29 21:17:54 +05:30
if @project
2020-07-28 23:09:34 +05:30
packages_for_a_single_project
2021-04-29 21:17:54 +05:30
elsif @group
2020-07-28 23:09:34 +05:30
packages_for_multiple_projects
else
2021-03-08 18:12:59 +05:30
::Packages::Package.none
2020-07-28 23:09:34 +05:30
end
end
def packages_with_path
2021-04-29 21:17:54 +05:30
matching_packages = base.only_maven_packages_with_path(@path, use_cte: @group.present?)
if group_level_improvements?
matching_packages = matching_packages.order_by_package_file if @order_by_package_file
else
matching_packages = matching_packages.order_by_package_file if versionless_package?(matching_packages)
end
2021-03-08 18:12:59 +05:30
matching_packages
2020-07-28 23:09:34 +05:30
end
2021-03-08 18:12:59 +05:30
def versionless_package?(matching_packages)
return if matching_packages.empty?
# if one matching package is versionless, they all are.
matching_packages.first&.version.nil?
2020-07-28 23:09:34 +05:30
end
# Produces a query that retrieves packages from a single project.
def packages_for_a_single_project
2021-04-29 21:17:54 +05:30
@project.packages
2020-07-28 23:09:34 +05:30
end
# Produces a query that retrieves packages from multiple projects that
# the current user can view within a group.
def packages_for_multiple_projects
2021-04-29 21:17:54 +05:30
if group_level_improvements?
packages_visible_to_user(@current_user, within_group: @group)
else
::Packages::Package.for_projects(projects_visible_to_current_user)
end
2020-07-28 23:09:34 +05:30
end
# Returns the projects that the current user can view within a group.
def projects_visible_to_current_user
2021-04-29 21:17:54 +05:30
@group.all_projects
.public_or_visible_to_user(@current_user)
end
def group_level_improvements?
strong_memoize(:group_level_improvements) do
Feature.enabled?(:maven_packages_group_level_improvements, default_enabled: :yaml)
end
2020-07-28 23:09:34 +05:30
end
end
end
end