diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-04 15:17:18 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-04 15:17:18 +0300 |
commit | fb5d3cceb8d43f8c2dc22a5d8c74327e9397f2e8 (patch) | |
tree | dbd3a17217fa46cf279ed692b605e03222fca360 /app/services/packages | |
parent | 6cd4578a23ffe0fb94632f83a07a25d01f8d6821 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/packages')
-rw-r--r-- | app/services/packages/conan/search_service.rb | 37 | ||||
-rw-r--r-- | app/services/packages/conan/single_package_search_service.rb | 50 |
2 files changed, 66 insertions, 21 deletions
diff --git a/app/services/packages/conan/search_service.rb b/app/services/packages/conan/search_service.rb index df22a895c00..c65c9a85da8 100644 --- a/app/services/packages/conan/search_service.rb +++ b/app/services/packages/conan/search_service.rb @@ -8,10 +8,6 @@ module Packages WILDCARD = '*' RECIPE_SEPARATOR = '@' - def initialize(user, params) - super(nil, user, params) - end - def execute ServiceResponse.success(payload: { results: search_results }) end @@ -23,35 +19,34 @@ module Packages return search_for_single_package(sanitized_query) if params[:query].include?(RECIPE_SEPARATOR) - search_packages(build_query) + search_packages end def wildcard_query? params[:query] == WILDCARD end - def build_query - return "#{sanitized_query}%" if params[:query].end_with?(WILDCARD) - - sanitized_query - end - - def search_packages(query) - ::Packages::Conan::PackageFinder.new(current_user, query: query).execute.map(&:conan_recipe) + def sanitized_query + @sanitized_query ||= sanitize_sql_like(params[:query].delete(WILDCARD)) end def search_for_single_package(query) - name, version, username, _ = query.split(%r{[@/]}) - full_path = Packages::Conan::Metadatum.full_path_from(package_username: username) - project = Project.find_by_full_path(full_path) - return unless Ability.allowed?(current_user, :read_package, project&.packages_policy_subject) + ::Packages::Conan::SinglePackageSearchService + .new(query, current_user) + .execute[:results] + end - result = project.packages.with_name(name).with_version(version).order_created.last - [result&.conan_recipe].compact + def search_packages + ::Packages::Conan::PackageFinder + .new(current_user, { query: build_query }, project: project) + .execute + .map(&:conan_recipe) end - def sanitized_query - @sanitized_query ||= sanitize_sql_like(params[:query].delete(WILDCARD)) + def build_query + return "#{sanitized_query}%" if params[:query].end_with?(WILDCARD) + + sanitized_query end end end diff --git a/app/services/packages/conan/single_package_search_service.rb b/app/services/packages/conan/single_package_search_service.rb new file mode 100644 index 00000000000..e133b35c2cf --- /dev/null +++ b/app/services/packages/conan/single_package_search_service.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module Packages + module Conan + class SinglePackageSearchService # rubocop:disable Search/NamespacedClass + include Gitlab::Utils::StrongMemoize + + def initialize(query, current_user) + @name, @version, @username, _ = query.split(%r{[@/]}) + @current_user = current_user + end + + def execute + ServiceResponse.success(payload: { results: search_results }) + end + + private + + attr_reader :name, :version, :username, :current_user + + def search_results + return [] unless can_access_project_package? + + [package&.conan_recipe].compact + end + + def package + project + .packages + .with_name(name) + .with_version(version) + .order_created + .last + end + + def project + Project.find_by_full_path(full_path) + end + strong_memoize_attr :project + + def full_path + ::Packages::Conan::Metadatum.full_path_from(package_username: username) + end + + def can_access_project_package? + Ability.allowed?(current_user, :read_package, project.try(:packages_policy_subject)) + end + end + end +end |