Skip to content

Draft: Create npm search service

Moaz Khalifa requested to merge 475037-create-npm-search-service into master

Context

In order to implement NPM Package Registry add Search Endpoint (#475037), we need to be able to search for packages in the npm Registry. The first step is to create a search service that could receive the needed arguments and respond with the matching results.

It should be mentioned that $ npm search returns only the latest version of the found packages. So if we have multiple versions of the same package, the search service should return only the latest version, not all versions. This $ npm search behavior can be tested by searching for any package that we know it has many versions on the NPM registry.

For example, yargs package has 250 published versions, but if you searched for it by $ npm search yargs, only the latest version will be retuned in the search results, plus some other packages that have the yargs part in their names, or in their keywords and other identifiers.

In our first iteration, we only search in the packages names. So the service does a fuzzy search using the search term and returns the matching packages. Later, we could support searching in other metadata, such as keywords or description.

In a subsequent MR, we are going to implement the search endpoint that would call the search service.

What does this MR do?

  • Add Packages::Npm::SearchService
  • Add specs for the service

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

We can test the service in rails console:

# stub file upload
def fixture_file_upload(*args, **kwargs)
  Rack::Test::UploadedFile.new(*args, **kwargs)
end

project = Project.last

# create some npm packages to test with
pkg1 = FactoryBot.create(:npm_package, project: project, name: 'DummyPackageA')
pkgs2 = FactoryBot.create_list(:npm_package, 3, project: project, name: 'DummyPackageB')
pkgs3 = FactoryBot.create_list(:npm_package, 3, project: project, name: 'DummyPackageC')
pkg4 = FactoryBot.create(:npm_package, project: project, name: 'FooBarD')
pkg5 = FactoryBot.create(:npm_package, name: 'DummyPackageA')

# call the service with a search term. The matching packages should be returned, and note that only the latest version of `pkgs2` & `pkgs3` is returned
Packages::Npm::SearchService.new(project, User.first, {text: 'ummy', size: 5, from: 0}).execute

# Play around with variations of the params and conditions. More testing cases can be found in the specs.

Related to #475037

Edited by Moaz Khalifa

Merge request reports

Loading