Skip to content

Filter tags from API for exact match in ContainerRepository#tag

Adie (she/her) requested to merge 440328-add-filter-to-find-exact-match-tag into master

What does this MR do and why?

In !141838 (merged), we introduced the usage of the list repository tags API which accepts a parameter name that it uses to filter tags.

We have a reported issue where there are multiple tags that have the tag name as a substring. Example:

  • a-main
  • main

So when fetching the tag main, both are returned but we get the first result and in this case, it will be a-main. In this MR, we filter the results further and make sure that we return the tag with the exact name as the provided.

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.

How to set up and validate locally

To use the feature, we need to simulate that we have migrated repositories. For now, only Gitlab.com is recognized as migrated but we can update the check to simulate that our local setup is also migrated. Update the following method in app/models/container_repository.rb and add true ||

def migrated?
  true || Gitlab.com_except_jh?
end

Make sure to also have the container registry running and that you have tags available. It would be great if you can have tags that are substrings of a tag (i.e. a-main, main, main-b). As we want to test that the right tag is fetched.

  1. We can run the following in the Rails Console and it should return true:
repository = ContainerRepository.find(<repository-id>)
repository.migrated? && ContainerRegistry::GitlabApiClient.supports_gitlab_api? #=> true
  1. We then call the Gitlab API. You can run the following command in your terminal (your personal access token should have access to the project and the container registry):
curl --header "PRIVATE-TOKEN: <your-personal-access-token-here>" \
     "http://gdk.test:3000/api/v4/projects/<project-id>/registry/repositories/<repository id>/tags/<tag-name>"

You may find the project id and repository id via the Rails Console.

The result would look something like:

{"name":"latest","path":"october-group/proj-lalalo:latest","location":"registry.test:5000/october-group/proj-lalalo:latest","revision":"8ac438ac5ec8f4f2ec2ac99afa64a514","short_revision":"8ac438ac5","digest":"sha256:02399a844fe2a1e4ce421decfb31ca938","created_at":"2023-12-20T17:19:17.843+00:00","total_size":185117038}
  1. Using the Rails console, you can also call ContainerRepository#tag and verify that you get the right tag.
repo = ContainerRepository.find(id)
tag = repo.tag(tag_name)

Please verify that the tag.name is tag_name. And the attribute @from_api of tag should be true (otherwise, verify that you have the container registry running and that repo.migrated? is true).

Related to #440328 (closed)

Edited by Adie (she/her)

Merge request reports