2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
module FinderHelper
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
InvalidPackageTypeError = Class.new(StandardError)
|
|
|
|
InvalidStatusError = Class.new(StandardError)
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
private
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def packages_for_project(project)
|
|
|
|
project.packages.installable
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def packages_visible_to_user(user, within_group:)
|
|
|
|
return ::Packages::Package.none unless within_group
|
2021-04-29 21:17:54 +05:30
|
|
|
return ::Packages::Package.none unless Ability.allowed?(user, :read_group, within_group)
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
projects = projects_visible_to_reporters(user, within_group: within_group)
|
2021-06-08 01:23:25 +05:30
|
|
|
::Packages::Package.for_projects(projects.select(:id)).installable
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def projects_visible_to_user(user, within_group:)
|
|
|
|
return ::Project.none unless within_group
|
2021-04-29 21:17:54 +05:30
|
|
|
return ::Project.none unless Ability.allowed?(user, :read_group, within_group)
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
projects_visible_to_reporters(user, within_group: within_group)
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def projects_visible_to_reporters(user, within_group:)
|
2021-06-08 01:23:25 +05:30
|
|
|
if user.is_a?(DeployToken) && Feature.enabled?(:packages_finder_helper_deploy_token, default_enabled: :yaml)
|
2021-04-29 21:17:54 +05:30
|
|
|
user.accessible_projects
|
|
|
|
else
|
|
|
|
within_group.all_projects
|
|
|
|
.public_or_visible_to_user(user, ::Gitlab::Access::REPORTER)
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
def package_type
|
|
|
|
params[:package_type].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_package_type(packages)
|
2021-06-08 01:23:25 +05:30
|
|
|
return packages.without_package_type(:terraform_module) unless package_type
|
2021-03-11 19:13:27 +05:30
|
|
|
raise InvalidPackageTypeError unless ::Packages::Package.package_types.key?(package_type)
|
|
|
|
|
|
|
|
packages.with_package_type(package_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_package_name(packages)
|
|
|
|
return packages unless params[:package_name].present?
|
|
|
|
|
|
|
|
packages.search_by_name(params[:package_name])
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def filter_by_package_version(packages)
|
|
|
|
return packages unless params[:package_version].present?
|
|
|
|
|
|
|
|
packages.with_version(params[:package_version])
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def filter_with_version(packages)
|
|
|
|
return packages if params[:include_versionless].present?
|
|
|
|
|
|
|
|
packages.has_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_status(packages)
|
|
|
|
return packages.displayable unless params[:status].present?
|
|
|
|
raise InvalidStatusError unless Package.statuses.key?(params[:status])
|
|
|
|
|
|
|
|
packages.with_status(params[:status])
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|