debian-mirror-gitlab/app/services/packages/conan/search_service.rb

59 lines
1.5 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
module Conan
class SearchService < BaseService
include ActiveRecord::Sanitization::ClassMethods
WILDCARD = '*'
RECIPE_SEPARATOR = '@'
def initialize(user, params)
super(nil, user, params)
end
def execute
ServiceResponse.success(payload: { results: search_results })
end
private
def search_results
return [] if wildcard_query?
return search_for_single_package(sanitized_query) if params[:query].include?(RECIPE_SEPARATOR)
search_packages(build_query)
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)
end
def search_for_single_package(query)
2021-09-30 23:02:18 +05:30
name, version, username, _ = query.split(%r{[@/]})
2020-07-28 23:09:34 +05:30
full_path = Packages::Conan::Metadatum.full_path_from(package_username: username)
project = Project.find_by_full_path(full_path)
2021-04-17 20:07:23 +05:30
return unless Ability.allowed?(current_user, :read_package, project)
2020-07-28 23:09:34 +05:30
result = project.packages.with_name(name).with_version(version).order_created.last
[result&.conan_recipe].compact
end
def sanitized_query
@sanitized_query ||= sanitize_sql_like(params[:query].delete(WILDCARD))
end
end
end
end