Call rename endpoint with push/pull token in GitlabAPIClient
Overview
In container-registry#894 (closed) and container-registry#895 (closed), a new API endpoint to rename base repositories have been added to the Container Registry.
In this MR, we allow GitlabApiClient
to call this new API endpoint with a push/pull token as the registry endpoint will only allow requests from JWTs with pull scopes: {{repository-path}}/*
, {{repository-path}}
AND push scope: {{repository-path}}
.
You can find the documentation to the API endpoint here: https://gitlab.com/gitlab-org/container-registry/-/blob/master/docs-gitlab/api.md#rename-base-repository. From the documentation, we can see that we can pass a dry_run
option to the API endpoint and we also support that in this MR.
In a succeeding PR, we will use this new function in the GitlabApiClient
in the actual rename operation.
Note: To run this, the metadata database should be setup and redis should be connected as well.
How to set up and validate locally
- In the Rails Console, get a project
project = Project.last
- We then call the rename API endpoint via
GitlabApiClient
. We first try it with setting thedry_run
parameter totrue
.ContainerRegistry::GitlabApiClient.rename_base_repository_path(project.full_path, name: "newname", dry_run: true)
- When successful, the command above should return:
:lease_accepted
- We can then try to execute the operation by setting
dry_run
tofalse
.ContainerRegistry::GitlabApiClient.rename_base_repository_path(project.full_path, name: "newname", dry_run: false)
- And this should return an
:ok
::ok
- We can then check in the database of the registry the changed name. Access the
psql
command, enter the registry database, and query for the container repositories. The new name should now reflect.psql -U postgres \c registry_dev select * from repositories;
Example Run
project = Project.last
ContainerRegistry::GitlabApiClient.rename_base_repository_path(project.full_path, name: "newname", dry_run: true)
# => :lease_accepted
The change is not yet made due to dry_run: true
and if we check the registry database, we can still see the original project name:
registry_dev=# select * from repositories;
id | top_level_namespace_id | parent_id | created_at | updated_at | name | path | deleted_at
----+------------------------+-----------+-------------------------------+------------+--------+----------------------+------------
2 | 2 | | 2023-10-01 13:52:21.505244+02 | | proj-1 | october-group/proj-1 |
With:
ContainerRegistry::GitlabApiClient.rename_base_repository_path(project.full_path, name: "newname", dry_run: false)
# => :ok
The change has been persisted and querying for the project again, we see its updated name
registry_dev=# select * from repositories;
id | top_level_namespace_id | parent_id | created_at | updated_at | name | path | deleted_at
----+------------------------+-----------+-------------------------------+------------+---------+-----------------------+------------
2 | 2 | | 2023-10-01 13:52:21.505244+02 | | newname | october-group/newname |
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #423995 (closed)