Optimize ContainerRepository#has_tags? to only check for one tag

What does this MR do and why?

ContainerRepository#has_tags? calls ContainerRepository#tags which loads all pages of tags when the GitLab API is supported. In this MR, we only get one tags page with a page_size of 1 when checking has_tags?

This optimization was first explored in !183128 (merged) and we said to do it in another MR to keep it focused.

This change is added behind the feature flag: optimize_container_repository_has_tags since has_tags? is used in a lot of places.

How to set up and validate locally

In the rails console, try the following command with the GitLab API supported (w/ metadata database):

ContainerRepository.first.has_tags?

👉 When the feature optimize_container_repository_has_tags is enabled, from the logs, you can notice n=1:

Feature.enable(:optimize_container_repository_has_tags)
"uri":"/gitlab/v1/repositories/group-may2024/project-may2024/samp/tags/list/?n=1"
2025-07-19_07:35:42.91877 registry                : {"content_type":"application/json","correlation_id":"01K0GSRE23KC1C1VHV626M8YAE","duration_ms":2,"host":"gdk.test:5100","level":"info","method":"GET","msg":"access","proto":"HTTP/1.1","read_bytes":1167,"referrer":"","remote_addr":"172.16.123.1:54843","remote_ip":"172.16.123.1","status":404,"system":"http","time":"2025-07-19T09:35:42.918+02:00","ttfb_ms":2,"uri":"/gitlab/v1/repositories/group-may2024/project-may2024/samp/tags/list/?n=1","user_agent":"GitLab/18.2.0-pre","written_bytes":142}

👉 When the feature optimize_container_repository_has_tags is disabled, you can notice the n=100.

Feature.disable(:optimize_container_repository_has_tags)
"uri":"/gitlab/v1/repositories/group-may2024/project-may2024/samp/tags/list/?n=100"
2025-07-19_07:37:30.13579 registry                : {"content_type":"application/json","correlation_id":"01K0GSVPRK0WQJVASKRF7PFNRN","duration_ms":3,"host":"gdk.test:5100","level":"info","method":"GET","msg":"access","proto":"HTTP/1.1","read_bytes":1169,"referrer":"","remote_addr":"172.16.123.1:55304","remote_ip":"172.16.123.1","status":404,"system":"http","time":"2025-07-19T09:37:30.135+02:00","ttfb_ms":3,"uri":"/gitlab/v1/repositories/group-may2024/project-may2024/samp/tags/list/?n=100","user_agent":"GitLab/18.2.0-pre","written_bytes":142}

Without the metadata database, it has the same behaviour as before where if both manifest and manifest['tags'] are present, then has_tags? is true, otherwise, it is false.

MR acceptance checklist

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

Related to #548145 (closed)

Edited by Adie (she/her)

Merge request reports

Loading