Skip to content

Use metadata cache for npm metadata endpoint at project level

What does this MR do and why?

This MR changes npm metadata endpoint at the project level to return cached metadata when it exists.

We still keep the generation "on the fly" as a fallback when metadata cache doesn't exist.

Screenshots or screen recordings

No.

How to set up and validate locally

This is the mix of steps that should be either executed in rails console (ruby commands) or in the terminal (curl commands).

  1. Enable the npm_metadata_cache feature flag for chosen project.

    Feature.enable(:npm_metadata_cache, Project.first)
  2. Create a new npm package with several tags.

    def fixture_file_upload(*args, **kwargs)
      Rack::Test::UploadedFile.new(*args, **kwargs)
    end
    
    p = FactoryBot.create(:npm_package, name: 'test', project: Project.first)
    
    FactoryBot.create_list(:packages_tag, 5, package: p)
    
    p.name
    p.project_id

    Note: note down the package's name and project id - they'll be used in the following steps.

  3. Call the metadata endpoint.

    $ curl --header "PRIVATE-TOKEN: <your-token>" "http://gdk.test:3000/api/v4/projects/<project-id>/packages/npm/<package-name>"

    Note: replace <your-token> with your personal access token and <project-id> and <package-name> with the project id and package name from step (1.).

    Observation: In this case we check that in case metadata cache is missing there is still a fallback to "on the fly" generation.

  4. Make sure sidekiq is up and running. Create a metadata cache.

    Packages::Npm::CreateMetadataCacheWorker.new.perform(Project.first.id, <package-name>)

    Note: replace <package-name> with the name of your package.

  5. Call the metadata endpoint again.

    $ curl --header "PRIVATE-TOKEN: <your-token>" "http://gdk.test:3000/api/v4/projects/<project-id>/packages/npm/<package-name>"

    Note: replace <your-token> with your personal access token and <project-id> and <package-name> with the project id and package name from step (1.).

    Observation: This time the metadata should come from Packages::Npm::MetadataCache object created in the previous step.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #393655 (closed)

Edited by Dzmitry (Dima) Meshcharakou

Merge request reports

Loading