Skip to content

Use new API Endpoint to check for repositories with tag

Adie (she/her) requested to merge 388537-new-endpoint-container-registry into master

What does this MR do and why?

Closes #388537 (closed).

Feature flag rollout issue: #392372 (closed)

There is a new list sub-repositories endpoint that was introduced to the Registry Gitlab's v1 API.

GET /gitlab/v1/repository-paths/<namespace>/repositories/list/?n=1

We would like to update rails to use this new API endpoint as it is faster and queries more directly (vs. via namespace).

In this MR, we are updating the implementation of two methods under Namespace:

Old version:

  def any_project_has_container_registry_tags?
    all_projects.includes(:container_repositories).any?(&:has_container_registry_tags?)
  end

  def first_project_with_container_registry_tags
    all_projects.find(&:has_container_registry_tags?)
  end

We would like them to utilize the new API endpoint. We start by adding the endpoint in ContainerRegistry::GitlabApiClient. From the discussions in #388537 (comment 1263733512), we said to maintain the fetching of the repository details (vs just the existence of it).

The new API endpooint only works for Gitlab.com, so we do check for that.

If it's in Gitlab.com, then we use the new endpoint else we use the old implementation (optimized with .includes).

def first_project_with_container_registry_tags
    if Gitlab.com?
      ContainerRegistry::GitlabApiClient.one_project_with_container_registry_tag(full_path)
    else
      all_projects.includes(:container_repositories).find(&:has_container_registry_tags?)
    end
end

We then update the method any_project_has_container_registry_tags to use that:

def any_project_has_container_registry_tags?
   first_project_with_container_registry_tags.present?
end

There were several tests that needed a stub when calling the new endpoint. They were:

  • spec/models/container_repository_spec.rb
  • spec/workers/container_registry/migration/guard_worker_spec.rb
  • ee/spec/lib/ee/gitlab/hook_data/user_builder_spec.rb

These were due to the user having a callback to User#namespace_move_dir_allowed which calls any_project_has_container_registry_tags that subsequently calls the GitlabApiClient with this new implementation.

Screenshots or screen recordings

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

How to set up and validate locally

As this is a gitlab/v1 endpoint, please make sure to have the metadata database setup to make it work.

You may try to call the methods (i.e. #first_project_with_container_registry_tags) via the console and notice the logs showing the /gitlab/v1 URL being hit.

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 #388537 (closed)

Edited by Adie (she/her)

Merge request reports